Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,47 @@ export function getStaticProps() {
};
}

Amplify offers the ability to add layers to your Functions which contain your library dependencies. To get started, specify the `layers` property in `defineFunction`:
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.

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

export const myFunction = defineFunction({
name: "my-function",
layers: {
"@aws-lambda-powertools/logger":
"arn:aws:lambda:us-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:12",
},
});
```
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).

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.
2. Once your layer is created and available in AWS, you can reference it in your Amplify project as shown below.

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

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

export const handler: Handler = async (event, context) => {
logger.info("Hello World");
};
```
export const myFunction = defineFunction({
name: "my-function",
layers: {
"@aws-lambda-powertools/logger":
"arn:aws:lambda:us-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:12",
},
});
```

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.

<Callout type="warning">

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.

</Callout>

3. Then use the locally installed module in the function handler:
```ts title="amplify/functions/my-function/handler.ts"
import { Logger } from "@aws-lambda-powertools/logger";
import type { Handler } from "aws-lambda";

const logger = new Logger({ serviceName: "serverlessAirline" });

export const handler: Handler = async (event, context) => {
logger.info("Hello World");
};
```

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)