How to use cloud services locally – Localstack.

silver and black laptop computer
Reading Time: 3 minutes

What is Localstack ?

Localstack is a fully functional local cloud stack.It helps to develop and test the cloud and servless apps locally.

LocalStack is a mirror of cloud services.Secondly , The only difference is that you run this on your local.It is a mocking framework for developing cloud applications.Thirdly , It serves us an environment that provides the same functionality as that of cloud services.

Where does Localstack fits in the CI-CD ?

Lets assume the below use case:-

  • Firstly A developer develops an application that needs cloud services.
  • Secondly now it provisions all required cloud services in a local container.
  • It fits in the CI environment .When changes are pushed in the environment the test is done with the it. Lastly once everything works fine with it services then further it is provisioned with aws .

Why to use LocalStack

  • It helps to run application without connecting to aws .
  • Using this one can focus more on development rather than configuring aws.
  • All suitable error test can be done on local before deploying actual on aws.
  • Can be integrated with CI CD
  • Has a support for various CI tools with the help of plugins.

How to use Localstack ?

It is a python application that runs as a HTTP request on a certain port.

Prerequiste

  • Python installed in system
  • Pip
  • Docker

Ways to run Localstack

  • As a docker container
  • As an application

Running localstack with python

Firstly to install it run the below command

pip install localstack

Further With the above command it is installed in the system.

Output

Localstack start

To check the container just type the below command

docker ps

Output

So it runs under a python docker conatiner.

Check the various services under localstack

To check the various services that it provides run the below command.

localstack status services

Output

Various services under localstack

Accessing the infrastructure via CLI

Firstly you can access the infra locally.To do so use the below command.

List the streams

aws --endpoint-url=http://localhost:4566 kinesis list-streams

The endpoint is the port for our localstack-service .The above command is used to list the stream of local infrastructure.Further , we dont have any stream so the output will be empty.

Installing the local cli for localstack

To work with it you need to install aws-local cli. Further this helps to run command without using the endpoint url.

To install the cli run the below command.

pip install awscli-local

Once installed the interaction with various services is started.

Create sqs que from localstack

Firstly to create the sqs service from it run the below command.

awslocal sqs create-queue --queue-name sample-queue

Further the above command creates a queue named sample-queue.

Output

The queue is made with the url. Now this url can be used in the service for sqs.

Creating and listing buckets on localstack.

As we already know it is a mirror of cloud services.So lets create a s3 bucket and list it in a particular region.

To create the s3 bucket in it run the below command.

awslocal s3api create-bucket --bucket rishi --region us-east-1

In the above command awslocal cli is used.The bucket named rishi is created in us-east-1 region.

Further To list the bucket run the below command.

awslocal s3api list-buckets

The above blog will list all the bucket created.

Output

awslocal bucket creation

Further Like the above many more interactions can be done. All the services mentioned can be created and destroyed.

Conclusion

Lastly it saves time of creating services on cloud.All the testing can be done locally with this.You can easily see what you are creating is working or not.In conclusion the more we test locally, the less AWS services we use.

Reference

https://leviwheatcroft.github.io/selfhosted-awesome-unlist/localstack.html

Written by 

Rishivant is a enthusiastic devops learner at Knoldus. He believes in going 1% up everyday and showcases his learning in his work.