Localstack#

LocalStack is a cloud service emulator that runs in a single container on your laptop or in your CI environment. With LocalStack, you can run your AWS applications or Lambdas entirely on your local machine without connecting to a remote cloud provider! Read the Docs.
Localstack provides us with a dockerfile and a docker-compose to run locally. let’s set it up.
In a docker-copmose.yml let’s add these 2 containers:

  • localstack

  • dynamodb-admin

version: '3.9'

services:
  dynamodb:
    image: aaronshaf/dynamodb-admin
    ports:
      - "8001:8001"
    environment:
      - DYNAMO_ENDPOINT=http://localstack:4566
    networks:
      - localstack-net

  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack:1.2.0
    ports:
      - "127.0.0.1:4566:4566"            # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559"  # external services port range
      - "127.0.0.1:53:53"                # DNS config (only required for Pro)
      - "127.0.0.1:53:53/udp"            # DNS config (only required for Pro)
      - "127.0.0.1:443:443"              # LocalStack HTTPS Gateway (only required for Pro)
    environment:
      - DEBUG=${DEBUG-}
      - PERSISTENCE=${PERSISTENCE-}
      - LAMBDA_EXECUTOR=local
      - LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY-}  # only required for Pro
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    networks:
      - localstack-net

networks:
  localstack-net:
    name: localstack-net

Now Let’s spin our containers up:

docker-compose up -d

To view the logs you can run this of localstack you can run this:

docker logs -f --tail 500 `docker ps |grep localstack |awk '{print $1}'`

(or just do docker ps grep the id, and run docker logs -f --tail 500 <id>)

serverless framework integration with localstack#

One last part before we can deploy our application locally is to configure our up to work with local stack.
To achieve that we will add a section to the templated serverless.yml file. We will use this serverless plugin

npm install --save serverless@2.72.3
npm install --save-dev serverless-localstack@1.0.1
npm install --save-dev serverless-python-requirements@5.4.0

You can see the added plugin in package.json.
image

Now, let’s add it to the serverless.yml file:
Add in the uppermost hierarchy:

plugins:
  - serverless-python-requirements
  - serverless-localstack

Let’s minimize the deployed package size by adding this in serverless.yml (directly, not in another section)

package:
  exclude:
    - './**'
  include:
    - './handler.py'

And again, add in the uppermost hierarchy::

custom:
  localstack:
    stages:
      # list of stages for which the plugin should be enabled
      - local
    host: http://localhost  # optional - LocalStack host to connect to
    edgePort: 4566  # optional - LocalStack edge port to connect to
    autostart: true  # optional - Start LocalStack in Docker on Serverless deploy
    networks: #optional - attaches the list of networks to the localstack docker container after startup
      - host
      - overlay
      - localstack-net
    lambda:
      # Enable this flag to improve performance
      mountCode: false  # specify either "true", or a relative path to the root Lambda mount path
    docker:
      # Enable this flag to run "docker ..." commands as sudo
      sudo: False
  pythonRequirements:
    fileName: requirements.txt
    dockerizePip: false