This is a CDK template to develop lambda functions in your isolated environment
When you are building a lambda function using CDK in a team, your functions get overriden by other developers functions when they deploy. By having isolated CDK environment which imports external resources (e.g. DynamoDB, S3), we can safely develop our lambda functions. Once you confirmed that your function is working properly in your environment, you can copy your code to your base CDK project.
- Set your development Stack info
new DevelopmentTemplateStack(app, `AppStack`, {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
},
tags: {
app: "tag",
},
});
- Add your external resources & lambda function
export class DevelopmentTemplateStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// Reference your resouces
// See how to reference external resources https://docs.aws.amazon.com/cdk/v2/guide/resources.html
const s3Bucket = s3.Bucket.fromBucketArn(this, 'MyBucket', 'arn:aws:s3:::my-bucket-name');
// Set your lambda
cont yourLambda = new lambda_nodejs.NodejsFunction(this, "yourlambda", {
runtime: lambda.Runtime.NODEJS_18_X,
handler: "handler",
entry: path.join(`${__dirname}/../`, "functions", "yourlambda/index.ts"),
environment: {
BUCKET: props.s3Bucket.bucketName,
},
});
props.s3Bucket.grantReadWrite(yourLambda);
}
}
- Your lambda
- This project uses pnpm workspace.
- Add your function under
functions
(e.g.functions/yourlambda/index.ts
) - you need to run
pnpm i
at the root of the project so that pnpm can recognize your function. - If you want add other dependencies, call
pnpm init
underfunctions/yourlambda
thenpnpm add {dependecies you want to add}
- Deploy
pnpm cdk:deploy
You can use submodules to access the lambda layer
git submodule add git@github.com:yourproject/main-cdk.git main-cdk
Then add the path in tsconfig.json
{
...
"paths": {
"/opt/nodejs/s3": ["main-cdk/functions/layers/awsservice/nodejs/s3"]
}
}
and when you update submodule
git submodule update --recursive --remote --merge