Skip to content

Commit c1a51b7

Browse files
authored
docs: how to create a layer in AWS Lambda
1 parent 6b6145d commit c1a51b7

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

supplemental-docs/AWS_LAMBDA.md

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
Several AWS Lambda runtimes, including those for Node.js, include the AWS SDK at various versions.
66

77
The SDK is provided as a convenience for development. For greater control of the SDK version and its runtime characteristics such as
8-
JavaScript bundling, upload your selection of the AWS SDK as part of your function code.
8+
JavaScript bundling, create an AWS Lambda layer containing your AWS SDK dependencies or
9+
upload your selection of the AWS SDK as part of your function code.
910

1011
To check the version of the SDK that is installed, you can log the package.json metadata of a package that you are using.
1112

@@ -19,6 +20,106 @@ exports.handler = function (event) {
1920
};
2021
```
2122

23+
## Creating an AWS Lambda layer
24+
25+
This information was taken from https://docs.aws.amazon.com/lambda/latest/dg/nodejs-layers.html.
26+
27+
### Create the content for your Layer
28+
29+
In a clean folder, create a `package.json` with your required dependencies.
30+
31+
```json5
32+
// package.json
33+
{
34+
"dependencies": {
35+
"@aws-sdk/client-s3": "<=3.750.0",
36+
"@aws-sdk/client-dynamodb": "<=3.750.0",
37+
"@aws-sdk/lib-dynamodb": "<=3.750.0",
38+
"@aws-sdk/client-lambda": "<=3.750.0"
39+
}
40+
}
41+
```
42+
43+
In this example we've used the `<=` version range to ensure the AWS SDK dependencies use the same transitive dependency,
44+
i.e., core package versions. Run `npm install` or equivalent to create the `node_modules` folder, and then zip the contents
45+
into the following structure:
46+
47+
```
48+
layer_content.zip
49+
└ nodejs
50+
└ node_modules
51+
└ @aws-sdk/etc.
52+
```
53+
54+
### Deploying the AWS Lambda layer to your account
55+
56+
```js
57+
// example: deploying the AWS Lambda layer.
58+
import { Lambda } from "@aws-sdk/client-lambda";
59+
import fs from "node:fs";
60+
61+
const lambda = new Lambda({
62+
region: "us-west-2",
63+
});
64+
65+
await lambda.publishLayerVersion({
66+
LayerName: "MyAwsSdkDependencies-layer",
67+
Description: "Contains AWS SDK dependencies to use in my AWS Lambda functions",
68+
Content: {
69+
// read the layer zip file as a buffer.
70+
ZipFile: fs.readFileSync("./path/to/layer_content.zip"),
71+
},
72+
CompatibleRuntimes: ["nodejs18.x", "nodejs20.x", "nodejs22.x"],
73+
CompatibleArchitectures: [
74+
"x86_64",
75+
"arm64",
76+
],
77+
});
78+
```
79+
80+
### Using the AWS Lambda layer in your functions
81+
82+
If you have a lot of layers, you may need to paginate the `listLayers` request or
83+
find the ARN another way.
84+
85+
```js
86+
// example: looking up the layer's ARN by using its name from earlier.
87+
import { Lambda } from "@aws-sdk/client-lambda";
88+
89+
const lambda = new Lambda({
90+
region: "us-west-2",
91+
});
92+
93+
const listLayers = await lambda.listLayers({});
94+
const layerArn = listLayers.Layers.find(
95+
(l) => l.LayerName === "MyAwsSdkDependencies-layer"
96+
).LatestMatchingVersion.LayerVersionArn;
97+
```
98+
99+
Adding the layer to an existing function or creating a new function with the layer:
100+
101+
```js
102+
await lambda.updateFunctionConfiguration({
103+
FunctionName: "FUNCTION_NAME",
104+
Layers: [layerArn],
105+
});
106+
107+
// or:
108+
109+
await lambda.createFunction({
110+
FunctionName: "FUNCTION_NAME",
111+
Role: "Role.Arn",
112+
Code: {
113+
ZipFile: Buffer.from("..."),
114+
},
115+
Runtime: Runtime.nodejs22x,
116+
Description: `my new function using a layer`,
117+
Timeout: 45,
118+
Layers: [layerArn],
119+
Handler: `./index.handler`,
120+
});
121+
```
122+
22123
## Best practices for initializing AWS SDK Clients in AWS Lambda
23124

24125
Suppose that you have an `async` function called, for example `prepare`, that you need to initialize only once.

0 commit comments

Comments
 (0)