Aws serverless

From j4flmao/agent-skills · View interactive version

Aws serverless is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about AWS Lambda internals, Firecracker microVMs, the execution environment lifecycle, concurrency limits, event source mappings, function URLs, layers, or Lambda timeouts. Produces correct Lambda function design and configuration. Not for cold-start tuning (use cloud-native-s

How to use this skill

Skill file

AWS Lambda Internals and Function Design

Purpose

Explain what actually happens when a Lambda function is invoked — Firecracker microVM isolation, the execution environment lifecycle, and the concurrency model — so that function code, timeouts, memory, and event-source settings are chosen from mechanics rather than folklore.

Agent Protocol

Trigger

"aws lambda internals", "firecracker", "lambda execution environment", "lambda concurrency", "event source mapping", "lambda function url", "lambda layers", "reserved concurrency", "lambda timeout".

Input Context

Output Artifact

Function configuration (IaC snippet) plus handler structure. No file unless requested.

Response Format

Invocation model: {sync|async|poll-based (ESM)}
Memory / timeout: {MB} / {s}  (why)
Concurrency: {unreserved|reserved=N|provisioned=N}
Retries / failure destination: {...}
Handler notes: {init vs handler work}

Completion Criteria

How an Invocation Runs

  1. Firecracker microVM. Lambda runs each execution environment inside a Firecracker microVM on KVM. Firecracker exposes a minimal device model (virtio-net, virtio-block, serial console, keyboard for reset) and is documented to boot in about 125 ms with under 5 MiB memory overhead per microVM. Isolation boundary = the VM, not a container.
  2. Execution environment lifecycle (Lambda Runtime API phases):
  1. Reuse. A warm environment is reused for later invocations; globals, /tmp (512 MB default, configurable up to 10,240 MB), and open sockets survive. Never rely on reuse for correctness.
flowchart LR
  Req[Invoke] --> FE[Frontend / Invoke service]
  FE -->|warm env available| Env[Execution env in Firecracker microVM]
  FE -->|none available| Init[INIT: runtime + module code] --> Env
  Env --> Resp[Response]

Concurrency Model

| Source | Invocation | Retries on error | Failure handling |

|---|---|---|---|

| API Gateway, Function URL, SDK RequestResponse | Sync | None (caller retries) | Return error to caller |

| S3, SNS, EventBridge, SDK Event | Async (internal queue) | 2 retries by default (0–2), max event age up to 6 h | On-failure destination or DLQ |

| SQS | Poll (ESM) | Until visibility timeout/maxReceiveCount | Queue redrive DLQ, ReportBatchItemFailures |

| Kinesis / DynamoDB Streams | Poll (ESM) | Until record expires unless limited | MaximumRetryAttempts, BisectBatchOnFunctionError, on-failure destination |

Function Configuration (Terraform)

resource "aws_lambda_function" "orders" {
  function_name = "orders-api"
  role          = aws_iam_role.orders.arn
  runtime       = "python3.12"
  handler       = "app.handler"
  filename      = "build/orders.zip"
  source_code_hash = filebase64sha256("build/orders.zip")
  architectures = ["arm64"]          # Graviton: usually cheaper per GB-s
  memory_size   = 1024               # CPU scales with memory (1 vCPU ≈ 1,769 MB)
  timeout       = 25                 # below API Gateway's 29 s integration timeout
  reserved_concurrent_executions = 200 # protects the database

  environment {
    variables = { TABLE_NAME = aws_dynamodb_table.orders.name }
  }
  tracing_config { mode = "Active" }
}

resource "aws_lambda_event_source_mapping" "queue" {
  event_source_arn = aws_sqs_queue.jobs.arn
  function_name    = aws_lambda_function.worker.arn
  batch_size       = 10
  function_response_types = ["ReportBatchItemFailures"]
  scaling_config { maximum_concurrency = 50 } # SQS-only cap, 2..1000
}

Handler Structure

import json, os
import boto3

# INIT phase: runs once per execution environment, reused while warm
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(os.environ["TABLE_NAME"])

def handler(event, context):
    failures = []
    for record in event.get("Records", []):
        try:
            body = json.loads(record["body"])
            table.put_item(
                Item={"pk": body["order_id"], "status": "RECEIVED"},
                ConditionExpression="attribute_not_exists(pk)",  # idempotent
            )
        except dynamodb.meta.client.exceptions.ConditionalCheckFailedException:
            continue  # duplicate delivery: already processed
        except Exception:
            failures.append({"itemIdentifier": record["messageId"]})
    return {"batchItemFailures": failures}

Decision Rules

Anti-Patterns

| Anti-pattern | Why it hurts | Fix |

|---|---|---|

| Lambda calling Lambda synchronously | Double billing, cascading timeouts | Step Functions or async events |

| Recursive S3 trigger writing to same bucket/prefix | Infinite loop (Lambda recursion detection stops some loops, don't rely on it) | Separate output prefix/bucket |

| Unbounded concurrency to a fragile dependency | Throttles/outages downstream | Reserved concurrency or ESM maximum_concurrency |

| Secrets in plain environment variables | Visible to anyone with GetFunctionConfiguration | Secrets Manager / Parameter Store with caching extension |

| Non-idempotent handlers | At-least-once delivery creates duplicates | Conditional writes, idempotency keys (Powertools Idempotency) |

Checklist

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.