How to mock AWS services in local development?
AWS provides many products that are very much an integral part of our application development. It provides many products like analytics, database, storage, security, streaming, IoT, Blockchain, etc., for integrations.
We always have the following questions while integrating with AWS services
- Is there a way for us to avoid connecting to AWS during local development?
- Is it possible to have local development as close to production?
- Is it possible to save AWS costs during development?
I always like the idea of developing applications in airplane mode until the product is ready for prime time. This story covers mocking AWS services locally using Atlassian localstack and creating a simple application using nodejs, multer, and local AWS S3.
Atlassian Localstack
LocalStack provides an easy-to-use test/mocking framework for developing Cloud applications. It spins up a testing environment on your local machine that provides the same functionality and APIs as the real AWS cloud environment. For additional information on services refer to localstack on github
We can run localstack on docker and will use below service in the docker-compose.yml file to run our sample application
In the compose file, we configured docker to open ports, setting up environment variables, and mounting volumes. The DATA_DIR environment variable is being used by the docker container to store AWS services data locally.
Setup local environment
- Install Docker
- Clone the sample application from https://github.com/zrven/aws-mock-s3-example.git
git clone https://github.com/zrven/aws-mock-s3-example.git
npm install
- Run localstack docker container
docker-compose up -d
- Install AWS CLI in terminal
awscli client (install using brew, pip3 or visit https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
pip3 install aws-cli
//OR
brew install awscli
- Configure AWS key and secret in localstack
aws configure --profile localstack
AWS Access Key ID [None]: ACCESSKEYAWSUSER
AWS Secret Access Key [None]: sEcreTKey
Default region name [None]: us-west-2
Default output format [None]: json
- Create a S3 bucket in localstack
aws --profile localstack --endpoint-url=http://localhost:4566 s3api create-bucket --bucket demo-bucketaws --profile localstack --endpoint-url=http://localhost:4566 s3api put-bucket-acl --bucket demo-bucket --acl public-read
In this example, we are setting ACL as public-read to access the images using S3 URL. It can be based on your security settings.
- Verify S3 bucket
*** EDIT — This section might not be needed, jump to “Prepare and Run application”
Open http://localhost:8055/#!/infra in the browser and you’ll see a bucket in AWS localstack
*** EDIT — END
Prepare and run sample Application
It is a simple REST API created in nodejs, express and uses multer (for file uploads), and aws-sdk libraries (to upload the files or images to S3).
- app.js file to create an express server
- route.js for a POST route
- aws.js to upload the file to S3
- Run the application using
npm start
- Send POST request to http://localhost:4000/api/document/ and pass the files using form-data with a key as a file, and value with an image.
curl -X POST http://localhost:4000/api/document \
-F file=@GOPR3856.jpeg
- AWS S3 will return a response after successful image upload
{
Location: 'http://localhost:4572/demo-bucket/GOPR3856.jpeg',
Bucket: 'demo-bucket',
Key: 'GOPR3856.jpeg',
ETag: '"75b886fe73388d4fd63a19197272d848-2"'
}
Now you’ll be able to view or download the file directly from S3 using the Location URL. Its time for “claps”
I recommend localstack if you are building applications and using AWS services.
PS: The sample code is available in https://github.com/zrven/aws-mock-s3-example.git