$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-lambdaProvides AWS CloudFormation patterns for Lambda functions, layers, API Gateway integration, event sources, cold start optimization, monitoring, logging, template validation, and deployment workflows. Use when creating Lambda functions with CloudFormation, configuring event source
| 1 | # AWS CloudFormation Lambda Functions |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Create production-ready Lambda functions using CloudFormation templates with validation and deployment workflows. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Creating Lambda functions with CloudFormation |
| 10 | - Configuring event sources (S3, SQS, DynamoDB, Kinesis) |
| 11 | - Implementing Lambda layers and cold start optimization |
| 12 | - Integrating Lambda with API Gateway |
| 13 | - Deploying Lambda infrastructure with validation |
| 14 | |
| 15 | ## Deployment Workflow |
| 16 | |
| 17 | Always follow this deployment workflow: |
| 18 | |
| 19 | ### 1. Validate Template |
| 20 | ```bash |
| 21 | aws cloudformation validate-template --template-body file://template.yaml |
| 22 | ``` |
| 23 | |
| 24 | ### 2. Deploy Stack |
| 25 | ```bash |
| 26 | aws cloudformation deploy \ |
| 27 | --template-file template.yaml \ |
| 28 | --stack-name my-lambda-stack \ |
| 29 | --capabilities CAPABILITY_IAM \ |
| 30 | --parameter-overrides Environment=prod |
| 31 | ``` |
| 32 | |
| 33 | ### 3. Monitor Stack Events |
| 34 | ```bash |
| 35 | aws cloudformation describe-stack-events \ |
| 36 | --stack-name my-lambda-stack \ |
| 37 | --query 'StackEvents[?ResourceStatus==`CREATE_FAILED`||ResourceStatus==`UPDATE_FAILED`]' |
| 38 | ``` |
| 39 | |
| 40 | ### 4. Verify Resources |
| 41 | ```bash |
| 42 | aws lambda get-function --function-name my-lambda-stack-function |
| 43 | aws cloudformation describe-stacks --stack-name my-lambda-stack \ |
| 44 | --query 'Stacks[0].StackStatus' |
| 45 | ``` |
| 46 | |
| 47 | ### 5. Rollback on Failure |
| 48 | ```bash |
| 49 | aws cloudformation delete-stack --stack-name my-lambda-stack |
| 50 | aws logs describe-log-groups --log-group-name-prefix "/aws/lambda/my-lambda" |
| 51 | ``` |
| 52 | |
| 53 | ## Instructions |
| 54 | |
| 55 | Follow these steps to create Lambda functions with CloudFormation: |
| 56 | |
| 57 | ### 1. Define Lambda Function Parameters |
| 58 | |
| 59 | Specify runtime, memory, timeout, and environment variables: |
| 60 | |
| 61 | ```yaml |
| 62 | Parameters: |
| 63 | FunctionMemory: |
| 64 | Type: Number |
| 65 | Default: 256 |
| 66 | AllowedValues: |
| 67 | - 128 |
| 68 | - 256 |
| 69 | - 512 |
| 70 | - 1024 |
| 71 | - 2048 |
| 72 | Description: Lambda function memory in MB |
| 73 | |
| 74 | FunctionTimeout: |
| 75 | Type: Number |
| 76 | Default: 30 |
| 77 | MinValue: 1 |
| 78 | MaxValue: 900 |
| 79 | Description: Function timeout in seconds |
| 80 | |
| 81 | Runtime: |
| 82 | Type: String |
| 83 | Default: nodejs20.x |
| 84 | AllowedValues: |
| 85 | - nodejs20.x |
| 86 | - python3.11 |
| 87 | - java21 |
| 88 | - dotnet8 |
| 89 | - go1.x |
| 90 | Description: Lambda runtime environment |
| 91 | ``` |
| 92 | |
| 93 | ### 2. Create Lambda Function |
| 94 | |
| 95 | Define the basic function configuration: |
| 96 | |
| 97 | ```yaml |
| 98 | Resources: |
| 99 | LambdaFunction: |
| 100 | Type: AWS::Lambda::Function |
| 101 | Properties: |
| 102 | FunctionName: !Sub "${AWS::StackName}-function" |
| 103 | Runtime: !Ref Runtime |
| 104 | Handler: index.handler |
| 105 | Role: !Ref ExecutionRole |
| 106 | MemorySize: !Ref FunctionMemory |
| 107 | Timeout: !Ref FunctionTimeout |
| 108 | Code: |
| 109 | S3Bucket: !Ref CodeBucket |
| 110 | S3Key: !Ref CodeKey |
| 111 | Environment: |
| 112 | Variables: |
| 113 | LOG_LEVEL: INFO |
| 114 | DATABASE_URL: !Ref DatabaseUrl |
| 115 | Tags: |
| 116 | - Key: Environment |
| 117 | Value: !Ref Environment |
| 118 | ``` |
| 119 | |
| 120 | ### 3. Configure Execution Role |
| 121 | |
| 122 | Apply least privilege IAM policies: |
| 123 | |
| 124 | ```yaml |
| 125 | Resources: |
| 126 | ExecutionRole: |
| 127 | Type: AWS::IAM::Role |
| 128 | Properties: |
| 129 | AssumeRolePolicyDocument: |
| 130 | Version: "2012-10-17" |
| 131 | Statement: |
| 132 | - Effect: Allow |
| 133 | Principal: |
| 134 | Service: lambda.amazonaws.com |
| 135 | Action: sts:AssumeRole |
| 136 | ManagedPolicyArns: |
| 137 | - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole |
| 138 | Policies: |
| 139 | - PolicyName: S3ReadAccess |
| 140 | PolicyDocument: |
| 141 | Version: "2012-10-17" |
| 142 | Statement: |
| 143 | - Effect: Allow |
| 144 | Action: |
| 145 | - s3:GetObject |
| 146 | Resource: !Sub "${DataBucket.Arn}/*" |
| 147 | ``` |
| 148 | |
| 149 | ### 4. Add Event Sources |
| 150 | |
| 151 | Configure triggers for Lambda invocation: |
| 152 | |
| 153 | ```yaml |
| 154 | Resources: |
| 155 | # S3 event source |
| 156 | S3EventSource: |
| 157 | Type: AWS::Lambda::EventSourceMapping |
| 158 | Properties: |
| 159 | EventSourceArn: !GetAtt DataBucket.Arn |
| 160 | FunctionName: !Ref LambdaFunction |
| 161 | |
| 162 | # SQS event source |
| 163 | SQSEventSource: |
| 164 | Type: AWS::Lambda::EventSourceMapping |
| 165 | Properties: |
| 166 | EventSourceArn: !GetAtt Queue.Arn |
| 167 | FunctionName: !Ref LambdaFunction |
| 168 | BatchSize: 10 |
| 169 | MaximumBatchingWindowInSeconds: 5 |
| 170 | ``` |
| 171 | |
| 172 | ### 5. Configure API Gateway Integration |
| 173 | |
| 174 | Set up REST or HTTP API integration: |
| 175 | |
| 176 | ```yaml |
| 177 | Resources: |
| 178 | # HTTP API integration |
| 179 | HttpApi: |
| 180 | Type: AWS::ApiGatewayV2::Api |
| 181 | Properties: |
| 182 | Name: !Sub "${AWS::StackName}-api" |
| 183 | ProtocolType: HTTP |
| 184 | Target: !Ref LambdaFunction |
| 185 | |
| 186 | ApiIntegration: |
| 187 | Type: AWS::ApiGatewayV2::Integration |
| 188 | Properties: |
| 189 | ApiId: !Ref HttpApi |
| 190 | IntegrationType: AWS_PROXY |
| 191 | IntegrationUri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/f |