$npx -y skills add tranhieutt/software_development_department --skill aws-serverlessProvides AWS serverless architecture patterns for Lambda, API Gateway, DynamoDB, SQS, and SAM/CDK. Use when working with AWS serverless files (serverless.yml, CDK stacks) or when the user mentions Lambda, API Gateway, serverless, or AWS SAM.
| 1 | # AWS Serverless |
| 2 | |
| 3 | ## Critical rules (non-obvious) |
| 4 | |
| 5 | - **Initialize clients OUTSIDE handler** — Lambda reuses execution environments across invocations; creating clients inside costs 100-500ms per cold start |
| 6 | - **`context.callbackWaitsForEmptyEventLoop = false`** — prevents Node.js from hanging on open async handles (DB connections, etc.) |
| 7 | - **SQS `VisibilityTimeout` = 6× Lambda timeout** — if Lambda takes 30s, set 180s; otherwise messages return to queue mid-processing |
| 8 | - **`FunctionResponseTypes: [ReportBatchItemFailures]`** — partial batch failure; without this, any single failure retries the entire batch |
| 9 | - **Never use `*` in `Access-Control-Allow-Origin` with `credentials: true`** — browsers block it; use explicit origin |
| 10 | |
| 11 | ## Lambda handler pattern |
| 12 | |
| 13 | ```javascript |
| 14 | // Initialize once (reused across invocations = faster after cold start) |
| 15 | const { DynamoDBClient } = require("@aws-sdk/client-dynamodb"); |
| 16 | const { DynamoDBDocumentClient, GetCommand } = require("@aws-sdk/lib-dynamodb"); |
| 17 | |
| 18 | const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({})); |
| 19 | |
| 20 | exports.handler = async (event, context) => { |
| 21 | context.callbackWaitsForEmptyEventLoop = false; // don't hang on open handles |
| 22 | try { |
| 23 | const body = typeof event.body === "string" ? JSON.parse(event.body) : event.body; |
| 24 | const result = await docClient.send(new GetCommand({ |
| 25 | TableName: process.env.TABLE_NAME, |
| 26 | Key: { id: body.id }, |
| 27 | })); |
| 28 | return { statusCode: 200, headers: { "Content-Type": "application/json" }, body: JSON.stringify(result.Item) }; |
| 29 | } catch (err) { |
| 30 | console.error(JSON.stringify({ error: err.message, requestId: context.awsRequestId })); |
| 31 | return { statusCode: err.statusCode ?? 500, body: JSON.stringify({ error: err.message }) }; |
| 32 | } |
| 33 | }; |
| 34 | ``` |
| 35 | |
| 36 | ## SAM template: HTTP API + DynamoDB |
| 37 | |
| 38 | ```yaml |
| 39 | # template.yaml |
| 40 | AWSTemplateFormatVersion: "2010-09-09" |
| 41 | Transform: AWS::Serverless-2016-10-31 |
| 42 | |
| 43 | Globals: |
| 44 | Function: |
| 45 | Runtime: nodejs20.x |
| 46 | Timeout: 30 |
| 47 | MemorySize: 256 |
| 48 | Environment: |
| 49 | Variables: |
| 50 | TABLE_NAME: !Ref ItemsTable |
| 51 | |
| 52 | Resources: |
| 53 | HttpApi: |
| 54 | Type: AWS::Serverless::HttpApi |
| 55 | Properties: |
| 56 | CorsConfiguration: |
| 57 | AllowOrigins: ["https://yourdomain.com"] # never * with credentials |
| 58 | AllowMethods: [GET, POST, DELETE] |
| 59 | AllowHeaders: ["*"] |
| 60 | |
| 61 | GetItemFunction: |
| 62 | Type: AWS::Serverless::Function |
| 63 | Properties: |
| 64 | Handler: src/handlers/get.handler |
| 65 | Events: |
| 66 | GetItem: |
| 67 | Type: HttpApi |
| 68 | Properties: |
| 69 | ApiId: !Ref HttpApi |
| 70 | Path: /items/{id} |
| 71 | Method: GET |
| 72 | Policies: |
| 73 | - DynamoDBReadPolicy: |
| 74 | TableName: !Ref ItemsTable |
| 75 | |
| 76 | ItemsTable: |
| 77 | Type: AWS::DynamoDB::Table |
| 78 | Properties: |
| 79 | AttributeDefinitions: |
| 80 | - AttributeName: id |
| 81 | AttributeType: S |
| 82 | KeySchema: |
| 83 | - AttributeName: id |
| 84 | KeyType: HASH |
| 85 | BillingMode: PAY_PER_REQUEST |
| 86 | |
| 87 | Outputs: |
| 88 | ApiUrl: |
| 89 | Value: !Sub "https://${HttpApi}.execute-api.${AWS::Region}.amazonaws.com" |
| 90 | ``` |
| 91 | |
| 92 | ## SQS async processing with partial batch failure |
| 93 | |
| 94 | ```yaml |
| 95 | # In template.yaml |
| 96 | ProcessorFunction: |
| 97 | Type: AWS::Serverless::Function |
| 98 | Properties: |
| 99 | Events: |
| 100 | SQSEvent: |
| 101 | Type: SQS |
| 102 | Properties: |
| 103 | Queue: !GetAtt ProcessingQueue.Arn |
| 104 | BatchSize: 10 |
| 105 | FunctionResponseTypes: |
| 106 | - ReportBatchItemFailures # critical: retry only failed items |
| 107 | |
| 108 | ProcessingQueue: |
| 109 | Type: AWS::SQS::Queue |
| 110 | Properties: |
| 111 | VisibilityTimeout: 180 # 6x Lambda timeout (30s) |
| 112 | RedrivePolicy: |
| 113 | deadLetterTargetArn: !GetAtt DeadLetterQueue.Arn |
| 114 | maxReceiveCount: 3 |
| 115 | |
| 116 | DeadLetterQueue: |
| 117 | Type: AWS::SQS::Queue |
| 118 | Properties: |
| 119 | MessageRetentionPeriod: 1209600 # 14 days |
| 120 | ``` |
| 121 | |
| 122 | ```javascript |
| 123 | // Handler with partial batch failure reporting |
| 124 | exports.handler = async (event) => { |
| 125 | const batchItemFailures = []; |
| 126 | for (const record of event.Records) { |
| 127 | try { |
| 128 | await processMessage(JSON.parse(record.body)); |
| 129 | } catch (err) { |
| 130 | console.error(`Failed ${record.messageId}:`, err.message); |
| 131 | batchItemFailures.push({ itemIdentifier: record.messageId }); |
| 132 | } |
| 133 | } |
| 134 | return { batchItemFailures }; // only failed items are retried |
| 135 | }; |
| 136 | ``` |
| 137 | |
| 138 | ## Sharp edges |
| 139 | |
| 140 | | Issue | Severity | Fix | |
| 141 | |---|---|---| |
| 142 | | Cold start > 1s | High | Move SDK init outside handler; use `--no-install-suggests` in Docker layers | |
| 143 | | Timeout without response | High | Always set explicit timeout < Lambda timeout in downstream ca |