Issue
Can please anyone help me to check if a zip object exists in S3 in order to use it while creating a lambda with CDK?
Problem
This is my proposal code in CDK, and I want to add the validation running the checkZipIfExistInS3 function. I'm guessing that function should be async and also use SDK to retrieve S3 data and do the validation. But I couldn't find a way to await an async functions inside CDK construct.
class myLambdaConstruct extends Construct {
public readonly lambdaFunction: lambda.Function;
constructor(scope: Construct, id: string, opts: MyOptions) {
super(scope, id);
let code = await checkZipIfExistInS3()
? lambda.Code.fromBucket(importedBucket, opts.bucketKey)
: lambda.Code.fromBucket(importedBucket, 'initial.zip')
const lambdaFunctionProps: lambda.FunctionProps = {
functionName: opts.functionName,
architecture: Architecture.X86_64,
runtime: lambda.Runtime.DOTNET_6,
handler: opts.handler,
code: code,
tracing: lambda.Tracing.ACTIVE,
deadLetterQueueEnabled: true,
timeout: opts.timeout,
memorySize: opts.memorySize,
logRetention: opts.logRetention,
environment: opts.environment,
description: opts.description,
reservedConcurrentExecutions: opts.reservedConcurrentExecutions,
};
this.lambdaFunction = new lambda.Function(this, id, lambdaFunctionProps);
}
}
This validation is intended to solve a deadlock generated the first time when I try to deploy the whole infrastructure in AWS, this is, the CDK should create the lambda using .zip artifact from S3, which does not exist (so here lambda creation fails). In order to upload that .zip, the lambda pipeline must run prior but also will fail because that pipeline will only update the code, not create the lambda function (the lambda function doesn't still exist), CDK should create it, but again, CDK needs the .zip code, and so on.
I'm able to solve that deadlock by running both pipelines (lambda and CDK pipeline) a couple of times and accepting they fail, but need to get a better solution, especially for a Disaster Recovery scenario.
Finally, I'm open to changing the way currently deploying the infra or don't use an async validation function at all. I'll appreciate any answer.
Additional information
- S3 bucket is created manually and the
initial.zipfile is also uploaded at that time. - I have two repos, one for CDK infrastructure and one for lambda code itself, each repo has its own pipeline and S3 bucket in the middle used as an artifactory.
- Lambda pipeline only uploads the built zip file to S3.
- CDK pipeline or CDK code itself deploy the infra, create a lambda, and also updates its code using the latest zip code in S3.
Solution
You can't run await the SDK call in your stack. If you would like to use the results of your SDK call in your stack you can do the following:
1: Create an async function in the lib outside of the stack. 2: Call this async function in the bin to inject the results into your stack as props. 3: Use the results in your stack as props.
Answered By - Faruk Ada
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.