AWS provides us with too many useful services, some of them are not tightly coupled to AWS such as Memcached, Redis, MySQL, … and some of them are AWS specific services like DynamoDB, Lambda, API Gateway … So if you are using one of the AWS Specific services then the main headache you will face is usually how to get a local development environment.
When do we need to mock AWS
Sometimes we need to fake AWS services in the local environment instead of wasting money and resources on deploy stuff to the real AWS for each developer in the team or wasting money on running a test on CI.
So I found mocking AWS services is really good for situations like:
- Local development environment.
- Unit/Functional test.
- Debugging (special for debugging lambda).
on the other hand, it’s not good for,
- End-to-End test.
- Infrastructure test.
How can we mock AWS
Last few months my team and I were looking for a way to run a test without costing money, and we found few approaches to achieve this,
1- Find the right tool for each service
You can always search for the right open-source project that emulates AWS specific services like, local-dynamodb or docker-lambda. The problem with that is if your system contains a lot of these services, then you need to maintain and set up all of these services which could be hard.
2- Use tool gives you multiple services like,
- SAM, which is a CLI tool for local development and testing of Serverless applications.
- Develop by AWS
- Still new tool, no big community yet.
- Need cloudformation configuration.
- Build only for Serverless.
- LocalStack, it is a fully functional local AWS cloud stack.
- Support a lot of AWS services like API Gateway, Kinesis, DynamoDB, Elasticsearch, S3, Lambda, …
- Builds on existing best-of-breed mocking/testing tools
- Bigger community.
- Easy to setup, “just run on docker” initialised-localstack img.
- Easy to use, “just override AWS endpoint URL for each service”.
aws --endpoint-url=http://localhost:4569 dynamodb list-tables
client = boto.client('dynamodb', endpoint_url = "http://172.17.0.1:4569/") client.list_tables()
Also, it has a dashboard
Localstack is a really useful tool, however, it has some limitation, like
- Some of the services support only us-east-1 region.
- It assumes that all services have Admin IAM role and policy.
Which sometimes makes it tricky to re-use and re-run the same infrastructure scripts you have on-place for the real services to setup Localstack, but to be fair this didn’t bother me that much when I use it.
I highly recommend you to try LocalStack if you are developing AWS based applications, especially if you wanna to use same mocking tool to mock AWS services anywhere you have it in your solution then go with LocalStack. On the other hand, if you have only Serverless application then go with SAM.