What is 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.

It’s possible to run Lambda functions, store data to DynamoDb tables and traffic messages in SQS Queues on your local machine, without ever talking to the cloud.

What is it for?

We need to develop in an environment that is minimally similar (ideally the same) to the one where the application will run. Those who develop for the AWS environment do not always have an AWS account available or, even when they do, they cannot manipulate resources with great freedom due to the security of the corporate environment. We know that “experiment is necessary”, but no one wants to take unnecessary risks.

So, with AWS Localstack we can test our features at development time without having to consume the resources of an AWS account.

Advantages

  • Money saving, as you don’t need to use paid features of an account to test things at deployment time
  • It’s cheaper. You save time and money to perform functional testing of our features in a local environment (since you’re able to consume Cloud resources for free and locally \o/ )

Hot topic!

  • Not all AWS features are offered for free by Localstack, but it is possible to architect an environment very close to the real environment used in the cloud. See available resources for free here.

Cool! And how can I starting using it?

Install and configure AWS CLI:

$ aws configure

AWS Access Key ID [None]: ${set_some_access_key_id}

AWS Secret Access Key [None]: ${set_some_secret_access_key}

Default region name [None]: ${set_some_aws_region}

Default output format [None]: json

Run AWS Localstack with Docker-Compose

docker-compose.yaml

services:
  localstack:
    container_name: aws-localstack
    image: localstack/localstack
    restart: always
    network_mode: bridge
    ports:
      - "127.0.0.1:4510-4530:4510-4530"
      - "127.0.0.1:4566:4566"
      - "127.0.0.1:4571:4571"
    environment:
      - AWS_DEFAULT_REGION=sa-east-1
      - AWS_ACCESS_KEY_ID=admin
      - AWS_SECRET_ACCESS_KEY=admin
      - DEFAULT_REGION=sa-east-1
      -SERVICES=s3,dynamodb,sqs,apigateway,cloudformation,cloudwatch,ec2,iam,kinesis,kms,lambda,route53,secretsmanager,sns,ssm,stepfuncstions,sts
      - DEBUG=${DEBUG-}
      - DATA_DIR=${DATA_DIR-}
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
      - HOST_TMP_FOLDER=${TMPDIR:-/tmp/}localstack
      - DOCKER_HOST=unix:///var/run/docker.sock
      - DISABLE_CORS_CHECKS=1
    volumes:
      - "${TMPDIR:-/tmp}/localstack:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

Command to execute aws localstack with docker-compose:

$ docker-compose up -d

Now, we can see the AWS LocalStack (port 4566) via the command line:

We can use the AWS Resources locally!

So, let’s create a SQS queue:

$ aws --endpoint-url=http://localhost:4566 sqs create-queue --queue-name hello-world

Let’s send a message to the new queue:

$ aws --endpoint-url=http://localhost:4566 sqs send-message --queue-url http://localhost:4566/queue/hello-world --message-body "My first Message"

We can also see the messages from our new queue:

$ aws --endpoint-url=http://localhost:4566 sqs receive-message --queue-url http://localhost:4566/queue/hello-world --max-number-of-messages 10

We can create DynamoDb Tables and manipulate the datas.

Create table:

$ aws dynamodb --endpoint-url=http://localhost:4566 create-table \
--table-name Music \
--attribute-definitions \
AttributeName=Artist,AttributeType=S \
AttributeName=SongTitle,AttributeType=S \
--key-schema \
AttributeName=Artist,KeyType=HASH \
AttributeName=SongTitle,KeyType=RANGE \
--provisioned-throughput \
ReadCapacityUnits=10,WriteCapacityUnits=5

Insert data to table:

$ aws --endpoint-url=http://localhost:4566 dynamodb put-item --table-name Music --item '{"Artist": {"S": "Guns N Roses"}, "SongTitle": {"S": "Sweet Child O Mine"}}'

Read table:

$ aws dynamodb scan --endpoint-url=http://localhost:4566 --table-name Music

Final Considerations

In conclusion, we can put our application behind an API Gateway, create KMS crypto’s to SQS Queue, write lambda functions and feed events through Kinesis streams, all this for free and in our local machine.

It is also possible to use SDKs provided by AWS to connect our applications to free AWS Localstack resources. Learn more about it here.

In my opinion, this is a great tool to test development features in a simulated environment without needing a cloud account to do this.

Download Source Code

$ git clone https://github.com/favtuts/aws-localstack-practices.git
$ cd localstack-docker-compose

Leave a Reply

Your email address will not be published. Required fields are marked *