Skip to content

Repository files navigation

cdk-http-openapi

CDK Construct that lets you build AWS Api Gateway Http Api, backed by Lambdas, based on a OpenAPI spec file.

Features

  • 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.

Setup

Add latest package to your project with npm/yarn

npm i --save cdk-http-openapi
yarn add -D cdk-http-openapi

Start using cdk constructs in your infrastructure definition

Examples

1. Define an Http API + 1 lambda for each route

import{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: '...'},}]});}}

2. Enable Custom Domain for your API: my-awesome-api.cool.io

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);

3. Configure wildcard CORS (allow '*')

constapi=newHttpOpenApi(this,'MyApi',{functionNamePrefix: 'my-service',openApiSpec: './openapi.yml',// ... other propscorsAllowAllOrigins: true,});

4. Configure CORS more specific

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']}});

5. Enable Customer Authorizer Lambda for your endpoints

constapi=newHttpOpenApi(this,'MyApi',{functionNamePrefix: 'my-service',openApiSpec: './openapi.yml',// ... other propscustomAuthorizerLambdaArn: `arn:aws:lambda:${AWS_REGION}:${AWS_ACCOUNT}:function:${AUTHORIZER_LAMBDA_NAME}`});

About

CDK Construct that lets you build AWS Api Gateway Http Api, backed by Lambdas, based on a OpenAPI spec file.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages