Lambda Function: Configuration
Understand how to configure AWS Lambda functions by setting environment variables in serverless.yml for dynamic access to DynamoDB table names. Learn to assign least privilege IAM roles allowing specific DynamoDB actions to secure function permissions, enabling effective integration and deployment.
We'll cover the following...
We've created the DynamoDB table to store the inventory information. We also have the logic for the Lambda handler. Now, let's add the Lambda function to our serverless.yml.
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
export const hello = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
return {
statusCode: 200,
body: JSON.stringify({
message: "Welcome to the Sweet Factory!",
input: event,
}),
};
};
In the code below, in our hello function (within the functions section), we'll add the ingredientPreparation function declaration with a link to the handler, and we'll add an environment variable with INVENTORY_TABLE :
In the given configuration, we use the environment property to set environment variables for the Lambda function. By using environment variables, we can easily manage and access configuration values within the function without hardcoding them. Hardcoding values is generally not a good idea. This is because it can lead to ...