Erstellen Sie eine Tabelle mit aktivierten Streams.
# Create a table with DynamoDB Streams enabled
aws dynamodb create-table \
--table-name StreamsDemo \
--attribute-definitions \
AttributeName=ID,AttributeType=S \
--key-schema \
AttributeName=ID,KeyType=HASH \
--billing-mode PAY_PER_REQUEST \
--stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES
Beschreiben Sie Streams.
# Get information about the stream
aws dynamodb describe-table \
--table-name StreamsDemo \
--query "Table.StreamSpecification"
# Get the stream ARN
STREAM_ARN=$(aws dynamodb describe-table \
--table-name StreamsDemo \
--query "Table.LatestStreamArn" \
--output text)
echo "Stream ARN: $STREAM_ARN"
# Describe the stream
aws dynamodbstreams describe-stream \
--stream-arn $STREAM_ARN
Erstellen Sie eine Lambda-Funktion für Streams.
# Step 1: Create an IAM role for the Lambda function
cat > trust-policy.json << 'EOF'
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
aws iam create-role \
--role-name DynamoDBStreamsLambdaRole \
--assume-role-policy-document file://trust-policy.json
# Step 2: Attach permissions to the role
aws iam attach-role-policy \
--role-name DynamoDBStreamsLambdaRole \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaDynamoDBExecutionRole
# Step 3: Create a Lambda function (code would be in a separate file)
echo "Lambda function creation would be done separately with appropriate code"
# Step 4: Create an event source mapping
echo "Example command to create event source mapping:"
echo "aws lambda create-event-source-mapping \\"
echo " --function-name ProcessDynamoDBRecords \\"
echo " --event-source $STREAM_ARN \\"
echo " --batch-size 100 \\"
echo " --starting-position LATEST"
Aktivieren Sie TTL für eine Tabelle.
# Create a table for TTL demonstration
aws dynamodb create-table \
--table-name TTLDemo \
--attribute-definitions \
AttributeName=ID,AttributeType=S \
--key-schema \
AttributeName=ID,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
# Wait for table to become active
aws dynamodb wait table-exists --table-name TTLDemo
# Enable TTL on the table
aws dynamodb update-time-to-live \
--table-name TTLDemo \
--time-to-live-specification "Enabled=true, AttributeName=ExpirationTime"
Fügen Sie Elemente mit TTL-Attributen hinzu.
# Calculate expiration time (current time + 1 day in seconds)
EXPIRATION_TIME=$(date -d "+1 day" +%s)
# Add an item with TTL attribute
aws dynamodb put-item \
--table-name TTLDemo \
--item '{
"ID": {"S": "item1"},
"Data": {"S": "This item will expire in 1 day"},
"ExpirationTime": {"N": "'$EXPIRATION_TIME'"}
}'
# Add an item that expires in 1 hour
EXPIRATION_TIME_HOUR=$(date -d "+1 hour" +%s)
aws dynamodb put-item \
--table-name TTLDemo \
--item '{
"ID": {"S": "item2"},
"Data": {"S": "This item will expire in 1 hour"},
"ExpirationTime": {"N": "'$EXPIRATION_TIME_HOUR'"}
}'
Beschreiben Sie die TTL-Einstellungen.
# Describe TTL settings for a table
aws dynamodb describe-time-to-live \
--table-name TTLDemo