Recently I came across this github project called Localstack. It allows you to spin up a local AWS environment as a service or as a Docker container. You can utilize such a tool in your integration testing in your CI/CD pipelines while not paying a cent for the used AWS services or also for all kinds of “hacking AWS” efforts. I’m pretty sure there is many more usage scenarios. Today I’d like to show you how this awesome stack works.
For this step by step tutorial, I will work in my Ubuntu environment and utilize Pipenv, so make sure to check that out if you haven’t already.
Now let’s get our hands dirty, let’s clone the Localstack Git repo.
git clone https://www.github.com/localstack/localstack localstack_playground
Let’s CD into the folder containing the codebase
and now let’s install into Pipenv the localstack tool with all the dependencies (npm) and related packages (awscli-local) needed:
pipenv install npm
pipenv install localstack
pipenv install awscli-local
Lets start the pipenv env shell
Lets start the localstack
Now the service is running:
Let’s open a new terminal window and we can start to hit the mocked up AWS services running now locally. We’ll create a s3 bucket called tutorial, list my buckets , change the access control list for this s3 bucket, upload a file we create and then remove this bucket and list all my buckets again to see that everything worked and the teardown cleanup phase successfully passed. For these s3 calls, we’ll use the awslocal cli wrapper around localstack, but you can proceed using Boto3 as well.
awslocal s3 mb s3://tutorial
awslocal s3 ls
echo Hello World! >> helloworld.txt
awslocal s3api put-bucket-acl --bucket tutorial --acl public-read
awslocal s3 cp helloworld.txt s3://tutorial
Let’s see the s3 objects in the browser using this URL ( the port of the mocked up s3 service is 4572 as you can see in the screenshot above:
try this url: http://localhost:4572/tutorial/
try this url: http://localhost:4572/tutorial/helloworld.txt
Now we shall remove the object, the bucket and list my buckets to see there is no bucket left
awslocal s3 rm s3://tutorial/helloworld.txt
awslocal s3 rb s3://tutorial
awslocal s3 ls
Now it’s clear you can easily work with AWS services like S3. Other services on this list work great as well, for instance let’s try to create a SNS topic and publish a message into it.
awslocal sns create-topic --name datahappy_topic
# you get back the topic arn id
awslocal sns publish --topic-arn "arn:aws:sns:us-east-1:123456789012:datahappy-topic" --message "datahappy about local mocked up sns"