Develop and Test AWS Apps free using LocalStack
ADTECH Corp. (A CMMI Level 3 Company)
Engineering Transformative Solutions
As cloud platforms get popular day by day, challenges like local development, testing, and cost related to these activities have become a pain point these days. Multiple studies suggest, that Dev activities take around 2 to 5% of total application development cost.
Continuous Integration helps you increase the speed of the development while ensuring the quality of the code that is deployed. In order for your CI to work properly, your code coverage should be good, and all the unit testing and integration testing code should cover all aspects of the application. With developers releasing small increments and lots of automated tests run during build and deployment, which increases the Cloud cost. With an average of 2-3% cost for deployment per environment, the costs will be higher if you have more environments.
For AWS there are some tools available that can help you minimize these costs. One such tool is?Localstack, it provides AWS services that can be run locally to help developers develop and unit test their code locally before deploying to Cloud. Localstack can also be integrated with the CI pipeline to reduce the cost. In this article, I will talk about how to setup up LocalStack and use the services to test your application. You can find more about LocalStack from their?official site?and?GitHub page.
How to Run
Localstack can be run using
For this article, I am using the following docker-compose file
version: "3.7"
services:
localstack:
image: localstack/localstack:latest
container_name: localstack
network_mode: bridge
ports:
- 4566:4566
- 8080:8080
environment:
- DEBUG=1
- SERVICES=s3,lambda
- DATA_DIR=/tmp/localstack/data
- DEFAULT_REGION=us-east-1
- LAMBDA_EXECUTOR=docker
volumes:
- "./storage:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
- "./startup:/docker-entrypoint-initaws.d"
Localstack uses a single port?4566?to run all its services. After you use?docker-compose up?or?docker-compose up -d?(detached mode) command, all the LocalStack services start running. The health of the running services can be verified using this?https://localhost:4566/health?URL. The configurations done in the docker-compose file are as follows:
Configurations
image:?Specific name and tag of LocalStack Docker image to use. I have used the latest tag, you can use a specific version instead.
DEBUG:?(0|1) Flag to increase log level and print more verbose logs (useful for troubleshooting issues)
SERVICES:?Comma-separated list of?AWS CLI service names. All the services are run if this property is kept empty, so it is better to specify the required services. In my example, I have used only S3 and Lambda services.
DATA_DIR:?Local directory for saving persistent data
领英推荐
LAMBDA_EXECUTOR:?(docker | local | docker-reuse) Specifies method to use while executing Lambda function.?docker?uses different containers for each lambda invocation,?docker-reuse?uses one docker container per function and shares it for all invocations, and?local?runs lambda in a temporary directory on the local machine.
docker-entry point:?You can provide a folder with shell scripts containing commands to create AWS resources. For this example, I have kept a shell script with the following command (It will create an S3 bucket after the services are up).
#!/bin/bas
awslocal s3api create-bucket --bucket test-bucket-1
Interaction
After the services are up, you can use?awscli?or?awscli-local?(wrapper on top of awscli) to access the local AWS services. If you use awscli?--endpoint-url?flag needs to be set to https://localhost4566, awscli-local automatically sets the endpoint URL.
Let us run a few commands to access the AWS services.
Here I tried getting the list of S3 buckets, we got one bucket "test-bucket-1" which was created as a part of docker-entrypoint script, then an S3 bucket "test-bucket" is created which got listed when I tried accessing S3 buckets again.
In your application, you might be using AWS SDK to interact with the AWS resources, if you do not do any code change then it will still call the Cloud AWS services. In order to force your application to start using local AWS services (Localstack services), you have to set?ServiceURL?property to point to the Localstack endpoint. Once this change is in place, it will start using the local services. This can be made configurable in your application so that only in DEBUG mode ServiceURL is set.
Conclusion
You can find the code snippets used in this post?here, In Adtech we have used Localstack extensively to bring down the development cost. Please stay connected if you want to know more about it and want to leverage its potential!