From a1cd7432eb817a0a9361c907445c01eea4cb1321 Mon Sep 17 00:00:00 2001 From: Michael <6798695+gruenem@users.noreply.github.com> Date: Thu, 16 Jan 2020 16:10:02 +0100 Subject: [PATCH] feat(lambda): allow inline code for nodejs12.x runtime (#5710) * chore(lambda): new runtimes support inline code The nodejs12.x and python3.8 Lambda runtimes actually support inline code. This reverts commit feb1f9beb9e4d2fa9d23006e2e7715b8a5434d5e. * Python3.8 doesn't support inline code yet * add integ test * add missing expected.json Co-authored-by: Niranjan Jayakar <16217941+nija-at@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- packages/@aws-cdk/aws-lambda/lib/runtime.ts | 2 +- .../integ.runtime.inlinecode.expected.json | 281 ++++++++++++++++++ .../test/integ.runtime.inlinecode.ts | 53 ++++ 3 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 packages/@aws-cdk/aws-lambda/test/integ.runtime.inlinecode.expected.json create mode 100644 packages/@aws-cdk/aws-lambda/test/integ.runtime.inlinecode.ts diff --git a/packages/@aws-cdk/aws-lambda/lib/runtime.ts b/packages/@aws-cdk/aws-lambda/lib/runtime.ts index 57b6f3d160c85..29f1d0358b977 100644 --- a/packages/@aws-cdk/aws-lambda/lib/runtime.ts +++ b/packages/@aws-cdk/aws-lambda/lib/runtime.ts @@ -35,7 +35,7 @@ export class Runtime { /** @deprecated Use {@link NODEJS_10_X} */ public static readonly NODEJS_8_10 = new Runtime('nodejs8.10', RuntimeFamily.NODEJS, { supportsInlineCode: true }); public static readonly NODEJS_10_X = new Runtime('nodejs10.x', RuntimeFamily.NODEJS, { supportsInlineCode: true }); - public static readonly NODEJS_12_X = new Runtime('nodejs12.x', RuntimeFamily.NODEJS); + public static readonly NODEJS_12_X = new Runtime('nodejs12.x', RuntimeFamily.NODEJS, { supportsInlineCode: true }); public static readonly PYTHON_2_7 = new Runtime('python2.7', RuntimeFamily.PYTHON, { supportsInlineCode: true }); public static readonly PYTHON_3_6 = new Runtime('python3.6', RuntimeFamily.PYTHON, { supportsInlineCode: true }); public static readonly PYTHON_3_7 = new Runtime('python3.7', RuntimeFamily.PYTHON, { supportsInlineCode: true }); diff --git a/packages/@aws-cdk/aws-lambda/test/integ.runtime.inlinecode.expected.json b/packages/@aws-cdk/aws-lambda/test/integ.runtime.inlinecode.expected.json new file mode 100644 index 0000000000000..8bbe8cdef572a --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/integ.runtime.inlinecode.expected.json @@ -0,0 +1,281 @@ +{ + "Resources": { + "NODEJS10XServiceRole2FD24B65": { + "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" + ] + ] + } + ] + } + }, + "NODEJS10XF3831960": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "exports.handler = async function(event) { return \"success\" }" + }, + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "NODEJS10XServiceRole2FD24B65", + "Arn" + ] + }, + "Runtime": "nodejs10.x" + }, + "DependsOn": [ + "NODEJS10XServiceRole2FD24B65" + ] + }, + "NODEJS12XServiceRole59E71436": { + "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" + ] + ] + } + ] + } + }, + "NODEJS12X8B8075A4": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "exports.handler = async function(event) { return \"success\" }" + }, + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "NODEJS12XServiceRole59E71436", + "Arn" + ] + }, + "Runtime": "nodejs12.x" + }, + "DependsOn": [ + "NODEJS12XServiceRole59E71436" + ] + }, + "PYTHON27ServiceRoleF484A17D": { + "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" + ] + ] + } + ] + } + }, + "PYTHON27F8E941FA": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "def handler(event, context):\n return \"success\"" + }, + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "PYTHON27ServiceRoleF484A17D", + "Arn" + ] + }, + "Runtime": "python2.7" + }, + "DependsOn": [ + "PYTHON27ServiceRoleF484A17D" + ] + }, + "PYTHON36ServiceRole814B3AD9": { + "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" + ] + ] + } + ] + } + }, + "PYTHON364935EF15": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "def handler(event, context):\n return \"success\"" + }, + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "PYTHON36ServiceRole814B3AD9", + "Arn" + ] + }, + "Runtime": "python3.6" + }, + "DependsOn": [ + "PYTHON36ServiceRole814B3AD9" + ] + }, + "PYTHON37ServiceRoleDE7E561E": { + "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" + ] + ] + } + ] + } + }, + "PYTHON37D3A10E04": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "def handler(event, context):\n return \"success\"" + }, + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "PYTHON37ServiceRoleDE7E561E", + "Arn" + ] + }, + "Runtime": "python3.7" + }, + "DependsOn": [ + "PYTHON37ServiceRoleDE7E561E" + ] + } + }, + "Outputs": { + "NODEJS10XfunctionName": { + "Value": { + "Ref": "NODEJS10XF3831960" + } + }, + "NODEJS12XfunctionName": { + "Value": { + "Ref": "NODEJS12X8B8075A4" + } + }, + "PYTHON27functionName": { + "Value": { + "Ref": "PYTHON27F8E941FA" + } + }, + "PYTHON36functionName": { + "Value": { + "Ref": "PYTHON364935EF15" + } + }, + "PYTHON37functionName": { + "Value": { + "Ref": "PYTHON37D3A10E04" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/test/integ.runtime.inlinecode.ts b/packages/@aws-cdk/aws-lambda/test/integ.runtime.inlinecode.ts new file mode 100644 index 0000000000000..aa4ef06e6a5e1 --- /dev/null +++ b/packages/@aws-cdk/aws-lambda/test/integ.runtime.inlinecode.ts @@ -0,0 +1,53 @@ +import { App, CfnOutput, Stack } from '@aws-cdk/core'; +import { Function, InlineCode, Runtime } from '../lib'; + +// CloudFormation supports InlineCode only for a subset of runtimes. This integration test +// is used to verify that the ones marked in the CDK are in fact supported by CloudFormation. +// Running `cdk deploy` on this stack will confirm if all the runtimes here are supported. +// +// To verify that the lambda function works correctly, use the function names that are part +// of the stack output (printed on the console at the end of 'cdk deploy') and run the command - +// aws lambda invoke --function-name +// +// If successful, the output will contain "success" + +const app = new App(); + +const stack = new Stack(app, 'aws-cdk-lambda-runtime-inlinecode'); + +const node10xfn = new Function(stack, 'NODEJS_10_X', { + code: new InlineCode('exports.handler = async function(event) { return "success" }'), + handler: 'index.handler', + runtime: Runtime.NODEJS_10_X, +}); +new CfnOutput(stack, 'NODEJS_10_X-functionName', { value: node10xfn.functionName }); + +const node12xfn = new Function(stack, 'NODEJS_12_X', { + code: new InlineCode('exports.handler = async function(event) { return "success" }'), + handler: 'index.handler', + runtime: Runtime.NODEJS_12_X, +}); +new CfnOutput(stack, 'NODEJS_12_X-functionName', { value: node12xfn.functionName }); + +const python27 = new Function(stack, 'PYTHON_2_7', { + code: new InlineCode('def handler(event, context):\n return "success"'), + handler: 'index.handler', + runtime: Runtime.PYTHON_2_7, +}); +new CfnOutput(stack, 'PYTHON_2_7-functionName', { value: python27.functionName }); + +const python36 = new Function(stack, 'PYTHON_3_6', { + code: new InlineCode('def handler(event, context):\n return "success"'), + handler: 'index.handler', + runtime: Runtime.PYTHON_3_6, +}); +new CfnOutput(stack, 'PYTHON_3_6-functionName', { value: python36.functionName }); + +const python37 = new Function(stack, 'PYTHON_3_7', { + code: new InlineCode('def handler(event, context):\n return "success"'), + handler: 'index.handler', + runtime: Runtime.PYTHON_3_7, +}); +new CfnOutput(stack, 'PYTHON_3_7-functionName', { value: python37.functionName }); + +app.synth(); \ No newline at end of file