diff --git a/packages/@aws-cdk/aws-lambda/lib/code.ts b/packages/@aws-cdk/aws-lambda/lib/code.ts index e8348d9da3c36..9b7c9e3b1f5f8 100644 --- a/packages/@aws-cdk/aws-lambda/lib/code.ts +++ b/packages/@aws-cdk/aws-lambda/lib/code.ts @@ -54,6 +54,15 @@ export abstract class Code { return new AssetCode(path, options); } + /** + * Loads the function code from an Asset construct. + * + * @param asset An Asset construct containing the Lambda code bundle or a .zip file + */ + public static fromAssetConstruct(asset: s3_assets.Asset): AssetConstructCode { + return new AssetConstructCode(asset); + } + /** * Loads the function code from an asset created by a Docker build. * @@ -317,6 +326,32 @@ export interface ResourceBindOptions { readonly resourceProperty?: string; } +/** + * Lambda code from an Asset. + */ +export class AssetConstructCode extends Code { + public readonly isInline = false; + /** + * @param asset An Asset construct containing the Lambda code bundle or a .zip file. + */ + constructor(private readonly asset: s3_assets.Asset) { + super(); + } + + public bind(_scope: Construct): CodeConfig { + if (!this.asset.isZipArchive) { + throw new Error(`Asset must be a .zip file or a directory ${this.asset.assetPath}`); + } + + return { + s3Location: { + bucketName: this.asset.s3BucketName, + objectKey: this.asset.s3ObjectKey, + }, + }; + } +} + /** * Construction properties for {@link CfnParametersCode}. */ diff --git a/packages/@aws-cdk/aws-lambda/test/code.test.ts b/packages/@aws-cdk/aws-lambda/test/code.test.ts index ed7767e488045..b6c593b3a92b3 100644 --- a/packages/@aws-cdk/aws-lambda/test/code.test.ts +++ b/packages/@aws-cdk/aws-lambda/test/code.test.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import { Match, Template } from '@aws-cdk/assertions'; import * as ecr from '@aws-cdk/aws-ecr'; +import * as s3_assets from '@aws-cdk/aws-s3-assets'; import { testLegacyBehavior } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import * as cxapi from '@aws-cdk/cx-api'; @@ -101,6 +102,53 @@ describe('code', () => { }); }); + describe('lambda.Code.fromAssetConstruct', () => { + test('fails if a non-zip asset is used', () => { + // GIVEN + const stack = new cdk.Stack(); + const fileAsset = new s3_assets.Asset(stack, 'CodeAsset', { + path: path.join(__dirname, 'my-lambda-handler', 'index.py'), + }); + const code = lambda.Code.fromAssetConstruct(fileAsset); + + // THEN + expect(() => defineFunction(code)).toThrow(/Asset must be a \.zip file or a directory/); + }); + + test('able to re-use one Asset object in multiple functions', () => { + // GIVEN + const app = new cdk.App({ + context: { + [cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT]: false, + }, + }); + const stack = new cdk.Stack(app, 'MyStack'); + const directoryAsset = new s3_assets.Asset(stack, 'CodeAsset', { + path: path.join(__dirname, 'my-lambda-handler'), + }); + const code = lambda.Code.fromAssetConstruct(directoryAsset); + + // WHEN + new lambda.Function(stack, 'Func1', { + handler: 'foom', + runtime: lambda.Runtime.NODEJS_14_X, + code, + }); + + new lambda.Function(stack, 'Func2', { + handler: 'foom', + runtime: lambda.Runtime.NODEJS_14_X, + code, + }); + + // THEN + const assembly = app.synth(); + const synthesized = assembly.stacks[0]; + + expect(synthesized.assets.length).toEqual(1); + }); + }); + describe('lambda.Code.fromCfnParameters', () => { test("automatically creates the Bucket and Key parameters when it's used in a Function", () => { const stack = new cdk.Stack(); @@ -513,9 +561,8 @@ describe('code', () => { }); }); -function defineFunction(code: lambda.Code, runtime: lambda.Runtime = lambda.Runtime.NODEJS_14_X) { - const stack = new cdk.Stack(); - return new lambda.Function(stack, 'Func', { +function defineFunction(code: lambda.Code, runtime: lambda.Runtime = lambda.Runtime.NODEJS_14_X, stack?: cdk.Stack) { + return new lambda.Function(stack ?? new cdk.Stack(), 'Func', { handler: 'foom', code, runtime, diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/asset.a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/asset.a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip new file mode 100644 index 0000000000000..eae53ec32da7a Binary files /dev/null and b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/asset.a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip differ diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/cdk.out b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/cdk.out new file mode 100644 index 0000000000000..8ecc185e9dbee --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"21.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/integ.json b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/integ.json new file mode 100644 index 0000000000000..92020386ce976 --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "21.0.0", + "testCases": { + "lambda-test-from-asset-construct/DefaultTest": { + "stacks": [ + "lambda-test-from-asset-construct-stack" + ], + "assertionStack": "lambda-test-from-asset-construct/DefaultTest/DeployAssert", + "assertionStackName": "lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-assets-from-construct.assets.json b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-assets-from-construct.assets.json new file mode 100644 index 0000000000000..444e74d35bf2d --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-assets-from-construct.assets.json @@ -0,0 +1,32 @@ +{ + "version": "21.0.0", + "files": { + "a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f": { + "source": { + "path": "asset.a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + }, + "e363111b44d82e0bfe0be44fe813f2dd0883c834a1652d558de090ae83e22857": { + "source": { + "path": "lambda-test-assets-from-construct.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "e363111b44d82e0bfe0be44fe813f2dd0883c834a1652d558de090ae83e22857.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-assets-from-construct.template.json b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-assets-from-construct.template.json new file mode 100644 index 0000000000000..163e09b1a4ea2 --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-assets-from-construct.template.json @@ -0,0 +1,91 @@ +{ + "Resources": { + "MyLambdaServiceRole4539ECB6": { + "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" + ] + ] + } + ] + } + }, + "MyLambdaCCE802FB": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": { + "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" + }, + "S3Key": "a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MyLambdaServiceRole4539ECB6", + "Arn" + ] + }, + "Handler": "index.main", + "Runtime": "python3.9" + }, + "DependsOn": [ + "MyLambdaServiceRole4539ECB6" + ] + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "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." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-from-asset-construct-stack.assets.json b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-from-asset-construct-stack.assets.json new file mode 100644 index 0000000000000..5f4c22567a9cb --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-from-asset-construct-stack.assets.json @@ -0,0 +1,32 @@ +{ + "version": "21.0.0", + "files": { + "a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f": { + "source": { + "path": "asset.a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + }, + "e363111b44d82e0bfe0be44fe813f2dd0883c834a1652d558de090ae83e22857": { + "source": { + "path": "lambda-test-from-asset-construct-stack.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "e363111b44d82e0bfe0be44fe813f2dd0883c834a1652d558de090ae83e22857.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-from-asset-construct-stack.template.json b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-from-asset-construct-stack.template.json new file mode 100644 index 0000000000000..163e09b1a4ea2 --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambda-test-from-asset-construct-stack.template.json @@ -0,0 +1,91 @@ +{ + "Resources": { + "MyLambdaServiceRole4539ECB6": { + "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" + ] + ] + } + ] + } + }, + "MyLambdaCCE802FB": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": { + "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" + }, + "S3Key": "a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MyLambdaServiceRole4539ECB6", + "Arn" + ] + }, + "Handler": "index.main", + "Runtime": "python3.9" + }, + "DependsOn": [ + "MyLambdaServiceRole4539ECB6" + ] + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "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." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.assets.json b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.assets.json new file mode 100644 index 0000000000000..74cb0c8eddfae --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.assets.json @@ -0,0 +1,19 @@ +{ + "version": "21.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.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": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.template.json b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "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." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/manifest.json b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/manifest.json new file mode 100644 index 0000000000000..b30e836496d24 --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/manifest.json @@ -0,0 +1,117 @@ +{ + "version": "21.0.0", + "artifacts": { + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + }, + "lambda-test-from-asset-construct-stack.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "lambda-test-from-asset-construct-stack.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "lambda-test-from-asset-construct-stack": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "lambda-test-from-asset-construct-stack.template.json", + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/e363111b44d82e0bfe0be44fe813f2dd0883c834a1652d558de090ae83e22857.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "lambda-test-from-asset-construct-stack.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "lambda-test-from-asset-construct-stack.assets" + ], + "metadata": { + "/lambda-test-from-asset-construct-stack/MyLambda/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "MyLambdaServiceRole4539ECB6" + } + ], + "/lambda-test-from-asset-construct-stack/MyLambda/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "MyLambdaCCE802FB" + } + ], + "/lambda-test-from-asset-construct-stack/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/lambda-test-from-asset-construct-stack/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "lambda-test-from-asset-construct-stack" + }, + "lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.template.json", + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "lambdatestfromassetconstructDefaultTestDeployAssertF70D74A5.assets" + ], + "metadata": { + "/lambda-test-from-asset-construct/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/lambda-test-from-asset-construct/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "lambda-test-from-asset-construct/DefaultTest/DeployAssert" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/tree.json b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/tree.json new file mode 100644 index 0000000000000..0b6d27442c49c --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/from-asset-construct.integ.snapshot/tree.json @@ -0,0 +1,179 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.92" + } + }, + "lambda-test-from-asset-construct-stack": { + "id": "lambda-test-from-asset-construct-stack", + "path": "lambda-test-from-asset-construct-stack", + "children": { + "AssetCode": { + "id": "AssetCode", + "path": "lambda-test-from-asset-construct-stack/AssetCode", + "children": { + "Stage": { + "id": "Stage", + "path": "lambda-test-from-asset-construct-stack/AssetCode/Stage", + "constructInfo": { + "fqn": "@aws-cdk/core.AssetStaging", + "version": "0.0.0" + } + }, + "AssetBucket": { + "id": "AssetBucket", + "path": "lambda-test-from-asset-construct-stack/AssetCode/AssetBucket", + "constructInfo": { + "fqn": "@aws-cdk/aws-s3.BucketBase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-s3-assets.Asset", + "version": "0.0.0" + } + }, + "MyLambda": { + "id": "MyLambda", + "path": "lambda-test-from-asset-construct-stack/MyLambda", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "lambda-test-from-asset-construct-stack/MyLambda/ServiceRole", + "children": { + "Resource": { + "id": "Resource", + "path": "lambda-test-from-asset-construct-stack/MyLambda/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "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" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-iam.Role", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "lambda-test-from-asset-construct-stack/MyLambda/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Function", + "aws:cdk:cloudformation:props": { + "code": { + "s3Bucket": { + "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" + }, + "s3Key": "a37d3ef54c18e7738fe5dc008504591bd3b1f14c6a09ee91eac6d55f7ca5ba5f.zip" + }, + "role": { + "Fn::GetAtt": [ + "MyLambdaServiceRole4539ECB6", + "Arn" + ] + }, + "handler": "index.main", + "runtime": "python3.9" + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-lambda.CfnFunction", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-lambda.Function", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/core.Stack", + "version": "0.0.0" + } + }, + "lambda-test-from-asset-construct": { + "id": "lambda-test-from-asset-construct", + "path": "lambda-test-from-asset-construct", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "lambda-test-from-asset-construct/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "lambda-test-from-asset-construct/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.92" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "lambda-test-from-asset-construct/DefaultTest/DeployAssert", + "constructInfo": { + "fqn": "@aws-cdk/core.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests.IntegTest", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/core.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/integ.from-asset-construct.ts b/packages/@aws-cdk/aws-lambda/test/integ.from-asset-construct.ts new file mode 100644 index 0000000000000..3810c3e2f9143 --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/integ.from-asset-construct.ts @@ -0,0 +1,26 @@ +import * as path from 'path'; +import * as s3_assets from '@aws-cdk/aws-s3-assets'; +import * as cdk from '@aws-cdk/core'; +import { IntegTest } from '@aws-cdk/integ-tests'; +import * as lambda from '../lib'; + +class TestStack extends cdk.Stack { + constructor(scope: cdk.App, id: string) { + super(scope, id); + + const codeAsset = new s3_assets.Asset(this, 'AssetCode', { + path: path.join(__dirname, 'handler.zip'), + }); + new lambda.Function(this, 'MyLambda', { + code: lambda.Code.fromAssetConstruct(codeAsset), + handler: 'index.main', + runtime: lambda.Runtime.PYTHON_3_9, + }); + } +} + +const app = new cdk.App(); + +const stack = new TestStack(app, 'lambda-test-from-asset-construct-stack'); + +new IntegTest(app, 'lambda-test-from-asset-construct', { testCases: [stack] }); \ No newline at end of file