Hi there 👋
In the last blog I went through steps to run localstack on your machine. LocalStack: The Ultimate Tool for Local AWS Cloud Development and Testing
I’ll explain how to utilize some of the well-known features on localstack. The process is straightforward; you only need to include an additional parameter to indicate the location of your localstack.
TL;DR Just add --endpoint-url=http://localhost:4566
to any S3 command you use
Using S3 in localstack
S3 is a popular object storage service provided by AWS. S3 is used to store and retrieve any amount of data, at any time, from anywhere on the web. With LocalStack, you can emulate S3 on your local machine.
Create a Bucket
To use S3 in LocalStack, you will need to create a bucket. You can create a bucket by running the following command:
aws --endpoint-url=http://localhost:4566 s3 mb s3://demo-bucket
This command will create a bucket named “demo-bucket” in LocalStack.
http://localhost:4566
is the URL of localstack
List all S3 Buckets
aws --endpoint-url=http://localhost:4566 s3api list-buckets
Upload a file to S3 Bucket
aws --endpoint-url=http://localhost:4566 s3 cp ./file.txt s3://demo-bucket
List all files in a S3 Bucket
aws --endpoint-url=http://localhost:4566 s3 ls s3://demo-bucket
Using DynamoDB in localstack
DynamoDB is a NoSQL database provided by AWS. DynamoDB is used to store and retrieve any amount of data, at any time, from anywhere on the web. With LocalStack, you can emulate DynamoDB on your local machine.
Create a table
aws --endpoint-url=http://localhost:4566 dynamodb create-table \
--table-name users \
--attribute-definitions AttributeName=id,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
This command will create a table named “users” in LocalStack.
Insert a record
aws --endpoint-url=http://localhost:4566 dynamodb put-item \
--table-name users \
--item '{"Id": {"S": "1234"}, "Name": {"S": "John Doe"}, "Age": {"N": "15"}}'
Get Item By Id
aws --endpoint-url=http://localhost:4566 dynamodb get-item --table-name users --key '{"Id": {"S": "1234"}}'
Using SQS in LocalStack
SQS is a message queue service offered by AWS. It allows you to send, receive, and process messages between distributed software components. To use SQS in LocalStack, you can use the AWS SDK or the AWS CLI.
Create a SQS Queue
aws --endpoint-url=http://localhost:4566 sqs create-queue --queue-name demo-queue
List Queues
aws --endpoint-url=http://localhost:4566 sqs list-queues
Produce Message
aws --endpoint-url=http://localhost:4566 sqs send-message --queue-url http://localhost:4566/000000000000/demo-queue
--message-body "{\"id\":1234, \"message\":\"success\"}"
Consume Message
aws --endpoint-url=http://localhost:4566 sqs receive-message --queue-url http://localhost:4566/000000000000/demo-queue --attribute-names All --message-attribute-names All --max-number-of-messages 1