aws / aws/aws-cdk

(aws-cdk-lib.aws_lambda): Lambda alias/version not updated with tokenized env var

Open
#25,997 10 comments 7 reactions 1 assignee Claimed by @alvazjor View on GitHub
@aws-cdk/aws-lambda bug effort/large p1
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

### Describe the bug

When using tokenized input as a environment variable value, the Lambda alias is not automatically updated.

### Expected Behavior

A new Lambda version to be created and the Lambda alias updated with the new version.

### Current Behavior

The environment variables on the Lambda configuration was updated, but no new alias/version published.

### Reproduction Steps

```typescript
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';

export class CdkTestStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const hello = new cdk.CfnParameter(this, 'HELLO', {
type: 'String',
}).valueAsString

const fn = new lambda.Function(this, 'HelloFunction', {
runtime: lambda.Runtime.NODEJS_18_X,
handler: "index.handler",
code: lambda.Code.fromInline(`
exports.handler = async function (event) {
console.log("EVENT", event);
};
`),
environment: {
HELLO: hello
},
});

fn.addAlias('live')
}
}
```

```sh
# first
npx cdk deploy --parameters "CdkTest:HELLO=foo"
# second
npx cdk deploy --parameters "CdkTest:HELLO=bar"
```

The second time the Lambda will be updated with the new environment variable under Lambda configuration.
But there won't be a new alias version published.

Same goes for other tokenized input like SSM parameter instead of CfnParameter.

When changing the value a hard coded environment variable like `HELLO: 'foo'` it will publish a new version when changing to `HELLO: 'bar'`.

### Possible Solution

This might not be a bug, but since the documentation for addAlias https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda-readme.html#aliases say:

> When you change your lambda code or configuration, a new resource will be created.

It's a bit unexpected behaviour when the environment variable is changed and the Lambda is updated but a new version is not created.

### Additional Information/Context

There was a similar issue/feature in SAM: https://github.com/aws/serverless-application-model/issues/413

### CDK CLI Version

2.84.0 (build f7c792f)

### Framework Version

_No response_

### Node.js Version

v18.16.0

### OS

MacOS

### Language

Typescript

### Language Version

Typescript 5.0.4

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.