Skip to content

Commit 6884652

Browse files
committed
docs(aws-lambda): Warn users on using fromBucket without setting objectVersion
Fixes #6176
1 parent 1885062 commit 6884652

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,45 @@ import { UnscopedValidationError, ValidationError } from '../../core/lib/errors'
1515
export abstract class Code {
1616
/**
1717
* Lambda handler code as an S3 object.
18+
*
19+
* Note: If `objectVersion` is not defined, the lambda will not be updated automatically if the code in the bucket is updated.
20+
* This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion.
1821
* @param bucket The S3 bucket
1922
* @param key The object key
2023
* @param objectVersion Optional S3 object version
2124
*/
2225
public static fromBucket(bucket: s3.IBucket, key: string, objectVersion?: string): S3Code {
26+
if (objectVersion === undefined) {
27+
cdk.Annotations.of(bucket).addWarningV2(
28+
'@aws-cdk/aws-lambda:codeFromBucketObjectVersionNotSpecified',
29+
'objectVersion is not defined for S3Code.fromBucket(). The lambda will not be updated automatically if the code in the bucket is updated. ' +
30+
'This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion.',
31+
);
32+
}
33+
2334
return new S3Code(bucket, key, objectVersion);
2435
}
2536

2637
/**
2738
* Lambda handler code as an S3 object.
39+
*
40+
* Note: If `options.objectVersion` is not defined, the lambda will not be updated automatically if the code in the bucket is updated.
41+
* This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion.
2842
* @param bucket The S3 bucket
2943
* @param key The object key
3044
* @param options Optional parameters for setting the code, current optional parameters to set here are
3145
* 1. `objectVersion` to set S3 object version
3246
* 2. `sourceKMSKey` to set KMS Key for encryption of code
3347
*/
3448
public static fromBucketV2 (bucket: s3.IBucket, key: string, options?: BucketOptions): S3CodeV2 {
49+
if (options?.objectVersion === undefined) {
50+
cdk.Annotations.of(bucket).addWarningV2(
51+
'@aws-cdk/aws-lambda:codeFromBucketObjectVersionNotSpecified',
52+
'options.objectVersion is not defined for S3Code.fromBucketV2(). The lambda will not be updated automatically if the code in the bucket is updated. ' +
53+
'This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set options.objectVersion.',
54+
);
55+
}
56+
3557
return new S3CodeV2(bucket, key, options);
3658
}
3759

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

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as child_process from 'child_process';
22
import * as path from 'path';
3-
import { Match, Template } from '../../assertions';
3+
import { Annotations, Match, Template } from '../../assertions';
44
import * as ecr from '../../aws-ecr';
5+
import * as s3 from '../../aws-s3';
56
import * as cdk from '../../core';
67
import * as cxapi from '../../cx-api';
78
import * as lambda from '../lib';
@@ -615,6 +616,96 @@ describe('code', () => {
615616
expect(cpMock).toHaveBeenCalledWith('/my/image/path/.', undefined);
616617
});
617618
});
619+
620+
describe('lambda.Code.fromBucket', () => {
621+
test('fromBucket warns when no objectVersion is set', () => {
622+
// given
623+
const app = new cdk.App();
624+
const stack = new cdk.Stack(app, 'Stack');
625+
const bucket = new s3.Bucket(stack, 'Bucket');
626+
627+
// when
628+
new lambda.Function(stack, 'Fn', {
629+
code: lambda.Code.fromBucket(bucket, 'Object'),
630+
handler: 'index.handler',
631+
runtime: lambda.Runtime.NODEJS_LATEST,
632+
});
633+
634+
// then
635+
Annotations.fromStack(stack).hasWarning(
636+
'/Stack/Bucket',
637+
'objectVersion is not defined for S3Code.fromBucket(). The lambda will not be updated automatically if the code in the bucket is updated. ' +
638+
'This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion. ' +
639+
'[ack: @aws-cdk/aws-lambda:codeFromBucketObjectVersionNotSpecified]',
640+
);
641+
});
642+
643+
test('fromBucket does not warn when an objectVersion is set', () => {
644+
// given
645+
const app = new cdk.App();
646+
const stack = new cdk.Stack(app, 'Stack');
647+
const bucket = new s3.Bucket(stack, 'Bucket');
648+
649+
// when
650+
new lambda.Function(stack, 'Fn', {
651+
code: lambda.Code.fromBucket(bucket, 'Object', 'v1'),
652+
handler: 'index.handler',
653+
runtime: lambda.Runtime.NODEJS_LATEST,
654+
});
655+
656+
// then
657+
Annotations.fromStack(stack).hasNoWarning(
658+
'/Stack/Bucket',
659+
'objectVersion is not defined for S3Code.fromBucket(). The lambda will not be updated automatically if the code in the bucket is updated. ' +
660+
'This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion. ' +
661+
'[ack: @aws-cdk/aws-lambda:codeFromBucketObjectVersionNotSpecified]'
662+
);
663+
});
664+
665+
test('fromBucketV2 warns when no objectVersion is set', () => {
666+
// given
667+
const app = new cdk.App();
668+
const stack = new cdk.Stack(app, 'Stack');
669+
const bucket = new s3.Bucket(stack, 'Bucket');
670+
671+
// when
672+
new lambda.Function(stack, 'Fn', {
673+
code: lambda.Code.fromBucketV2(bucket, 'Object'),
674+
handler: 'index.handler',
675+
runtime: lambda.Runtime.NODEJS_LATEST,
676+
});
677+
678+
// then
679+
Annotations.fromStack(stack).hasWarning(
680+
'/Stack/Bucket',
681+
'options.objectVersion is not defined for S3Code.fromBucketV2(). The lambda will not be updated automatically if the code in the bucket is updated. ' +
682+
'This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set options.objectVersion. ' +
683+
'[ack: @aws-cdk/aws-lambda:codeFromBucketObjectVersionNotSpecified]',
684+
);
685+
});
686+
687+
test('fromBucketV2 does not warn when an objectVersion is set', () => {
688+
// given
689+
const app = new cdk.App();
690+
const stack = new cdk.Stack(app, 'Stack');
691+
const bucket = new s3.Bucket(stack, 'Bucket');
692+
693+
// when
694+
new lambda.Function(stack, 'Fn', {
695+
code: lambda.Code.fromBucketV2(bucket, 'Object', { objectVersion: 'v1' }),
696+
handler: 'index.handler',
697+
runtime: lambda.Runtime.NODEJS_LATEST,
698+
});
699+
700+
// then
701+
Annotations.fromStack(stack).hasNoWarning(
702+
'/Stack/Bucket',
703+
'options.objectVersion is not defined for S3Code.fromBucketV2(). The lambda will not be updated automatically if the code in the bucket is updated. ' +
704+
'This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set options.objectVersion. ' +
705+
'[ack: @aws-cdk/aws-lambda:codeFromBucketObjectVersionNotSpecified]',
706+
);
707+
});
708+
});
618709
});
619710

620711
function defineFunction(code: lambda.Code, runtime: lambda.Runtime = lambda.Runtime.NODEJS_LATEST) {

0 commit comments

Comments
 (0)