Lambda
Lambda Log Retention
Resources:
Function:
Type: AWS::Serverless::Function
Properties: ...
LogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${Function}"
RetentionInDays: 7
It looks like this won’t be changed any time soon: GitHub Issue
Typescript Lambda (API Gateway Proxy)
Typescript boilerplate
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
export const lambdaHandler = async (
event: APIGatewayProxyEvent,
): Promise<APIGatewayProxyResult> => {
// ...
};
SAM CloudFormation Template
Resources:
GetItemByIdFunction:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: es2020
EntryPoints:
- app.ts
External: # to be found in the dependency layer, will not be bundled
- "@aws-sdk/client-dynamodb"
- "@aws-sdk/lib-dynamodb"
Properties:
CodeUri: functions/getItemById
Description: Get an item by id from a DynamoDB table
Environment:
Variables:
SAMPLE_TABLE: !Ref SampleTable
Handler: app.lambdaHandler
Layers:
- !Ref DependencyLayer
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref SampleTable
Events:
Api:
Type: Api
Properties:
Path: /{id}
Method: GET
EventBridge Event type definition
Example of an EventBridge event type definition for an S3 Object Created event (the documentation currently lacks on this topic)
import {
EventBridgeEvent,
S3ObjectCreatedNotificationEventDetail,
} from "aws-lambda";
export const handler = async (
event: EventBridgeEvent<
"Object Created",
S3ObjectCreatedNotificationEventDetail
>,
): Promise<void> => {
console.log("event: ", JSON.stringify(event, null, 2));
};
CloudFront Functions
CloudFront Functions’ JavaScript runtime does not support exports, here is a simple workaround:
async function handler(event) {
// ...
}
var module = module || {};
module.exports = { handler };
Elastic Load Balancer Target Group naming
If an update triggers a replacement of a Load Balancer, it could error with
Target group 'arn:aws:...' cannot be associated with more than one load balancer
To avoid this issue, the name of the target group could contain the name of the load balancer.