Mocking AWS services for local development using LocalStack
Tags: aws, localstack, localdevelopment, python
Published: 2023-01-30 06:31:45

# 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: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhs6qkxr477cyq3rpu7c.png)


#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  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8l4ojud9nl1j8xkp873r.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bqep2q7b4zsue4f6o3y.png)

Once done, check if all the services are running by hitting the following url on browser: http://localhost:4566/health 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2631a2j7sqge77gv4nck.png)



# 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 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cl54i96gjcfvm2ssobjn.png)

Calling the methods from main python file. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gu9dc6a9klns99s2ih14.png)


###Code Output: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4z0wqctnho2peirotq5.png)
 

To see the files in the created buckets on the browser, go to http://localhost:4566/{your-bucket-name} 

 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hq7fxokak4lp96awuqna.png)

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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbi1ozaadwcx9yj0z2kz.png) 


We are calling the functions through main.py 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36d4x2k2pzc77eyxklju.png)


###SNS Code Output  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzskexwka045vq3xmle7.png)


##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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/giarb7q4nfvho7140hat.png)

Calling the methods in main.py 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0sws0zpa4nmupv7iwci4.png)
 

###SSM Code Output: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/am57w9wnaratyon247xd.png) 


#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.