Introduction to LocalStack
During local development, it can be expensive to use an AWS services license and to ensure the resources used stay within the allocated limits. LocalStack is an alternative that can remove the dependency on AWS services during local development and allow you to mock the services offline.
LocalStack is a cloud service emulator that runs in a single container on your laptop. It is an open-source mock for real AWS services. LocalStack allows us to test or develop applications that need AWS without connecting with AWS.
LocalStack has various flavors – Community, Pro, and Enterprise. Pro and Enterprise require us to purchase license, but the Community Edition option is free for personal usage. Some differences between Community and Paid versions (Pro onwards) onwards are as follows:
Installing LocalStack
Here, I am going to install LocalStack using docker-compose.
Requirements
We need to install the following packages:
- Docker, docker-compose and npm
- pip install botocore boto3 localstack awscli
In AWS credentials, we can add mock access id and access key.
[localstack]
aws_access_key_id = Test
aws_secret_access_key = Test
Getting LocalStack Up and Running
To start using the LocalStack, we need to paste the below content in a file and save the file as docker-compose.yaml.
Docker-compose.yaml
We can add more configurations as needed in the docker-compose.yaml file. For more information on Docker compose extra configurations, refer https://docs.localstack.cloud/references/configuration/
Next, through the terminal / command prompt, navigate to the folder containing the docker-compose.yaml file and type docker-compose up -d to start the LocalStack.
Once done, check if all the services are running by hitting the following url on browser: http://localhost:4566/health
LocalStack implementation examples:
All the code samples are developed and tested using community version of LocalStack.
Little to no changes are required if you have an existing code that uses AWS services. In the community version, we need to add endpoint_url parameter when making the AWS calls. Here, the endpoint_url points to the localhost.
S3 (Simple Storage Service) with LocalStack
In community versions, we do not have access to the s3 storage location in local system. Though the files persist in the docker container as long as the session persists.
LocalStack document for reference: https://docs.localstack.cloud/user-guide/aws/s3/
S3 Sample Code
We have created S3Storage Class
Calling the methods from main python file.
Code Output:
To see the files in the created buckets on the browser, go to http://localhost:4566/{your-bucket-name}
To have a look at a particular file in the bucket or to download a file, go to: http://localhost:4566/{your-bucket-name}/{file-name}. This will download the file through your browser.
SNS (Simple Notification Service) with LocalStack
LocalStack documentation for reference: https://docs.localstack.cloud/user-guide/aws/sns/
SNS Sample Code
We have SNS service class with implementations.
We are calling the functions through main.py
SNS Code Output
SSM (Systems Manager) with LocalStack
SSM Local Stack documentation: https://docs.localstack.cloud/user-guide/aws/systems-manager/
SSM Parameter Store Sample Code
SSM service class with put and get parameter implementations.
Calling the methods in main.py
SSM Code Output:
Advantages and Limitations:
Advantages
- It allows us to communicate with AWS services with little to no changes in the existing code.
- It allows us to develop and test AWS services without internet.
Limitations
- We need to use a third-party app Commandeer to get a UI (User Interface) for monitoring LocalStack.
- LocalStack docker container needs to be running to work with LocalStack.
- For community version, restarting the container causes loss of data as the everything is stored inside the container.
Summary and Conclusion
- LocalStack can be a good option for local development and testing.
- There is no dependency on AWS accounts, and we do not need to be worried about exceeding the resource limits.
- As LocalStack is locally present, development or testing that requires huge data upload/download on S3, etc can be done even if we are facing bandwidth issues.
- To get more from LocalStack, we would need to purchase the pro license. It will allow us to use LocalStack with our AWS services code base by keeping our source code as is.
Disclaimer
This is a personal [blog, post, statement, opinion]. The views and opinions expressed here are only those of the author and do not represent those of any organization or any individual with whom the author may be associated, professionally or personally.
Top comments (0)