Created
June 24, 2020 20:22
-
-
Save mlkirkpatrick/b0ccad32a47fbe6c6102d1d54e2bf277 to your computer and use it in GitHub Desktop.
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
const api = new apigateway.RestApi(this, 'ApiGateway', { | |
restApiName: props.stackName, | |
description: 'My API', | |
endpointExportName: `${props.stackName}-api-url`, | |
deployOptions: { | |
stageName: props.stage, | |
}, | |
defaultCorsPreflightOptions: { | |
allowOrigins: ['*'], | |
allowCredentials: false, | |
statusCode: 200, | |
}, | |
}) | |
api.addRequestValidator('RequestValidator', { | |
validateRequestParameters: true, | |
}) | |
const defaultMethodOptions = { | |
requestParameters: { | |
'method.request.querystring.preview': false, | |
'method.request.querystring.query': true, | |
}, | |
} | |
const secureMethodOptions = { | |
...defaultMethodOptions, | |
authorizationType: apigateway.AuthorizationType.CUSTOM, | |
authorizer: new apigateway.TokenAuthorizer(this, 'JwtAuthorizer', { | |
handler: lambda.Function.fromFunctionArn( | |
this, | |
'AuthorizerFunction', | |
`arn:aws:lambda:${this.region}:${this.account}:function:lambda-authorizer-${props.stage}`, | |
), | |
identitySource: 'method.request.header.Authorization', | |
authorizerName: 'jwt', | |
resultsCacheTtl: cdk.Duration.minutes(5), | |
}), | |
requestParameters: { | |
...defaultMethodOptions.requestParameters, | |
'method.request.header.Authorization': true, | |
}, | |
} | |
const resourceSetup = [ | |
{ path: 'query', secure: false, lambda: directQueryLambda }, | |
{ path: 'secureQuery', secure: true, lambda: directQueryLambda }, | |
{ path: 'livequery', secure: false, lambda: directQueryLambda }, | |
{ path: 'preview/query', secure: false, lambda: directQueryLambda }, | |
{ path: 'purl', secure: false, lambda: purlQueryLambda }, | |
{ path: 'purlSecure', secure: true, lambda: purlQueryLambda }, | |
{ path: 'archiveSecure', secure: true, lambda: archiveQueryLambda }, | |
] | |
new HierarchicalRestApiResources({ | |
api: api, | |
resources: resourceSetup.map((setupProps) => ({ | |
pathPart: setupProps.path, | |
options: { | |
defaultMethodOptions: setupProps.secure ? secureMethodOptions : defaultMethodOptions, | |
}, | |
methods: [ | |
{ | |
httpMethod: 'GET', | |
integration: new apigateway.LambdaIntegration(setupProps.lambda), | |
}, | |
], | |
})), | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment