Skip to content

Commit 8a6b376

Browse files
authored
feat(lambda): lambda code assets are marked as deploy time assets (#25705)
Mark lambda assets as deploy time assets. This has no functionality for default synthesis, but if used in conjunction with the `AppStagingSynthesizer`, lambda assets will be marked with the `deploy-time/` prefix in the staging bucket and subject to lifecycle policies. This is because for lambda code, the s3 staging bucket is an intermediate step and the code is copied to lambda immediately. The code in s3 is no longer referenced after deploy, except when in a rollback scenario. Since this is a no-op for normal synthesis, the only test that can be done is in `app-staging-synthesizer-alpha`. Also I've deleted a duplicate test I found in `app-staging-synthesizer-alpha`. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent bdfdd40 commit 8a6b376

File tree

6 files changed

+41
-27
lines changed

6 files changed

+41
-27
lines changed

packages/@aws-cdk/app-staging-synthesizer-alpha/test/app-staging-synthesizer.test.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as fs from 'fs';
2+
import * as path from 'path';
23
import { App, Stack, CfnResource, FileAssetPackaging, Token, Lazy, Duration } from 'aws-cdk-lib';
34
import { Match, Template } from 'aws-cdk-lib/assertions';
5+
import * as lambda from 'aws-cdk-lib/aws-lambda';
46
import * as cxschema from 'aws-cdk-lib/cloud-assembly-schema';
57
import { CloudAssembly } from 'aws-cdk-lib/cx-api';
68
import { evaluateCFN } from './evaluate-cfn';
@@ -178,32 +180,43 @@ describe(AppStagingSynthesizer, () => {
178180
deployTime: true,
179181
});
180182

181-
// THEN - asset has bucket prefix
183+
// THEN - asset has deploy time prefix
182184
expect(evalCFN(location.objectKey)).toEqual(`${DEPLOY_TIME_PREFIX}abcdef.js`);
183185
});
184186

185-
test('do not get specified bucketPrefix', () => {
186-
// GIVEN
187-
app = new App({
188-
defaultStackSynthesizer: AppStagingSynthesizer.defaultResources({ appId: APP_ID }),
189-
});
190-
stack = new Stack(app, 'Stack', {
191-
env: {
192-
account: '000000000000',
193-
region: 'us-west-2',
194-
},
195-
});
196-
187+
test('lambda assets are by default deploy time assets', () => {
197188
// WHEN
198-
const location = stack.synthesizer.addFileAsset({
199-
fileName: __filename,
200-
packaging: FileAssetPackaging.FILE,
201-
sourceHash: 'abcdef',
202-
deployTime: true,
189+
new lambda.Function(stack, 'Lambda', {
190+
handler: 'index.handler',
191+
code: lambda.Code.fromAsset(path.join(__dirname, 'assets')),
192+
runtime: lambda.Runtime.PYTHON_3_10,
203193
});
204194

205-
// THEN - asset has bucket prefix
206-
expect(evalCFN(location.objectKey)).toEqual(`${DEPLOY_TIME_PREFIX}abcdef.js`);
195+
// THEN - lambda asset has deploy time prefix
196+
const asm = app.synth();
197+
198+
const manifestArtifact = asm.artifacts.filter(isAssetManifest)[0];
199+
expect(manifestArtifact).toBeDefined();
200+
const manifest: cxschema.AssetManifest = JSON.parse(fs.readFileSync(manifestArtifact.file, { encoding: 'utf-8' }));
201+
202+
expect(manifest.files).toBeDefined();
203+
expect(Object.keys(manifest.files!).length).toEqual(2);
204+
const firstFile = manifest.files![Object.keys(manifest.files!)[0]];
205+
const assetHash = '68539effc3f7ad46fff9765606c2a01b7f7965833643ab37e62799f19a37f650';
206+
expect(firstFile).toEqual({
207+
source: {
208+
packaging: 'zip',
209+
path: `asset.${assetHash}`,
210+
},
211+
destinations: {
212+
'000000000000-us-east-1': {
213+
bucketName: `cdk-${APP_ID}-staging-000000000000-us-east-1`,
214+
objectKey: `${DEPLOY_TIME_PREFIX}${assetHash}.zip`,
215+
region: 'us-east-1',
216+
assumeRoleArn: `arn:\${AWS::Partition}:iam::000000000000:role/cdk-${APP_ID}-file-role-us-east-1`,
217+
},
218+
},
219+
});
207220
});
208221

209222
test('have s3 bucket has lifecycle rule by default', () => {

packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"additionalDependencies": [
2121
"synthesize-default-resources.assets"
2222
],
23-
"stackTemplateAssetObjectUrl": "s3://cdk-default-resources-staging-${AWS::AccountId}-${AWS::Region}/deploy-time/e21d11bec65be920861a56a86066cc88a0241d5cbe8324d0692ca982420e4cb0.json",
23+
"stackTemplateAssetObjectUrl": "s3://cdk-default-resources-staging-${AWS::AccountId}-${AWS::Region}/deploy-time/dc7275f639c45accfa2abc4842978bcb3b0c5f0b83fcde22015e344b2e008f26.json",
2424
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
2525
"lookupRole": {
2626
"arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}"

packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/synthesize-default-resources.assets.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-default-resources-staging-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "68539effc3f7ad46fff9765606c2a01b7f7965833643ab37e62799f19a37f650.zip",
12+
"objectKey": "deploy-time/68539effc3f7ad46fff9765606c2a01b7f7965833643ab37e62799f19a37f650.zip",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-default-resources-file-role-${AWS::Region}"
1414
}
1515
}
1616
},
17-
"e21d11bec65be920861a56a86066cc88a0241d5cbe8324d0692ca982420e4cb0": {
17+
"dc7275f639c45accfa2abc4842978bcb3b0c5f0b83fcde22015e344b2e008f26": {
1818
"source": {
1919
"path": "synthesize-default-resources.template.json",
2020
"packaging": "file"
2121
},
2222
"destinations": {
2323
"current_account-current_region": {
2424
"bucketName": "cdk-default-resources-staging-${AWS::AccountId}-${AWS::Region}",
25-
"objectKey": "deploy-time/e21d11bec65be920861a56a86066cc88a0241d5cbe8324d0692ca982420e4cb0.json",
25+
"objectKey": "deploy-time/dc7275f639c45accfa2abc4842978bcb3b0c5f0b83fcde22015e344b2e008f26.json",
2626
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-default-resources-file-role-${AWS::Region}"
2727
}
2828
}

packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/synthesize-default-resources.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"S3Bucket": {
3939
"Fn::Sub": "cdk-default-resources-staging-${AWS::AccountId}-${AWS::Region}"
4040
},
41-
"S3Key": "68539effc3f7ad46fff9765606c2a01b7f7965833643ab37e62799f19a37f650.zip"
41+
"S3Key": "deploy-time/68539effc3f7ad46fff9765606c2a01b7f7965833643ab37e62799f19a37f650.zip"
4242
},
4343
"Role": {
4444
"Fn::GetAtt": [

packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/tree.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"s3Bucket": {
106106
"Fn::Sub": "cdk-default-resources-staging-${AWS::AccountId}-${AWS::Region}"
107107
},
108-
"s3Key": "68539effc3f7ad46fff9765606c2a01b7f7965833643ab37e62799f19a37f650.zip"
108+
"s3Key": "deploy-time/68539effc3f7ad46fff9765606c2a01b7f7965833643ab37e62799f19a37f650.zip"
109109
},
110110
"role": {
111111
"Fn::GetAtt": [
@@ -1150,7 +1150,7 @@
11501150
}
11511151
},
11521152
"constructInfo": {
1153-
"fqn": "@aws-cdk/app-staging-synthesizer-alpha.DefaultStagingStack",
1153+
"fqn": "aws-cdk-lib.Stack",
11541154
"version": "0.0.0"
11551155
}
11561156
},

packages/aws-cdk-lib/aws-lambda/lib/code.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ export class AssetCode extends Code {
277277
if (!this.asset) {
278278
this.asset = new s3_assets.Asset(scope, 'Code', {
279279
path: this.path,
280+
deployTime: true,
280281
...this.options,
281282
});
282283
} else if (cdk.Stack.of(this.asset) !== cdk.Stack.of(scope)) {

0 commit comments

Comments
 (0)