Skip to content

Commit

Permalink
feat(lambda-layers): add AWS SDK JS layer
Browse files Browse the repository at this point in the history
Add a new package `@aws-cdk/lambda-layers` that offers pre-built layers.

Layer building is currently only supported for Node.js and works as follows:
* List static members of the `Layer` class (= layers to build)
* Create a dummy `package.json` file containing only dependencies for this layer
in the `layers` folder. Dependencies versions are extracted from the `package.json`
of `@aws-cdk/lambda-layers`.
* Run `npm install`

As layer dependencies are extracted from the main `package.json` they can be picked
up by `dependabot`, automatically updating the layer when needed.

The `getLayerVersion()` method returns a singleton `lambda.LayerVersion` that can be
used in Lambda functions.

The first layer is `AWS_SDK_JS` which offers a more recent version of the AWS SDK than
the one included in the Lambda runtime (it will also be updated more frequently).

Use the `AWS_SDK_JS` layer in `AwsCustomResource` and `AwsApi` event target.

Closes aws#2689
Closes aws#5063
  • Loading branch information
jogold committed Nov 18, 2019
1 parent f4ea264 commit 6423f64
Show file tree
Hide file tree
Showing 18 changed files with 863 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-events-targets/lib/aws-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import events = require('@aws-cdk/aws-events');
import iam = require('@aws-cdk/aws-iam');
import lambda = require('@aws-cdk/aws-lambda');
import { Layer } from '@aws-cdk/lambda-layers';
import path = require('path');
import metadata = require('./sdk-api-metadata.json');
import { addLambdaPermission } from './util';
Expand Down Expand Up @@ -87,6 +88,7 @@ export class AwsApi implements events.IRuleTarget {
handler: 'index.handler',
uuid: 'b4cf1abd-4e4f-4bc6-9944-1af7ccd9ec37',
lambdaPurpose: 'AWS',
layers: [Layer.AWS_SDK_JS.getLayerVersion(rule as events.Rule)]
});

if (this.props.policyStatement) {
Expand Down
8 changes: 5 additions & 3 deletions packages/@aws-cdk/aws-events-targets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
"@aws-cdk/aws-sns-subscriptions": "1.16.1",
"@aws-cdk/aws-sqs": "1.16.1",
"@aws-cdk/aws-stepfunctions": "1.16.1",
"@aws-cdk/core": "1.16.1"
"@aws-cdk/core": "1.16.1",
"@aws-cdk/lambda-layers": "1.16.1"
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
Expand All @@ -118,7 +119,8 @@
"@aws-cdk/aws-sns-subscriptions": "1.16.1",
"@aws-cdk/aws-sqs": "1.16.1",
"@aws-cdk/aws-stepfunctions": "1.16.1",
"@aws-cdk/core": "1.16.1"
"@aws-cdk/core": "1.16.1",
"@aws-cdk/lambda-layers": "1.16.1"
},
"engines": {
"node": ">= 10.3.0"
Expand All @@ -135,4 +137,4 @@
"props-default-doc:@aws-cdk/aws-events-targets.EcsTaskProps.containerOverrides"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,53 @@
]
}
},
"layerawssdkjsB2ED5E7B": {
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": {
"Ref": "AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3Bucket9111837A"
},
"S3Key": {
"Fn::Join": [
"",
[
{
"Fn::Select": [
0,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3VersionKey5C696394"
}
]
}
]
},
{
"Fn::Select": [
1,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3VersionKey5C696394"
}
]
}
]
}
]
]
}
},
"CompatibleRuntimes": [
"nodejs10.x"
],
"Description": "AWS SDK JS"
}
},
"AWSb4cf1abd4e4f4bc699441af7ccd9ec37ServiceRole9FFE9C50": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -139,7 +186,12 @@
"Arn"
]
},
"Runtime": "nodejs10.x"
"Runtime": "nodejs10.x",
"Layers": [
{
"Ref": "layerawssdkjsB2ED5E7B"
}
]
},
"DependsOn": [
"AWSb4cf1abd4e4f4bc699441af7ccd9ec37ServiceRoleDefaultPolicy4D43A7C1",
Expand Down Expand Up @@ -219,6 +271,18 @@
}
},
"Parameters": {
"AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3Bucket9111837A": {
"Type": "String",
"Description": "S3 bucket for asset \"1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7\""
},
"AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3VersionKey5C696394": {
"Type": "String",
"Description": "S3 key for asset version \"1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7\""
},
"AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7ArtifactHash24D48A0B": {
"Type": "String",
"Description": "Artifact hash for asset \"1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7\""
},
"AssetParameters7f96a9f879348c0e50ac55d67a74edede0cc96cf0615355938c45801708cd063S3Bucket05714AA7": {
"Type": "String",
"Description": "S3 bucket for asset \"7f96a9f879348c0e50ac55d67a74edede0cc96cf0615355938c45801708cd063\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CustomResource, CustomResourceProvider } from '@aws-cdk/aws-cloudformat
import iam = require('@aws-cdk/aws-iam');
import lambda = require('@aws-cdk/aws-lambda');
import cdk = require('@aws-cdk/core');
import { Layer } from '@aws-cdk/lambda-layers';
import fs = require('fs');
import path = require('path');

Expand Down Expand Up @@ -180,6 +181,7 @@ export class AwsCustomResource extends cdk.Construct implements iam.IGrantable {
lambdaPurpose: 'AWS',
timeout: props.timeout || cdk.Duration.seconds(30),
role: props.role,
layers: [Layer.AWS_SDK_JS.getLayerVersion(this)]
});
this.grantPrincipal = provider.grantPrincipal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,53 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"layerawssdkjsB2ED5E7B": {
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": {
"Ref": "AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3Bucket9111837A"
},
"S3Key": {
"Fn::Join": [
"",
[
{
"Fn::Select": [
0,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3VersionKey5C696394"
}
]
}
]
},
{
"Fn::Select": [
1,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3VersionKey5C696394"
}
]
}
]
}
]
]
}
},
"CompatibleRuntimes": [
"nodejs10.x"
],
"Description": "AWS SDK JS"
}
},
"AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -153,6 +200,11 @@
]
},
"Runtime": "nodejs10.x",
"Layers": [
{
"Ref": "layerawssdkjsB2ED5E7B"
}
],
"Timeout": 30
},
"DependsOn": [
Expand Down Expand Up @@ -230,6 +282,18 @@
}
},
"Parameters": {
"AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3Bucket9111837A": {
"Type": "String",
"Description": "S3 bucket for asset \"1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7\""
},
"AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7S3VersionKey5C696394": {
"Type": "String",
"Description": "S3 key for asset version \"1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7\""
},
"AssetParameters1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7ArtifactHash24D48A0B": {
"Type": "String",
"Description": "Artifact hash for asset \"1225cfb84ab98458f900aca0912b08536c3fc65797fbc5fa9bea7f85e756a1c7\""
},
"AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3Bucket7871AF0F": {
"Type": "String",
"Description": "S3 bucket for asset \"ba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915\""
Expand Down Expand Up @@ -269,4 +333,4 @@
}
}
}
}
}
17 changes: 17 additions & 0 deletions packages/@aws-cdk/lambda-layers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.js
*.js.map
*.d.ts
node_modules
dist
tsconfig.json
tslint.json
.jsii

.LAST_BUILD
.LAST_PACKAGE
*.snk
.nyc_output
coverage
.nycrc

layers
18 changes: 18 additions & 0 deletions packages/@aws-cdk/lambda-layers/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Don't include original .ts files when doing `npm pack`
*.ts
!*.d.ts
coverage
.nyc_output
*.tgz

dist
.LAST_PACKAGE
.LAST_BUILD
!*.js

# Include .jsii
!.jsii

*.snk

*.tsbuildinfo
Loading

0 comments on commit 6423f64

Please sign in to comment.