Skip to content

Commit 65eca47

Browse files
authored
feat(aws-lambda): Generates a version when currentVersionOptions is set (#23225)
## Description Specifying `currentVersionOptions` for a `Function` has no effect unless the `currentVersion` method is called. ## Solution when defining a `currentVersionOptions` a new version will be created automatically, without the need to execute `currentVersion` method. Fixes #23002 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 482dcca commit 65eca47

12 files changed

+610
-13
lines changed

packages/@aws-cdk/aws-lambda/README.md

+16-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ To deploy a `DockerImageFunction` on Lambda `arm64` architecture, specify `Archi
8585
This will bundle docker image assets for `arm64` architecture with `--platform linux/arm64` even if build within an `x86_64` host.
8686

8787
```ts
88-
new DockerImageFunction(this, 'AssetFunction', {
89-
code: DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-arm64-handler')),
90-
architecture: Architecture.ARM_64,
88+
new lambda.DockerImageFunction(this, 'AssetFunction', {
89+
code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-arm64-handler')),
90+
architecture: lambda.Architecture.ARM_64,
9191
});
9292
```
9393

@@ -283,6 +283,19 @@ const version = new lambda.Version(this, 'MyVersion', {
283283
});
284284
```
285285

286+
Or setting the `currentVersionOptions` when creating a new lambda
287+
288+
```ts
289+
new lambda.Function(this, 'MyVersionedLambda', {
290+
runtime: lambda.Runtime.NODEJS_18_X,
291+
handler: 'index.handler',
292+
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
293+
currentVersionOptions: {
294+
provisionedConcurrentExecutions: 3,
295+
},
296+
});
297+
```
298+
286299
The major caveat to know here is that a function version must always point to a
287300
specific 'version' of the function. When the function is modified, the version
288301
will continue to point to the 'then version' of the function.

packages/@aws-cdk/aws-lambda/lib/function.ts

+3
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,9 @@ export class Function extends FunctionBase {
858858
}
859859

860860
this.currentVersionOptions = props.currentVersionOptions;
861+
if (props.currentVersionOptions) {
862+
this.currentVersion;
863+
}
861864

862865
if (props.filesystem) {
863866
if (!props.vpc) {

packages/@aws-cdk/aws-lambda/test/function.test.ts

+35-10
Original file line numberDiff line numberDiff line change
@@ -2871,7 +2871,7 @@ describe('function', () => {
28712871

28722872
describe('FunctionUrl', () => {
28732873
test('addFunctionUrl creates a function url with default options', () => {
2874-
// GIVEN
2874+
// GIVEN
28752875
const stack = new cdk.Stack();
28762876
const fn = new lambda.Function(stack, 'MyLambda', {
28772877
code: new lambda.InlineCode('hello()'),
@@ -2895,7 +2895,7 @@ describe('function', () => {
28952895
});
28962896

28972897
test('addFunctionUrl creates a function url with all options', () => {
2898-
// GIVEN
2898+
// GIVEN
28992899
const stack = new cdk.Stack();
29002900
const fn = new lambda.Function(stack, 'MyLambda', {
29012901
code: new lambda.InlineCode('hello()'),
@@ -3045,6 +3045,31 @@ describe('function', () => {
30453045
},
30463046
});
30473047
});
3048+
3049+
test('Generates a version when currentVersionOptions is set', () => {
3050+
const stack = new cdk.Stack();
3051+
3052+
new lambda.Function(stack, 'MyLambda', {
3053+
code: new lambda.InlineCode('foo'),
3054+
handler: 'index.handler',
3055+
runtime: lambda.Runtime.NODEJS_14_X,
3056+
currentVersionOptions: {
3057+
provisionedConcurrentExecutions: 3,
3058+
},
3059+
});
3060+
3061+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Version', {
3062+
ProvisionedConcurrencyConfig: {
3063+
ProvisionedConcurrentExecutions: 3,
3064+
},
3065+
});
3066+
3067+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
3068+
Code: { ZipFile: 'foo' },
3069+
Handler: 'index.handler',
3070+
Runtime: 'nodejs14.x',
3071+
});
3072+
});
30483073
});
30493074

30503075
test('throws if ephemeral storage size is out of bound', () => {
@@ -3141,14 +3166,14 @@ test('set SnapStart to desired value', () => {
31413166

31423167
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
31433168
Properties:
3144-
{
3145-
Code: { ZipFile: 'java11-test-function.zip' },
3146-
Handler: 'example.Handler::handleRequest',
3147-
Runtime: 'java11',
3148-
SnapStart: {
3149-
ApplyOn: 'PublishedVersions',
3150-
},
3151-
},
3169+
{
3170+
Code: { ZipFile: 'java11-test-function.zip' },
3171+
Handler: 'example.Handler::handleRequest',
3172+
Runtime: 'java11',
3173+
SnapStart: {
3174+
ApplyOn: 'PublishedVersions',
3175+
},
3176+
},
31523177
});
31533178
});
31543179

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "22.0.0",
3+
"files": {
4+
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
5+
"source": {
6+
"path": "CurrentVersionOptionsDefaultTestDeployAssertF66C8354.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Parameters": {
3+
"BootstrapVersion": {
4+
"Type": "AWS::SSM::Parameter::Value<String>",
5+
"Default": "/cdk-bootstrap/hnb659fds/version",
6+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
7+
}
8+
},
9+
"Rules": {
10+
"CheckBootstrapVersion": {
11+
"Assertions": [
12+
{
13+
"Assert": {
14+
"Fn::Not": [
15+
{
16+
"Fn::Contains": [
17+
[
18+
"1",
19+
"2",
20+
"3",
21+
"4",
22+
"5"
23+
],
24+
{
25+
"Ref": "BootstrapVersion"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
32+
}
33+
]
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"22.0.0"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "22.0.0",
3+
"files": {
4+
"a37f787b341d3d57968efec7be372be0c175afbeae09ab1909503e7a7f7dc6b8": {
5+
"source": {
6+
"path": "current-version-options.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "a37f787b341d3d57968efec7be372be0c175afbeae09ab1909503e7a7f7dc6b8.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"Resources": {
3+
"FServiceRole3AC82EE1": {
4+
"Type": "AWS::IAM::Role",
5+
"Properties": {
6+
"AssumeRolePolicyDocument": {
7+
"Statement": [
8+
{
9+
"Action": "sts:AssumeRole",
10+
"Effect": "Allow",
11+
"Principal": {
12+
"Service": "lambda.amazonaws.com"
13+
}
14+
}
15+
],
16+
"Version": "2012-10-17"
17+
},
18+
"ManagedPolicyArns": [
19+
{
20+
"Fn::Join": [
21+
"",
22+
[
23+
"arn:",
24+
{
25+
"Ref": "AWS::Partition"
26+
},
27+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
28+
]
29+
]
30+
}
31+
]
32+
}
33+
},
34+
"FC4345940": {
35+
"Type": "AWS::Lambda::Function",
36+
"Properties": {
37+
"Code": {
38+
"ZipFile": "\n exports.handler = async(event) => {\n return \"My versioned lambda\";\n };\n "
39+
},
40+
"Role": {
41+
"Fn::GetAtt": [
42+
"FServiceRole3AC82EE1",
43+
"Arn"
44+
]
45+
},
46+
"Handler": "index.handler",
47+
"Runtime": "nodejs14.x"
48+
},
49+
"DependsOn": [
50+
"FServiceRole3AC82EE1"
51+
]
52+
},
53+
"FCurrentVersion58B8A55Dc23085bc26dff8641d9434d378493303": {
54+
"Type": "AWS::Lambda::Version",
55+
"Properties": {
56+
"FunctionName": {
57+
"Ref": "FC4345940"
58+
},
59+
"ProvisionedConcurrencyConfig": {
60+
"ProvisionedConcurrentExecutions": 3
61+
}
62+
}
63+
}
64+
},
65+
"Parameters": {
66+
"BootstrapVersion": {
67+
"Type": "AWS::SSM::Parameter::Value<String>",
68+
"Default": "/cdk-bootstrap/hnb659fds/version",
69+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
70+
}
71+
},
72+
"Rules": {
73+
"CheckBootstrapVersion": {
74+
"Assertions": [
75+
{
76+
"Assert": {
77+
"Fn::Not": [
78+
{
79+
"Fn::Contains": [
80+
[
81+
"1",
82+
"2",
83+
"3",
84+
"4",
85+
"5"
86+
],
87+
{
88+
"Ref": "BootstrapVersion"
89+
}
90+
]
91+
}
92+
]
93+
},
94+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
95+
}
96+
]
97+
}
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "22.0.0",
3+
"testCases": {
4+
"CurrentVersionOptions/DefaultTest": {
5+
"stacks": [
6+
"current-version-options"
7+
],
8+
"assertionStack": "CurrentVersionOptions/DefaultTest/DeployAssert",
9+
"assertionStackName": "CurrentVersionOptionsDefaultTestDeployAssertF66C8354"
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)