Tips and Tricks for Local AWS

Maria Livia Chiorean

Software Developer @ Sky

@MariaLiviaCh

Problem

(Story Time)

The Developer ^-^

A

B

C

Account 1

Account 2

  1. Credentials for account 1;

  2. Credentials for account 2;

  3. Create testing resources for 2 DynamoDB tables, 2 S3 buckets and 1 Kinesis;

  4. Add some test data;

  5. Start app C;

  6. Start app B;

  7. Start app A.

Steps

Solution

Localstack

Docker container

with Localstack image

...

Application

Prerequisites

> brew cask install docker
> brew install docker-compose

docker-compose.yml

version: "3.7"

services:
  localstack:
    image: localstack/localstack
    ports:
      - "4569:4569"
      - "4572:4572"
    environment:
      - SERVICES=dynamodb,s3

sbt-docker-compose plugin

addSbtPlugin("com.tapad" % "sbt-docker-compose" % "1.0.34")

lazy val root = (project in file("."))
  .enablePlugins(DockerComposePlugin)
  .settings(
    name := "localstack-example"
  )

Or simply run:

> docker-compose -d up
> sbt dockerComposeUp

Spin up docker:

Create AWS resources

> brew install awscli

> aws s3api 
    create-bucket
    --endpoint-url=http://localhost:4572 
    --bucket Config
AWSTemplateFormatVersion: "2010-09-09"

Resources:
  ConfigBucket:
    DeletionPolicy: "Retain"
    Type: "AWS::S3::Bucket"
    Properties:
      BucketName: "Config"

Update AWS Client

import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.PutObjectRequest

val client = S3Client.builder()
    .endpointOverride(URI.create("http://localhost:4572"))
    .build()

val request = PutObjectRequest.builder()
    .bucket("Config")
    .key("application")
    .build()
    
client.putObject(request, RequestBody.fromString("config data"))

Start application

Can we do better?

Write a plugin to do it for you!

Old World :(

 

  1. Install Docker and Docker Compose

  2. Write docker-compose.yml

  3. Spin up Docker

  4. Create resources

  5. Update client to use endpoint-url

  6. Run app

New World :)

 

  1. Install Docker and Docker Compose

  2. Run plugin command

  3. Update client to use endpoint-url

  4. Run app

The sbt-local-aws plugin

  • Spins up a docker container with localstack running.

  • Only starts up the requested AWS services.

  • Automatically creates AWS CLI resource creation commands form available cloudformation.

  • Performs cloudformation substitutions.

  • Commands for start, stop and printing out the commands.

The sbt-local-aws plugin

  • At the moment only works with DynamoDB resources.

  • Only supports yml cloudformation.

Demo

Resources

Special thanks to @OscarTheLargeCat (Instagram)

Copy of Copy of Tips and Tricks for Running AWS Dependencies Locally

By Maria Livia

Copy of Copy of Tips and Tricks for Running AWS Dependencies Locally

Sphere.it 2019

  • 636