|  | 
| 1 | 1 | import * as child_process from 'child_process'; | 
| 2 | 2 | import * as path from 'path'; | 
| 3 |  | -import { Match, Template } from '../../assertions'; | 
|  | 3 | +import { Annotations, Match, Template } from '../../assertions'; | 
| 4 | 4 | import * as ecr from '../../aws-ecr'; | 
|  | 5 | +import * as s3 from '../../aws-s3'; | 
| 5 | 6 | import * as cdk from '../../core'; | 
| 6 | 7 | import * as cxapi from '../../cx-api'; | 
| 7 | 8 | import * as lambda from '../lib'; | 
| @@ -615,6 +616,96 @@ describe('code', () => { | 
| 615 | 616 |       expect(cpMock).toHaveBeenCalledWith('/my/image/path/.', undefined); | 
| 616 | 617 |     }); | 
| 617 | 618 |   }); | 
|  | 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 | +  }); | 
| 618 | 709 | }); | 
| 619 | 710 | 
 | 
| 620 | 711 | function defineFunction(code: lambda.Code, runtime: lambda.Runtime = lambda.Runtime.NODEJS_LATEST) { | 
|  | 
0 commit comments