Skip to content

Commit

Permalink
the actual demo part
Browse files Browse the repository at this point in the history
  • Loading branch information
martzcodes committed Aug 6, 2020
1 parent 221fcf0 commit 9dd0361
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
33 changes: 32 additions & 1 deletion lib/blog-cdk-apigwv2-token-stack.ts
@@ -1,9 +1,40 @@
import * as cdk from '@aws-cdk/core';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import { HttpApi, CfnAuthorizer, LambdaProxyIntegration, HttpMethod, CfnRoute } from '@aws-cdk/aws-apigatewayv2';
import { Runtime } from '@aws-cdk/aws-lambda';

export class BlogCdkApigwv2TokenStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

// The code that defines your stack goes here
const someLambda = new NodejsFunction(this, "someLambdaFunction", {
entry: `${__dirname}/some-lambda.ts`,
handler: "handler",
runtime: Runtime.NODEJS_12_X
});
const httpApi = new HttpApi(this, "AuthorizedApi");
const someIntegration = new LambdaProxyIntegration({
handler: someLambda,
});
const routes = httpApi.addRoutes({
path: "/",
methods: [HttpMethod.GET],
integration: someIntegration,
});
const authorizer = new CfnAuthorizer(this, "SomeAuthorizer", {
apiId: httpApi.httpApiId,
authorizerType: "JWT", // HAS TO BE JWT FOR HTTP APIs !?!
identitySource: ["$request.header.Authorization"],
name: "some-authorizer",
jwtConfiguration: {
issuer: "https://martzcodes.us.auth0.com/",
audience: ["https://martzcodes.us.auth0.com/api/v2/"],
},
});
routes.forEach((route) => {
const routeCfn = route.node.defaultChild as CfnRoute;
routeCfn.authorizerId = authorizer.ref;
routeCfn.authorizationType = "JWT"; // THIS HAS TO MATCH THE AUTHORIZER TYPE ABOVE
});
}
}
9 changes: 9 additions & 0 deletions lib/some-lambda.ts
@@ -0,0 +1,9 @@
export const handler = async (event: any): Promise<any> => {
console.log(JSON.stringify(event));

return {
statusCode: 200,
headers: {},
body: "yeah it worked",
};
};

0 comments on commit 9dd0361

Please sign in to comment.