Skip to content

Instantly share code, notes, and snippets.

@fredericbarthelet
Created April 28, 2022 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredericbarthelet/38c269d66ab0e9149eac562607ef18f1 to your computer and use it in GitHub Desktop.
Save fredericbarthelet/38c269d66ab0e9149eac562607ef18f1 to your computer and use it in GitHub Desktop.
Serverless to CDK bridge with HttpApi extension
import type { AWS } from '@serverless/typescript';
import { App, Stack, Fn } from 'aws-cdk-lib';
// import { CfnApiMapping } from 'aws-cdk-lib/aws-apigatewayv2'
import { Certificate } from 'aws-cdk-lib/aws-certificatemanager'
import { ApiMapping, DomainName, HttpApi, HttpStage } from '@aws-cdk/aws-apigatewayv2-alpha';
const app = new App();
class MyStack extends Stack {
constructor(scope, id) {
super(scope, id);
// new CfnApiMapping(this, 'MyMappingl1', {
// apiId: Fn.ref('HttpApi'),
// domainName: 'toto',
// apiMappingKey: 'tata',
// stage: 'mystage',
// })
const serverlessProvisionnedHttpApi = HttpApi.fromHttpApiAttributes(this, 'MyApi', {
httpApiId: Fn.ref('HttpApi'),
})
// const newHttApi = new HttpApi(this, 'NewAPI');
const dn = new DomainName(this, 'DN', {
domainName: 'exemple.com',
certificate: Certificate.fromCertificateArn(this, 'cert', 'arn:aws:acm:us-east-1:111111111111:certificate'),
});
new ApiMapping(this, 'MyMappingL2', {
api: serverlessProvisionnedHttpApi,
domainName: dn,
stage: HttpStage.fromHttpStageAttributes(this, 'defaultStage', {
api: serverlessProvisionnedHttpApi,
stageName: '$default',
})
})
// new HttpApi(this, 'HttpProxyProdApi', {
// // https://${dn.domainName}/foo goes to prodApi $default stage
// defaultDomainMapping: {
// domainName: dn,
// mappingKey: 'foo',
// },
// });
}
}
const myStack = new MyStack(app, 'MyStack');
import hello from '@functions/hello';
const serverlessConfiguration: AWS = {
service: 'tmp',
frameworkVersion: '2',
plugins: ['serverless-esbuild', 'serverless-lift'],
provider: {
name: 'aws',
runtime: 'nodejs14.x',
region: 'eu-west-1',
apiGateway: {
minimumCompressionSize: 1024,
shouldStartNameWithService: true,
},
environment: {
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
NODE_OPTIONS: '--enable-source-maps --stack-trace-limit=1000',
},
lambdaHashingVersion: '20201221',
},
// import the function via paths
functions: { hello },
package: { individually: true },
resources: app.synth().getStackByName(myStack.stackName).template,
custom: {
esbuild: {
bundle: true,
minify: false,
sourcemap: true,
exclude: ['aws-sdk'],
target: 'node14',
define: { 'require.resolve': undefined },
platform: 'node',
concurrency: 10,
},
},
};
module.exports = serverlessConfiguration;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment