I am working on a cdk deploy script and I have it somewhat working but am lost/not having luck setting the different stages and applying different lambdas to the api resources.
So I have
// Construct lambdas - prod
const lambdaBacklogGet = new lambdajs.NodejsFunction(this, "name", {
nodeModules: ['axios'],
entry: './src/path/index.js',
handler: 'handler',
runtime: lambda.Runtime.NODEJS_12_X,
timeout: cdk.Duration.seconds(20),
role: webformRole
});
// Construct lambdas - dev
const devLambdaBacklogGet = new lambdajs.NodejsFunction(this, "name-dev", {
nodeModules: ['axios'],
entry: './src/path/index.js',
handler: 'handler',
runtime: lambda.Runtime.NODEJS_12_X,
timeout: cdk.Duration.seconds(20),
role: webformRole
});
// then I Construct API
const api = new apiGateway.RestApi(this, "name-api", {
defaultCorsPreflightOptions: {
allowOrigins: apiGateway.Cors.ALL_ORIGINS,
allowHeaders: apiGateway.Cors.DEFAULT_HEADERS,
allowMethods: apiGateway.Cors.ALL_METHODS,
},
description: "API for Something",
deploy: true,
});
// Default response parameters, to allow CORS
const corsResponseParams = {
"method.response.header.Access-Control-Allow-Origin": true,
};
const corsIntegrationResponseParams = {
"method.response.header.Access-Control-Allow-Origin": "'*'",
};
// REST API - /adding prod endpoint
const backlogResourceAPI = api.root.addResource("backlog");
const issuesBacklogGetIntegration = new apiGateway.LambdaIntegration(lambdaBacklogGet, {});
backlogResourceAPI.addMethod("GET", issuesBacklogGetIntegration, {});
//at the end I do this
// Then create an explicit Deployment construct
const deployment = new apiGateway.Deployment(this, 'my_deployment', { api });
// And different stages
const [devStage, testStage, prodStage] = ['dev', 'test'].map(item =>
new apiGateway.Stage(this, `${item}_stage`, { deployment, stageName: item }));
//api.deploymentStage = prodStage
api.deploymentStage = devStage
the last part I know isn't setup correctly. But basically I want one API Gateway and I want the prod/dev stages to have identical resources/structure. However prod stage should pull specific prod lambdas and dev should call dev lambdas.
Is there any way to set that up through the CDK?
Thanks, Tim