From ba10f667e3fd6479e31a4e84203b9df865449178 Mon Sep 17 00:00:00 2001 From: ykethan Date: Thu, 15 Aug 2024 15:11:40 -0400 Subject: [PATCH] adds layers page --- src/directory/directory.mjs | 3 + .../functions/add-lambda-layers/index.mdx | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx diff --git a/src/directory/directory.mjs b/src/directory/directory.mjs index d8bcb643a59..46318a737f5 100644 --- a/src/directory/directory.mjs +++ b/src/directory/directory.mjs @@ -382,6 +382,9 @@ export const directory = { { path: 'src/pages/[platform]/build-a-backend/functions/streaming-logs/index.mdx' }, + { + path: 'src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx' + }, { path: 'src/pages/[platform]/build-a-backend/functions/grant-access-to-other-resources/index.mdx' }, 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 new file mode 100644 index 00000000000..8401aef0979 --- /dev/null +++ b/src/pages/[platform]/build-a-backend/functions/add-lambda-layers/index.mdx @@ -0,0 +1,60 @@ +import { getCustomStaticPath } from '@/utils/getCustomStaticPath'; + +export const meta = { + title: 'Lambda Layers', + description: + 'Learn how to add layers to your function', + platforms: [ + 'android', + 'angular', + 'flutter', + 'javascript', + 'nextjs', + 'react', + 'react-native', + 'swift', + 'vue' + ] +}; + +export function getStaticPaths() { + return getCustomStaticPath(meta.platforms); +} + +export function getStaticProps() { + return { + props: { + meta + } + }; +} + +Amplify offers the ability to add layers to your Functions which contain your library dependencies. To get started, specify the `layers` property in `defineFunction`: + +```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", + }, +}); +``` + +The key for the layer is a module name hosted on your existing layer and will be externalized ensuring the module is not bundled with your function. The value accepts an Arn that references to a version of your layer that exists in the same region as your function. You can add upto 5 layers in your function. + +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"; + +const logger = new Logger({ serviceName: "serverlessAirline" }); + +export const handler = async (): Promise => { + 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) +