Skip to content

Commit

Permalink
fix: implemented workaround for aws/aws-cdk#1972
Browse files Browse the repository at this point in the history
  • Loading branch information
ryparker committed Sep 10, 2021
1 parent 682296c commit 3fbda2b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 21 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ _This only needs to be ran once per account/region._
yarn build
```

**3. Deploy Cloudformation infrastructure to AWS account**
**3. Deploy the Lambda Layer stack**

```shell
yarn deploy
yarn deploy LambdaLayerStack
```

**3. Deploy the Lambda Function stack**

```shell
yarn deploy LambdaFunctionStack
```
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"deploy": "cdk deploy"
},
"dependencies": {
"@aws-cdk/aws-lambda": "^1.121.0",
"aws-cdk": "^1.121.0",
"@aws-cdk/aws-lambda": "^1.122.0",
"@aws-cdk/aws-ssm": "^1.122.0",
"aws-cdk": "^1.122.0",
"typescript": "^4.4.2"
},
"resolutions": {
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// SSM Parameter Names: Lambda Layer Arns
export const NPM_DEPENDENCIES_LAYER_ARN_PARAM_NAME = '/sample-lambda-layers-app/shared/npmDependencyLayerArn';
22 changes: 6 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { Runtime } from '@aws-cdk/aws-lambda';
import { App, Stack, Stage } from "@aws-cdk/core";
import { Function, LayerVersion, Code } from '@aws-cdk/aws-lambda';
import { App } from "@aws-cdk/core";
import createLambdaLayerStack from './stacks/lambda-layer';
import createLambdaFunctionStack from './stacks/lambda-function';

const app = new App();
const stack = new Stack(app, "MyStack");

const layer = new LayerVersion(stack, 'NpmDependencyLayer', {
code: Code.fromAsset('nodejs.zip'),
compatibleRuntimes: [Runtime.NODEJS_14_X],
description: 'A layer that contains NPM dependencies.',
});
const app = new App();

new Function(stack, 'MyFunction', {
code: Code.fromAsset('src/lambdas'),
runtime: Runtime.NODEJS_14_X,
handler: 'index.handler',
layers: [layer],
});
createLambdaLayerStack(app);
createLambdaFunctionStack(app);
20 changes: 20 additions & 0 deletions src/stacks/lambda-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Stack, Construct } from "@aws-cdk/core";
import { Function, LayerVersion, Code, Runtime } from '@aws-cdk/aws-lambda';
import { StringParameter } from '@aws-cdk/aws-ssm';
import { NPM_DEPENDENCIES_LAYER_ARN_PARAM_NAME } from '../constants';

export default (scope: Construct) => {
const stack = new Stack(scope, 'LambdaFunctionStack');

const npmDependenciesLayerArn = StringParameter.valueForStringParameter(stack, NPM_DEPENDENCIES_LAYER_ARN_PARAM_NAME);
const npmDependenciesLayer = LayerVersion.fromLayerVersionArn(stack, 'NpmDependenciesLayerImport', npmDependenciesLayerArn);

new Function(stack, 'MyFunction', {
code: Code.fromAsset('src/lambdas'),
runtime: Runtime.NODEJS_14_X,
handler: 'index.handler',
layers: [npmDependenciesLayer],
});

return stack;
};
22 changes: 22 additions & 0 deletions src/stacks/lambda-layer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Runtime } from '@aws-cdk/aws-lambda';
import { Construct, Stack } from "@aws-cdk/core";
import { LayerVersion, Code } from '@aws-cdk/aws-lambda';
import { StringParameter } from '@aws-cdk/aws-ssm';
import { NPM_DEPENDENCIES_LAYER_ARN_PARAM_NAME } from '../constants';

export default (scope: Construct) => {
const stack = new Stack(scope, 'LambdaLayerStack');

const layer = new LayerVersion(stack, 'NpmDependenciesLayer', {
code: Code.fromAsset('nodejs.zip'),
compatibleRuntimes: [Runtime.NODEJS_14_X],
description: 'A layer that contains NPM dependencies.',
});

new StringParameter(stack, 'NpmDependenciesLayerParam', {
parameterName: NPM_DEPENDENCIES_LAYER_ARN_PARAM_NAME,
stringValue: layer.layerVersionArn,
})

return stack;
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
"@aws-cdk/core" "1.122.0"
constructs "^3.3.69"

"@aws-cdk/[email protected]":
"@aws-cdk/[email protected]", "@aws-cdk/aws-ssm@^1.122.0":
version "1.122.0"
resolved "https://registry.yarnpkg.com/@aws-cdk/aws-ssm/-/aws-ssm-1.122.0.tgz#31e5707bc818d1166a858db6a5ddd527061006b0"
integrity sha512-yoB4tUeV8GgA2MylEMAXlJQUbD9mvOUoZk9zTEGmNyjhpTZBGB24YbBfeoqnq2Igh9PfAlNZkPtguGp/tPZVJQ==
Expand Down

0 comments on commit 3fbda2b

Please sign in to comment.