SQS, SNS, Kinesis, ECS, EKS, and ECR commands for production messaging, streaming, and container workloads.
Create a standard queue with Dead Letter Queue.
## Create Dead Letter Queue first
aws sqs create-queue \
--queue-name prod-orders-dlq \
--attributes '{
"MessageRetentionPeriod": "1209600"
}' \
--region ap-south-1
## Get DLQ ARN
aws sqs get-queue-attributes \
--queue-url https://sqs.ap-south-1.amazonaws.com/123456789012/prod-orders-dlq \
--attribute-names QueueArn \
--region ap-south-1
## Create main queue with DLQ and long polling
aws sqs create-queue \
--queue-name prod-orders \
--attributes '{
"VisibilityTimeout": "180",
"ReceiveMessageWaitTimeSeconds": "20",
"MessageRetentionPeriod": "345600",
"RedrivePolicy": "{
\"deadLetterTargetArn\":
\"arn:aws:sqs:ap-south-1:123456789012:prod-orders-dlq\",
\"maxReceiveCount\":\"3\"
}"
}' \
--region ap-south-1
Parameter Breakdown:
VisibilityTimeout 180: Consumer has 3 minutes β set to 6Γ Lambda timeoutReceiveMessageWaitTimeSeconds 20: Long polling β waits up to 20s for messagesmaxReceiveCount 3: After 3 failures, message moves to DLQ automatically## Send a message with attributes
aws sqs send-message \
--queue-url https://sqs.ap-south-1.amazonaws.com/123456789012/prod-orders \
--message-body '{"orderId":"ORD-2024-98765","userId":"user-12345","amount":450}' \
--message-attributes '{
"Source": {
"DataType": "String",
"StringValue": "mobile-app"
}
}' \
--region ap-south-1
## Receive and delete messages
aws sqs receive-message \
--queue-url https://sqs.ap-south-1.amazonaws.com/123456789012/prod-orders \
--max-number-of-messages 10 \
--wait-time-seconds 20 \
--region ap-south-1
## Delete after processing (mandatory)
aws sqs delete-message \
--queue-url https://sqs.ap-south-1.amazonaws.com/123456789012/prod-orders \
--receipt-handle "RECEIPT-HANDLE-FROM-RECEIVE" \
--region ap-south-1
Parameter Breakdown:
--max-number-of-messages 10: Receive up to 10 at once (SQS maximum per call)--wait-time-seconds 20: Long polling β do not use 0, wastes API callsCreate SNS topic with SQS fan-out to multiple queues.
## Create SNS topic
aws sns create-topic \
--name prod-order-events \
--region ap-south-1
## Subscribe SQS queues to SNS topic
aws sns subscribe \
--topic-arn arn:aws:sns:ap-south-1:123456789012:prod-order-events \
--protocol sqs \
--notification-endpoint \
arn:aws:sqs:ap-south-1:123456789012:prod-fraud-queue \
--region ap-south-1
## Add filter policy (fraud queue: high-value orders only)
aws sns set-subscription-attributes \
--subscription-arn arn:aws:sns:ap-south-1:123456789012:prod-order-events:SUBID \
--attribute-name FilterPolicy \
--attribute-value '{"amount": [{"numeric": [">=", 2000]}]}' \
--region ap-south-1
## Publish event to topic
aws sns publish \
--topic-arn arn:aws:sns:ap-south-1:123456789012:prod-order-events \
--message '{"orderId":"ORD-98765","amount":4500,"status":"placed"}' \
--message-attributes '{
"amount": {"DataType": "Number","StringValue": "4500"}
}' \
--region ap-south-1
Parameter Breakdown:
Create a Kinesis stream and produce/consume records.
## Create stream with 5 shards (5 MB/s ingest)
aws kinesis create-stream \
--stream-name prod-clickstream \
--shard-count 5 \
--region ap-south-1
## Put record with partition key (routes to same shard)
aws kinesis put-record \
--stream-name prod-clickstream \
--partition-key "user-98765" \
--data '{"userId":"user-98765","event":"click","page":"/checkout","ts":1704067200}' \
--region ap-south-1
## Get shard iterator for consumer
aws kinesis get-shard-iterator \
--stream-name prod-clickstream \
--shard-id shardId-000000000000 \
--shard-iterator-type LATEST \
--region ap-south-1
## Scale stream shards up
aws kinesis update-shard-count \
--stream-name prod-clickstream \
--target-shard-count 10 \
--scaling-type UNIFORM_SCALING \
--region ap-south-1
Parameter Breakdown:
--partition-key "user-98765": Same partition key β same shard β ordered per userSHARD_ITERATOR_TYPE LATEST: Read only new records (use TRIM_HORIZON to replay all)## Authenticate Docker to ECR (12-hour token)
aws ecr get-login-password --region ap-south-1 | \
docker login \
--username AWS \
--password-stdin \
123456789012.dkr.ecr.ap-south-1.amazonaws.com
## Create repository with scan on push
aws ecr create-repository \
--repository-name prod/orders-api \
--image-tag-mutability IMMUTABLE \
--image-scanning-configuration scanOnPush=true \
--region ap-south-1
## Build, tag, and push
docker build -t orders-api:v2.1 .
docker tag orders-api:v2.1 \
123456789012.dkr.ecr.ap-south-1.amazonaws.com/prod/orders-api:v2.1
docker push \
123456789012.dkr.ecr.ap-south-1.amazonaws.com/prod/orders-api:v2.1
## Set lifecycle policy (keep last 10 images)
aws ecr put-lifecycle-policy \
--repository-name prod/orders-api \
--lifecycle-policy-text '{
"rules": [{
"rulePriority": 1,
"description": "Keep 10 most recent",
"selection": {
"tagStatus": "any",
"countType": "imageCountMoreThan",
"countNumber": 10
},
"action": {"type": "expire"}
}]
}' \
--region ap-south-1
Parameter Breakdown:
IMMUTABLE: Prevents overwriting a tag β v2.1 always refers to the same imagescanOnPush true: Vulnerability scan on every push β check results before deployingDeploy an ECS service with ALB and Auto Scaling.
## Register Task Definition
aws ecs register-task-definition \
--family prod-orders-api \
--requires-compatibilities FARGATE \
--network-mode awsvpc \
--cpu 512 --memory 1024 \
--execution-role-arn \
arn:aws:iam::123456789012:role/ecsTaskExecutionRole \
--task-role-arn \
arn:aws:iam::123456789012:role/orders-task-role \
--container-definitions '[{
"name": "orders-api",
"image": "123456789012.dkr.ecr.ap-south-1.amazonaws.com/prod/orders-api:v2.1",
"portMappings": [{"containerPort": 8080}],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/prod-orders-api",
"awslogs-region": "ap-south-1",
"awslogs-stream-prefix": "orders"
}
}
}]' \
--region ap-south-1
## Create ECS Service
aws ecs create-service \
--cluster prod-cluster \
--service-name prod-orders-api \
--task-definition prod-orders-api:1 \
--desired-count 3 \
--launch-type FARGATE \
--network-configuration '{
"awsvpcConfiguration": {
"subnets":["subnet-private-1a","subnet-private-1b"],
"securityGroups":["sg-ecs-tasks"],
"assignPublicIp":"DISABLED"
}
}' \
--load-balancers '[{
"targetGroupArn": "arn:aws:elasticloadbalancing:...",
"containerName": "orders-api",
"containerPort": 8080
}]' \
--region ap-south-1
## Register ECS Service Auto Scaling
aws application-autoscaling register-scalable-target \
--service-namespace ecs \
--resource-id service/prod-cluster/prod-orders-api \
--scalable-dimension ecs:service:DesiredCount \
--min-capacity 2 \
--max-capacity 20 \
--region ap-south-1
Parameter Breakdown:
execution-role-arn: ECS Agent uses this to pull image and write logs β not your apptask-role-arn: Your app code uses this to access S3, DynamoDB, SQSassignPublicIp DISABLED: Fargate tasks must be in private subnets| Need | SQS | SNS | Kinesis |
|---|---|---|---|
| One consumer per message | Yes | No | No |
| All subscribers get message | No | Yes | No |
| Replay past messages | No | No | Yes |
| Order guaranteed | FIFO only | No | Per partition key |
| Max throughput | Unlimited | Unlimited | Per shard limit |