Skip to content

Commit

Permalink
feat(aws-lambda): Generates a version when currentVersionOptions is s…
Browse files Browse the repository at this point in the history
…et (aws#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 aws#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*
  • Loading branch information
marciocadev authored and Brennan Ho committed Jan 20, 2023
1 parent 0e6880d commit 842ec55
Show file tree
Hide file tree
Showing 12 changed files with 610 additions and 13 deletions.
19 changes: 16 additions & 3 deletions packages/@aws-cdk/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ To deploy a `DockerImageFunction` on Lambda `arm64` architecture, specify `Archi
This will bundle docker image assets for `arm64` architecture with `--platform linux/arm64` even if build within an `x86_64` host.

```ts
new DockerImageFunction(this, 'AssetFunction', {
code: DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-arm64-handler')),
architecture: Architecture.ARM_64,
new lambda.DockerImageFunction(this, 'AssetFunction', {
code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-arm64-handler')),
architecture: lambda.Architecture.ARM_64,
});
```

Expand Down Expand Up @@ -283,6 +283,19 @@ const version = new lambda.Version(this, 'MyVersion', {
});
```

Or setting the `currentVersionOptions` when creating a new lambda

```ts
new lambda.Function(this, 'MyVersionedLambda', {
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
currentVersionOptions: {
provisionedConcurrentExecutions: 3,
},
});
```

The major caveat to know here is that a function version must always point to a
specific 'version' of the function. When the function is modified, the version
will continue to point to the 'then version' of the function.
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,9 @@ export class Function extends FunctionBase {
}

this.currentVersionOptions = props.currentVersionOptions;
if (props.currentVersionOptions) {
this.currentVersion;
}

if (props.filesystem) {
if (!props.vpc) {
Expand Down
45 changes: 35 additions & 10 deletions packages/@aws-cdk/aws-lambda/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2871,7 +2871,7 @@ describe('function', () => {

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

test('addFunctionUrl creates a function url with all options', () => {
// GIVEN
// GIVEN
const stack = new cdk.Stack();
const fn = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('hello()'),
Expand Down Expand Up @@ -3045,6 +3045,31 @@ describe('function', () => {
},
});
});

test('Generates a version when currentVersionOptions is set', () => {
const stack = new cdk.Stack();

new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_14_X,
currentVersionOptions: {
provisionedConcurrentExecutions: 3,
},
});

Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Version', {
ProvisionedConcurrencyConfig: {
ProvisionedConcurrentExecutions: 3,
},
});

Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
Code: { ZipFile: 'foo' },
Handler: 'index.handler',
Runtime: 'nodejs14.x',
});
});
});

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

Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
Properties:
{
Code: { ZipFile: 'java11-test-function.zip' },
Handler: 'example.Handler::handleRequest',
Runtime: 'java11',
SnapStart: {
ApplyOn: 'PublishedVersions',
},
},
{
Code: { ZipFile: 'java11-test-function.zip' },
Handler: 'example.Handler::handleRequest',
Runtime: 'java11',
SnapStart: {
ApplyOn: 'PublishedVersions',
},
},
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "22.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
"path": "CurrentVersionOptionsDefaultTestDeployAssertF66C8354.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"22.0.0"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "22.0.0",
"files": {
"a37f787b341d3d57968efec7be372be0c175afbeae09ab1909503e7a7f7dc6b8": {
"source": {
"path": "current-version-options.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "a37f787b341d3d57968efec7be372be0c175afbeae09ab1909503e7a7f7dc6b8.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"Resources": {
"FServiceRole3AC82EE1": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"FC4345940": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "\n exports.handler = async(event) => {\n return \"My versioned lambda\";\n };\n "
},
"Role": {
"Fn::GetAtt": [
"FServiceRole3AC82EE1",
"Arn"
]
},
"Handler": "index.handler",
"Runtime": "nodejs14.x"
},
"DependsOn": [
"FServiceRole3AC82EE1"
]
},
"FCurrentVersion58B8A55Dc23085bc26dff8641d9434d378493303": {
"Type": "AWS::Lambda::Version",
"Properties": {
"FunctionName": {
"Ref": "FC4345940"
},
"ProvisionedConcurrencyConfig": {
"ProvisionedConcurrentExecutions": 3
}
}
}
},
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "22.0.0",
"testCases": {
"CurrentVersionOptions/DefaultTest": {
"stacks": [
"current-version-options"
],
"assertionStack": "CurrentVersionOptions/DefaultTest/DeployAssert",
"assertionStackName": "CurrentVersionOptionsDefaultTestDeployAssertF66C8354"
}
}
}
Loading

0 comments on commit 842ec55

Please sign in to comment.