Localstack— Running AWS Locally — Introduction
I consider myself a strong exponent of Developer Experience ( DX / DevX ), i.e. I continuously endeavor to support the developer ecosystem to be an optimal one. This is a critical function in my daily scheme of things.
This article attempts to tackle the cloud development challenges for the developer by bringing the public cloud development experience to their machines.
Dealing with cloud development is not easy. It involves a longer turnaround time, paranoia around what could go wrong and of-course the cost. How many of us have the time to be so attentive to the pricing details anyway. As a developer we want to know the outcome quicker minus the fuss.
The cost can be any of the following :
- monetary,
- Time
- Breaking existing setup
- Injecting security / quality defects.
As developers getting a quicker feedback, or more importantly what we are doing wrong with convenience of our local system, reducing cost of errors, safety concerns has always been a problem we wrestle with everyday.
Opportunity with local cloud emulation :
- Saves time - Quicker turnaround time of development, infra provisioning, testing.
- Saves cost — You really don’t need to pay anything for running aws resources.
- Improves Developer experience — Overall developers have a better quality time doing the deliverables.
- Improves quality of deliverables — Promotes better DevOps ( Shift-left ) — Because of faster iterations and convenient testing support provides for better quality deliverables.
Localstack as cloud emulation :
Localstack provides AWS cloud emulation for your local system. It has emulation for more than 60 services.
It allows developers to run infrastructure setup locally without requiring an AWS account. There is no web console available. Its all over CLI.
Localstack also works well with all popular CI/CD tools such as Github actions, Gitlab, Circle CI.
Localstack also can be used with infrastructure provisioning tools such as Terraform, and Pulumi.
Setting up localstack :
1) Running localstack,
Localstack can be run using docker container.
The docker compose can look as follows:
https://github.com/prabhakarit/localstack-test/blob/main/Intro/docker-compose.yml
“SERVICES” variable is used to provision the services required to run our tests. In case no services are defined, than all services will be available.
“AWS_ACCESS_KEY_ID” and “AWS_SECRET_ACCESS_KEY” is used for connecting to emulated aws account.
Start localstack as follows,
docker compose-up
2) Installing localstack CLI,
Following works on mac, requires brew package manner installed,
brew install localstack
Following will require python, pip installed,
python3 -m pip install localstack
3) Configure AWS environment
aws configure --profile localstack
Provide inputs as follows to use authentication for AWS on localstack :
AWS Access Key ID [None]: dummy
AWS Secret Access Key [None]: dummy
Default region name [None]: ap-south-1
Default output format [None]: json
4) Setup environment variables
export AWS_DEFAULT_REGION=ap-south-1
export LOCALSTACK_URL=http://0.0.0.0:4566
export AWS_PROFILE=localstack
5) Setup Alias for AWS CLI to substitute default endpoint
alias aws="aws --endpoiny-url $LOCALSTACK_URL
Optional steps :
// Checking health of localstack
curl localhost:4566/health | jq
OR,
// validate localstack configuration
localstack config validate
// If you have a pro license, setup api key as part of environment variable before starting localstack docker container.
export LOCALSTACK_API_KEY=<your-api-key>
// reference : https://docs.localstack.cloud/get-started/pro/
Using localstack :
Now the fun begins.
To summarize, you have setup AWS localstack profile with dummy authentication, added supporting environment variables for running localhost endpoint with aws cli to effect changes to your local aws emulation instead ( of actual AWS remote account ).
Run following commands to confirm:
The following commands intend to validate operations on S3 from bucket, creation, object management and finally bucket deletion.
// list buckets
aws s3api list-buckets
// create a bucket
aws s3 mb s3://test-s3-bucket — region ap-south-1
// create a file to upload
echo “hello localstack” >> helloworld.txt
// upload file to bucket
aws s3 cp helloworld.txt s3://test-s3-bucket
// list objects in the bucket
aws s3api list-objects — bucket test-s3-bucket
// download the object to local filesystem
aws s3api get-object — bucket test-s3-bucket — key test output.txt
// confirm content of the file as expected
cat output.txt
// delete the object
aws s3api delete-object — bucket test-s3-bucket — key test
// confirm if object is deleted
aws s3api list-objects — bucket test-s3-bucket
// You can run following command before deleting the object to see the error for trying to delete an bucket with objects
aws s3api delete-bucket — bucket test-s3-bucket
// confirm if bucket is deleted
aws s3api list-buckets
We have come full circle here and it did feel like it was AWS all the time.
Observations :
Localstack setup is convenient with docker.
Few configurations are necessary to make localstack work as expected with AWS CLI. However, the configurations were very simple to get by.
Please note that all resources created are ephemeral and will only live for localstack uptime.
Additionally, note that some scenarios cannot be emulated, such as global unique s3 bucket names as localstack is local to the system.
There is decent community information to help with what you require with your setup. I was able to find enough content to get help with.
Response time of localstack is quick as it runs from local machine. This is very handy when you need to know what is the impact in good time.
Before planning to use localstack not all services are free. Most of the serverless offerings of AWS are free under localstack. The popular free services you can get started right away are S3, lambda, API Gateway, EC2, IAM, secrets manager, dynamo db, SQS, SNS, Logs, Event bridge.
AWS Services covered : https://docs.localstack.cloud/aws/feature-coverage/
Good to know :
FYI - I tried couple of other things with Localstack.
- Lambda runtime behind API-Gateway : lambda is a popular usecase to run locally before commiting directly to AWS. The ability to run and debug lambdas locally can save us a world of pain and suffering.
- CI/CD and Terraform : Localstack can be used in pipelines and has good support for popular pipeline providers. The idea is to validate terraform script with localstack before running same on actual AWS account. This provides a testing and validation stage for my terraform. This is a very handy thing to do. Improves durability of your pipeline to a whole new level.
I intend to cover above areas as well in future blog posts. Will update the links to them as and when they are available.
Thank you for getting this far :-)
I will be happy to know how was your experience with localstack. i.e. The good and the bad from your perspective.
You can expect more such write ups to help developers, technologists and enthusiasts like you. Until next time !