Skip to content
GitHubXDiscord

Function

The Function resource lets you manage AWS Lambda Functions and their configuration settings.

Create a basic Lambda function with required properties and a common optional property for memory size.

import AWS from "alchemy/aws/control";
const basicLambdaFunction = await AWS.Lambda.Function("basicLambda", {
FunctionName: "basicLambdaFunction",
Handler: "index.handler",
Runtime: "nodejs14.x",
Role: "arn:aws:iam::123456789012:role/execution_role",
Code: {
ZipFile: "exports.handler = async (event) => { return 'Hello, World!'; };"
},
MemorySize: 128
});

Configure a Lambda function with a VPC configuration and environment variables for enhanced security and functionality.

const advancedLambdaFunction = await AWS.Lambda.Function("advancedLambda", {
FunctionName: "advancedLambdaFunction",
Handler: "index.handler",
Runtime: "nodejs14.x",
Role: "arn:aws:iam::123456789012:role/execution_role",
Code: {
ZipFile: "exports.handler = async (event) => { return 'Advanced Lambda!'; };"
},
VpcConfig: {
SubnetIds: ["subnet-0bb1c79de3EXAMPLE"],
SecurityGroupIds: ["sg-0c9f7b1c8eEXAMPLE"]
},
Environment: {
Variables: {
ENVIRONMENT: "production",
API_URL: "https://api.example.com"
}
},
Timeout: 10
});

This example demonstrates how to configure a Lambda function with a dead letter queue for handling errors.

const lambdaWithDLQ = await AWS.Lambda.Function("lambdaWithDLQ", {
FunctionName: "lambdaWithDLQFunction",
Handler: "index.handler",
Runtime: "nodejs14.x",
Role: "arn:aws:iam::123456789012:role/execution_role",
Code: {
ZipFile: "exports.handler = async (event) => { throw new Error('Error!'); };"
},
DeadLetterConfig: {
TargetArn: "arn:aws:sqs:us-west-2:123456789012:dead-letter-queue"
},
MemorySize: 256,
Timeout: 15
});

This example shows how to add a Lambda layer for shared libraries.

const lambdaWithLayer = await AWS.Lambda.Function("lambdaWithLayer", {
FunctionName: "lambdaWithLayerFunction",
Handler: "index.handler",
Runtime: "nodejs14.x",
Role: "arn:aws:iam::123456789012:role/execution_role",
Code: {
ZipFile: "exports.handler = async (event) => { return 'Using layers!'; };"
},
Layers: [
"arn:aws:lambda:us-west-2:123456789012:layer:my-layer:1"
],
MemorySize: 128,
Timeout: 5
});

This example configures the Lambda function to enable AWS X-Ray tracing for monitoring.

const lambdaWithTracing = await AWS.Lambda.Function("lambdaWithTracing", {
FunctionName: "lambdaWithTracingFunction",
Handler: "index.handler",
Runtime: "nodejs14.x",
Role: "arn:aws:iam::123456789012:role/execution_role",
Code: {
ZipFile: "exports.handler = async (event) => { return 'Tracing enabled!'; };"
},
TracingConfig: {
Mode: "Active"
},
MemorySize: 128,
Timeout: 10
});