From 875dcb51d92ac17016369d6dd33bee8b1fd40872 Mon Sep 17 00:00:00 2001 From: ykethan Date: Fri, 11 Oct 2024 10:14:05 -0400 Subject: [PATCH 1/3] update layers page --- .../functions/add-lambda-layers/index.mdx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx b/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx index 692dd796868..767d03150ff 100644 --- a/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx +++ b/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx @@ -29,7 +29,15 @@ 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. + +To add a Lambda layer to your function, follow these steps: + +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). + +2. Once your layer is created and available in AWS, you can reference it in your Amplify project as shown below. + +Specify the `layers` property in `defineFunction`, for example: ```ts title="amplify/functions/my-function/resource.ts" import { defineFunction } from "@aws-amplify/backend"; @@ -45,6 +53,8 @@ export const myFunction = defineFunction({ 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. +> 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. + 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"; From 9fd8b02e811c130d82b56754a0e62883893943f7 Mon Sep 17 00:00:00 2001 From: ykethan Date: Fri, 11 Oct 2024 10:18:04 -0400 Subject: [PATCH 2/3] minor nit --- .../build-a-backend/functions/add-lambda-layers/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx b/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx index 767d03150ff..d77c6c99575 100644 --- a/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx +++ b/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx @@ -55,7 +55,7 @@ The Lambda layer is represented by an object of key/value pairs where the key is > 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. -Then use the locally installed module in the function handler: +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"; From 6139870914f6f72b7d9644bc71d28c0bdcc55bc3 Mon Sep 17 00:00:00 2001 From: ykethan Date: Fri, 11 Oct 2024 13:46:24 -0400 Subject: [PATCH 3/3] style improvements --- .../functions/add-lambda-layers/index.mdx | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx b/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx index d77c6c99575..132d20ec1ff 100644 --- a/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx +++ b/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx @@ -37,35 +37,39 @@ To add a Lambda layer to your function, follow these steps: 2. Once your layer is created and available in AWS, you can reference it in your Amplify project as shown below. -Specify the `layers` property in `defineFunction`, for example: + Specify the `layers` property in `defineFunction`, for example: -```ts title="amplify/functions/my-function/resource.ts" -import { defineFunction } from "@aws-amplify/backend"; + ```ts title="amplify/functions/my-function/resource.ts" + import { defineFunction } from "@aws-amplify/backend"; -export const myFunction = defineFunction({ - name: "my-function", - layers: { - "@aws-lambda-powertools/logger": - "arn:aws:lambda:us-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:12", - }, -}); -``` + 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. + 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. + + -> 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. + 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. + + 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"; + ```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" }); + const logger = new Logger({ serviceName: "serverlessAirline" }); -export const handler: Handler = async (event, context) => { - logger.info("Hello World"); -}; -``` + 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)