Skip to content

Commit 2d26d01

Browse files
authored
update layers page (#8032)
* update layers page * minor nit * style improvements
1 parent f1bf1f3 commit 2d26d01

File tree

1 file changed

+35
-21
lines changed
  • src/pages/[platform]/build-a-backend/functions/add-lambda-layers

1 file changed

+35
-21
lines changed

src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx

+35-21
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,47 @@ export function getStaticProps() {
2929
};
3030
}
3131

32-
Amplify offers the ability to add layers to your Functions which contain your library dependencies. To get started, specify the `layers` property in `defineFunction`:
32+
Amplify offers the ability to add layers to your functions which contain your library dependencies. Lambda layers allow you to separate your function code from its dependencies, enabling easier management of shared components across multiple functions and reducing deployment package sizes.
3333

34-
```ts title="amplify/functions/my-function/resource.ts"
35-
import { defineFunction } from "@aws-amplify/backend";
34+
To add a Lambda layer to your function, follow these steps:
3635

37-
export const myFunction = defineFunction({
38-
name: "my-function",
39-
layers: {
40-
"@aws-lambda-powertools/logger":
41-
"arn:aws:lambda:us-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:12",
42-
},
43-
});
44-
```
36+
1. First, create and set up your Lambda layer in AWS. You can do this through the AWS Console or using the AWS CLI. For guidance on creating layers, refer to the [AWS documentation on creating Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-create).
4537

46-
The Lambda layer is represented by an object of key/value pairs where the key is the module name that is exported from your layer and the value is the ARN of the layer. The key (module name) is used to externalize the module dependency so it doesn't get bundled with your lambda function. A maximum of 5 layers can be attached to a function, and they must be in the same region as the function.
38+
2. Once your layer is created and available in AWS, you can reference it in your Amplify project as shown below.
4739

48-
Then use the locally installed module in the function handler:
49-
```ts title="amplify/functions/my-function/handler.ts"
50-
import { Logger } from "@aws-lambda-powertools/logger";
51-
import type { Handler } from "aws-lambda";
40+
Specify the `layers` property in `defineFunction`, for example:
5241

53-
const logger = new Logger({ serviceName: "serverlessAirline" });
42+
```ts title="amplify/functions/my-function/resource.ts"
43+
import { defineFunction } from "@aws-amplify/backend";
5444

55-
export const handler: Handler = async (event, context) => {
56-
logger.info("Hello World");
57-
};
58-
```
45+
export const myFunction = defineFunction({
46+
name: "my-function",
47+
layers: {
48+
"@aws-lambda-powertools/logger":
49+
"arn:aws:lambda:us-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:12",
50+
},
51+
});
52+
```
53+
54+
The Lambda layer is represented by an object of key/value pairs where the key is the module name that is exported from your layer and the value is the ARN of the layer. The key (module name) is used to externalize the module dependency so it doesn't get bundled with your lambda function. A maximum of 5 layers can be attached to a function, and they must be in the same region as the function.
55+
56+
<Callout type="warning">
57+
58+
When using layers, be mindful of versioning. The ARN includes a version number (e.g., `:12` in the example). Ensure you're using the appropriate version and have a strategy for updating layers when new versions are released.
59+
60+
</Callout>
61+
62+
3. Then use the locally installed module in the function handler:
63+
```ts title="amplify/functions/my-function/handler.ts"
64+
import { Logger } from "@aws-lambda-powertools/logger";
65+
import type { Handler } from "aws-lambda";
66+
67+
const logger = new Logger({ serviceName: "serverlessAirline" });
68+
69+
export const handler: Handler = async (event, context) => {
70+
logger.info("Hello World");
71+
};
72+
```
5973

6074
For further information on creating and managing your layers refer to [AWS documentation for Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html)
6175

0 commit comments

Comments
 (0)