Skip to content

Instantly share code, notes, and snippets.

@mvillafuertem
Forked from lobster1234/localstack.md
Created September 28, 2022 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvillafuertem/225ee64bdc260d38992560ea9e149381 to your computer and use it in GitHub Desktop.
Save mvillafuertem/225ee64bdc260d38992560ea9e149381 to your computer and use it in GitHub Desktop.
Working with localstack on command line

Starting localstack

C02STG51GTFM:localstack mpandit$ make infra
. .venv/bin/activate; exec localstack/mock/infra.py
Starting local dev environment. CTRL-C to quit.
Starting local Elasticsearch (port 4571)...
Starting mock ES service (port 4578)...
Starting mock S3 server (port 4572)...
Starting mock SNS server (port 4575)...
Starting mock SQS server (port 4576)...
Starting mock API Gateway (port 4567)...
Starting mock DynamoDB (port 4569)...
Starting mock DynamoDB Streams (port 4570)...
Starting mock Firehose (port 4573)...
Starting mock Lambda (port 4574)...
Starting mock Kinesis (port 4568)...
Starting mock Redshift server (port 4577)...
Ready.

Optionally, start the web interface (make sure you pull the latest code, as there were node compatibility issues up until 3/31/2017)

C02STG51GTFM:localstack mpandit$ make web
(. .venv/bin/activate; bin/localstack web --port=8080)
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 272-228-949

S3

Create a bucket

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4572 s3 mb s3://mytestbucket
make_bucket: mytestbucket

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4572 s3 ls
2006-02-03 08:45:09 mytestbucket

Copy a file over

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4572 s3 cp /tmp/mongo.log s3://mytestbucket
upload: ../../../../tmp/mongo.log to s3://mytestbucket/mongo.log

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4572 s3 ls s3://mytestbucket
2017-04-05 01:18:39       4789 mongo.log

Delete this file

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4572 s3 rm s3://mytestbucket/mongo.log
delete: s3://mytestbucket/mongo.log

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4572 s3 ls s3://mytestbucket

C02STG51GTFM:localstack mpandit$ 

SNS

Create a topic

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4575 sns list-topics
{
    "Topics": []
}

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4575 sns create-topic --name test-topic
{
    "TopicArn": "arn:aws:sns:us-east-1:123456789012:test-topic"
}
C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4575 sns list-topics
{
    "Topics": [
        {
            "TopicArn": "arn:aws:sns:us-east-1:123456789012:test-topic"
        }
    ]
}

Subscribe to the topic

(use any random email)

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4575 sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:test-topic --protocol email --notification-endpoint pibehatin@1rentcar.top
{
    "SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:test-topic:5aacffbe-ccf7-40d5-be97-c55af7392935"
}

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4575 sns list-subscriptions
{
    "Subscriptions": [
        {
            "Owner": "", 
            "Endpoint": "pibehatin@1rentcar.top", 
            "Protocol": "email", 
            "TopicArn": "arn:aws:sns:us-east-1:123456789012:test-topic", 
            "SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:test-topic:5aacffbe-ccf7-40d5-be97-c55af7392935"
        }
    ]
}

Publish to this topic

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4575 sns publish  --topic-arn arn:aws:sns:us-east-1:123456789012:test-topic --message 'Test Message!'
{
    "MessageId": "n/a"
}

SQS

Create a Queue

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4576 sqs create-queue --queue-name test_queue
{
    "QueueUrl": "http://localhost:4576/123456789012/test_queue"
}

C02STG51GTFM:localstack mpandit$ aws --endpoint-url=http://localhost:4576 sqs list-queues
{
    "QueueUrls": [
        "http://localhost:4576/123456789012/test_queue"
    ]
}

Send a message to this queue

C02STG51GTFM:localstack mpandit$  aws --endpoint-url=http://localhost:4576 sqs send-message --queue-url http://localhost:4576/123456789012/test_queue --message-body 'Test Message!'
{
    "MD5OfMessageBody": "df69267381a60e476252c989db9ac8ad", 
    "MessageId": "a6ed436b-076a-0d8d-73e1-cc3291a19c28"
}

Receive the message from this queue

C02STG51GTFM:localstack mpandit$  aws --endpoint-url=http://localhost:4576 sqs receive-message --queue-url http://localhost:4576/123456789012/test_queue
{
    "Messages": [
        {
            "Body": "Test Message!", 
            "Attributes": {
                "ApproximateFirstReceiveTimestamp": "1.49138149959e+12", 
                "SenderId": "AIDAIT2UOQQY3AUEKVGXU", 
                "ApproximateReceiveCount": "1", 
                "SentTimestamp": "1.49138142195e+12"
            }, 
            "ReceiptHandle": "xuazrzyjcgpgzpzlxlyxmujbgzfkswixjkywshturlylrfwzyccutlumitgduyzddwkaoypcmswlkxrrjghdyztfewrpmkxdufptyketrfumwzicmggogdbaucwztvorplibccpfhirmalnixvfbklzrgncpisdsiuiajqwefxueqhuygfibmgqwx", 
            "MD5OfBody": "df69267381a60e476252c989db9ac8ad", 
            "MessageId": "a6ed436b-076a-0d8d-73e1-cc3291a19c28"
        }
    ]
}

If you do this many times, the process seems to sleep. May be to simulate the message visibility? Here are 2 consecutive runs.

real	0m15.185s
user	0m0.291s
sys	0m0.158s
real	0m26.829s
user	0m0.291s
sys	0m0.167s

Delete this message

aws --endpoint-url=http://localhost:4576 sqs delete-message --queue-url http://localhost:4576/123456789012/test_queue --receipt-handle 'yugzzebhnnksfuvbjlibfnlejyqbulxqfegsnrgafjeabxaatxbmeiyfkfliedslohseosgjwkxhdzllpudhccjhorpkwbgjgyzeyzjwkfvqflathnvsmugeyevbqmfyqanuxetvdhcetseuwzrqpexogzggznndxmbqowtlalvqtffntdahhihel'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment