DynamoDB Streams와 Time-to-Live 다루기

DynamoDB Streams와 Time-to-Live 다루기 (AWS CLI v2)

이 코드 예제에서는 AWS CLI를 사용해 DynamoDB Streams와 Time-to-Live(TTL) 기능을 관리하는 방법을 보여드려요.

출처: 문서

본문

다음 작업을 다루어요.

  • Streams가 활성화된 테이블 생성
  • Streams 설명(Describe)
  • Streams 처리용 Lambda 함수 생성
  • 테이블 TTL 활성화
  • TTL 속성이 있는 항목 추가
  • TTL 설정 설명

Bash (AWS CLI with Bash script)

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

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

Streams 처리용 Lambda 함수를 만들어요.

# 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"

테이블에 TTL을 활성화해요.

# 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"

TTL 속성이 있는 항목을 추가해요.

# 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'"}
	}'

TTL 설정을 설명해요.

# Describe TTL settings for a table
aws dynamodb describe-time-to-live \
	--table-name TTLDemo

API 상세는 AWS CLI Command Reference의 다음 항목을 참고해요.

  • AttachRolePolicy, CreateRole, CreateTable, DescribeTable, DescribeTimeToLive, PutItem, UpdateTimeToLive

더 알아보기 (Learn more)