I'm starting a small project with AWS CDK. I have created a bucket with CDK.
this.myBucket = new s3.Bucket(this,'my-first-bucket',{
removalPolicy: cdk.RemovalPolicy.DESTROY
});
When I deploy this using cdk deploy
, CloudFormation creates a bucket using a generated bucket name, like mystack-myfirstbucket786ee605-1q912cjgtpj27
.
Now, I start writing a lambda function, whose job will entail writing to this bucket. I pass the bucketName to the lambda by adding it to the lambda's environment variable.
this.myLambdaFunction = new lambda.Function(this, "my-function",{
code: lambda.Code.fromAsset('lambda'),
handler: 'myFunction.handler',
runtime: lambda.Runtime.NODEJS_14_X,
memorySize: 256,
timeout: cdk.Duration.seconds(60)
});
this.myLambdaFunction.addEnvironment('bucket', this.myBucket.bucketName );
When I run the lambda locally, as I iterate on developing it, if I inspect the process.env.bucket
in my lambda, I see that it has a different generated bucket name i.e.myfirstbucket786ee605
that is different that the bucket that CloudFormation created. This prevents my lambda from writing to that bucket while I develop locally.
How am I supposed to maintain the CloudFormation state in between my local and AWS environment as I develop?