Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
221fcf0
commit 9dd0361
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
}; | ||
}; |