CDK Construct that lets you build AWS Api Gateway Http Api, backed by Lambdas, based on a OpenAPI spec file.
- Deploy Api Gateway Http Api based on a OpenAPI spec file.
- Each API Route will be backed by 1 NodeJS lambda.
- Configure CORS for your API.
- Add custom domain (eg: https://my-awesome-api.my-domain.com) to your API.
- Enable custom authorizers to Http Api Lambda integrations.
- Customize your lambdas's memory, timeouts, log retention, env variables and other stuff.
Add latest package to your project with npm/yarn
npm i --save cdk-http-openapi
yarn add -D cdk-http-openapiimport{HttpOpenApi}from'cdk-http-openapi'import{Construct}from'constructs'import{Stack}from'aws-cdk-lib'exportclassMyServiceStackextendsStack{constructor(scope: Construct,id: string,){super(scope,id)// other resources for your microservice// define your API + lambdasconstapi=newHttpOpenApi(this,'MyApi',{functionNamePrefix: 'cool-api',openApiSpec: './openapi.yml',lambdasSourcePath: './dist/src',// optional. It defaults to './.build/src'integrations: [{operationId: 'getEntity',// for each operation you define in your OpenAPI spechandler: 'api.getEntity',// you can register a lambda handler to handle your http request},{operationId: 'storeEntity',handler: 'api.storeEntity',timeoutSeconds: 5,// timeout configuration for lambdamemorySize: 512,// memory configuration for lambdaenv: {// you can also inject ENV variables into your lambdaDB_HOST: '...',DB_USERNAME: '...',DB_PASSWORD: '...'},}]});}}constapi=newHttpOpenApi(this,'MyApi',{
...
})constdomainName='my-awesome-api.cool.io';constcertificateArn=`arn:aws:acm:${AWS_REGION}:${AWS_ACCOUNT}:certificate/${CERTIFICATE_ID}`;consthostedZone='cool.io';api.enableCustomDomain(domainName,certificateArn,hostedZone);constapi=newHttpOpenApi(this,'MyApi',{functionNamePrefix: 'my-service',openApiSpec: './openapi.yml',// ... other propscorsAllowAllOrigins: true,});constapi=newHttpOpenApi(this,'MyApi',{functionNamePrefix: 'my-service',openApiSpec: './openapi.yml',// ... other props// See AWS Docs for more info: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-cors.htmlcorsConfig: {allowMethods: ['GET','POST'],allowHeaders: ['application/json','authorization'],allowOrigins: ['https://my-frontend.com']}});constapi=newHttpOpenApi(this,'MyApi',{functionNamePrefix: 'my-service',openApiSpec: './openapi.yml',// ... other propscustomAuthorizerLambdaArn: `arn:aws:lambda:${AWS_REGION}:${AWS_ACCOUNT}:function:${AUTHORIZER_LAMBDA_NAME}`});