Ajay Wadhara
Ajay Wadhara

Reputation: 21

CDK/Cloudformation - Unable to deploy stack because of Lambda Version deleted manually

I have one CDK stack with Lambda, its versions and Alias pointing to the latest version. In my CDK code, I am following this approach:

  1. Create Lambda
  2. Create a Version
  3. Create an Alias pointing to the version.

With time, I had too many versions published, so I deleted the initial versions because of Lambda Storage limits. But the physical id of the initial version is still referenced in the CloudFormation Stack. Cloudformation stack creates resource success when tried the first time

I don't have version 7 as seen in the above screenshot. Now, I am on version 22 Current version of my Lambda

When I go and deploy my code, I get the below error because version 7 of Lambda is not there anymore

Error that I get while updating stack

Any idea, how can I fix this without deleting the Lambda and creating this again?

My CDK Code:

final Function function = Function.Builder.create(construct, InfraConstants.WEBHOOK_RECEIVER_LAMBDA)
        .runtime(Runtime.JAVA_11)
        .handler("com.org.test")
        .memorySize(1024)
        .timeout(Duration.seconds(100))
        .functionName(InfraConstants.WEBHOOK_RECEIVER_LAMBDA)
        .code(Code.fromAsset("src/main/resources/sample-java.jar"))
        .tracing(Tracing.PASS_THROUGH)
        .layers(layers)
        .environment(LambdaUtils.getEnvVariablesWebhookReceiver())
        .build();
LambdaUtils.enableSnapStart(function);
return function;
public Version createVersion(Construct construct, String versionId, IFunction lambdaFunction) {
    return Version.Builder.create(construct, versionId)
            .lambda(lambdaFunction)
            .build();
}

public Alias createAlias(Construct construct, String aliasId, Version version, String alias){
    return Alias.Builder.create(construct, aliasId)
            .aliasName(alias)
            .version(version)
            .build();
}

Upvotes: 2

Views: 1163

Answers (3)

The_Real_Epk
The_Real_Epk

Reputation: 181

One way to avoid this is to ensure that the logical 'id' of the Version is updated when you want to publish a new one. When you delete old versions then it will no longer be referenced in CloudFormation.

If you compute the hash of the .java/.pom files inside of the maven project containing the lambda code and use that as an ID, it will publish a new version each time you make changes to your lambda code.

Upvotes: 1

Palash Hariyani
Palash Hariyani

Reputation: 1

Another way to redeploy and fix the issue would be to change any environment variable in the CDK package which will re-trigger the deployment.

Upvotes: 0

alessiosavi
alessiosavi

Reputation: 3037

The simplest way of redeploy the lambda is change the CloudFormation ID of the resources.

Upvotes: 2

Related Questions