Loading...
Loading...
AWS Lambda serverless functions for event-driven compute. Use when creating functions, configuring triggers, debugging invocations, optimizing cold starts, setting up event source mappings, or managing layers.
npx skill4agent add itsmostafa/aws-agent-skills lambda| Type | Description | Use Case |
|---|---|---|
| Synchronous | Caller waits for response | API Gateway, direct invoke |
| Asynchronous | Fire and forget | S3, SNS, EventBridge |
| Poll-based | Lambda polls source | SQS, Kinesis, DynamoDB Streams |
# Create deployment package
zip function.zip lambda_function.py
# Create function
aws lambda create-function \
--function-name MyFunction \
--runtime python3.12 \
--role arn:aws:iam::123456789012:role/lambda-role \
--handler lambda_function.handler \
--zip-file fileb://function.zip \
--timeout 30 \
--memory-size 256
# Update function code
aws lambda update-function-code \
--function-name MyFunction \
--zip-file fileb://function.zipimport boto3
import zipfile
import io
lambda_client = boto3.client('lambda')
# Create zip in memory
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zf:
zf.writestr('lambda_function.py', '''
def handler(event, context):
return {"statusCode": 200, "body": "Hello"}
''')
zip_buffer.seek(0)
# Create function
lambda_client.create_function(
FunctionName='MyFunction',
Runtime='python3.12',
Role='arn:aws:iam::123456789012:role/lambda-role',
Handler='lambda_function.handler',
Code={'ZipFile': zip_buffer.read()},
Timeout=30,
MemorySize=256
)# Add permission for S3 to invoke Lambda
aws lambda add-permission \
--function-name MyFunction \
--statement-id s3-trigger \
--action lambda:InvokeFunction \
--principal s3.amazonaws.com \
--source-arn arn:aws:s3:::my-bucket \
--source-account 123456789012
# Configure S3 notification (see S3 skill)aws lambda create-event-source-mapping \
--function-name MyFunction \
--event-source-arn arn:aws:sqs:us-east-1:123456789012:my-queue \
--batch-size 10 \
--maximum-batching-window-in-seconds 5aws lambda update-function-configuration \
--function-name MyFunction \
--environment "Variables={DB_HOST=mydb.cluster-xyz.us-east-1.rds.amazonaws.com,LOG_LEVEL=INFO}"# Create layer
zip -r layer.zip python/
aws lambda publish-layer-version \
--layer-name my-dependencies \
--compatible-runtimes python3.12 \
--zip-file fileb://layer.zip
# Attach to function
aws lambda update-function-configuration \
--function-name MyFunction \
--layers arn:aws:lambda:us-east-1:123456789012:layer:my-dependencies:1# Synchronous invoke
aws lambda invoke \
--function-name MyFunction \
--payload '{"key": "value"}' \
response.json
# Asynchronous invoke
aws lambda invoke \
--function-name MyFunction \
--invocation-type Event \
--payload '{"key": "value"}' \
response.json| Command | Description |
|---|---|
| Create new function |
| Update function code |
| Update settings |
| Delete function |
| List all functions |
| Get function details |
| Command | Description |
|---|---|
| Invoke function |
| Async invoke (deprecated) |
| Command | Description |
|---|---|
| Add event source |
| List mappings |
| Update mapping |
| Remove mapping |
| Command | Description |
|---|---|
| Add resource-based policy |
| Remove permission |
| View resource policy |
# GOOD: Initialize outside handler
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('MyTable')
def handler(event, context):
# Reuses existing connection
return table.get_item(Key={'id': event['id']})Task timed out after X seconds# Check function configuration
aws lambda get-function-configuration \
--function-name MyFunction \
--query "Timeout"
# Increase timeout
aws lambda update-function-configuration \
--function-name MyFunction \
--timeout 60aws lambda update-function-configuration \
--function-name MyFunction \
--memory-size 512# Enable Provisioned Concurrency
aws lambda put-provisioned-concurrency-config \
--function-name MyFunction \
--qualifier LIVE \
--provisioned-concurrent-executions 5AccessDeniedException# Check execution role
aws lambda get-function-configuration \
--function-name MyFunction \
--query "Role"
# Check role policies
aws iam list-attached-role-policies \
--role-name lambda-role