Quickly debug AWS App locally use Localstack(AWS Mock in local)
When you develop components locally that rely on AWS, it can be cumbersome to configure authentication information, and sometimes network latency. It is important to mock with a native AWS component, which can:localstack I’m using Docker-Compose here to start a set of tools that local development depends on:
The docker-compose.yml looks like this following
1version: '3'
2
3services:
4 mongodb:
5 image: mongo:3.4.1
6 volumes:
7 - './easymock/data/db:/data/db'
8 networks:
9 - easy-mock
10 ports:
11 - "27017:27017"
12 restart: always
13 container_name: mongodb
14
15 redis:
16 image: redis:4.0.6
17 command: redis-server --appendonly yes
18 volumes:
19 - './easymock/data/redis:/data'
20 networks:
21 - easy-mock
22 restart: always
23 container_name: redis
24
25 localstack:
26 container_name: "${LOCALSTACK_DOCKER_NAME-localstack}"
27 image: localstack/localstack
28 ports:
29 - "4567-4597:4567-4597"
30 environment:
31 - DEBUG=${DEBUG-true}
32 - DATA_DIR=${DATA_DIR-/tmp/localstack/data}
33 - PORT_WEB_UI=${PORT_WEB_UI-8080}
34 - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-docker-reuse}
35 - DOCKER_HOST=unix:///var/run/docker.sock
36 - HOSTNAME_EXTERNAL=awsmock.local-dev.com
37 volumes:
38 - "./localstack/data:/tmp/localstack"
39 - "/var/run/docker.sock:/var/run/docker.sock"
40 networks:
41 - easy-mock
42
43 mysql:
44 container_name: "mysql"
45 image: mysql
46 command: "--default-authentication-plugin=mysql_native_password --skip-mysqlx"
47 ports:
48 - "3306:3306"
49 environment:
50 MYSQL_ROOT_PASSWORD: "xxxxx"
51 MYSQL_DATABASE: "awsmock"
52 MYSQL_PASSWORD: "xxxxx"
53 MYSQL_USER: "user"
54 volumes:
55 - "./mysql:/var/lib/mysql"
56 networks:
57 - easy-mock
58
59networks:
60 easy-mock:
you should create relative folder such as: mkdir -p ./localstack/data ./easymock/data/redis ./easymock/data/db
mount to container persistent datas
when exec docker-compose up -d .
the docker container should be is launched
the aws endpoint-url is configured: awsmock.local-dev.com
in /etc/hosts file map to 127.0.0.1
You only need to change the appropriate endpoint-url to use them, without the need for sophisticated credentials
Install AWS Cli command in Ubuntu
1 sudo apt-get update
2 sudo apt-get install awscli
3 aws --version
Now you can use AWS-cli command create resources:
1# create a sns
2aws --endpoint-url=http://awsmock.local-dev.com:4575 sns create-topic --name testSns
3#create a sqs
4aws --endpoint-url=http://awsmock.local-dev.com:4576 sqs create-queue --queue-name testSqs --attributes ReceiveMessageWaitTimeSeconds=20
5# get-queue-attributes
6aws --endpoint-url=http://awsmock.local-dev.com:4576 sqs get-queue-attributes --queue-url http://awsmock.local-dev.com:4576/queue/testSqs
7# add attributes for exist sqs
8aws --endpoint-url=http://awsmock.local-dev.com:4576 sqs set-queue-attributes --attributes ReceiveMessageWaitTimeSeconds=15 --queue-url http://awsmock.local-dev.com:4576/queue/testSqs
9#create bucket on s3 in mock
10aws --endpoint-url=http://awsmock.local-dev.com:4572 s3 mb s3://myTestBucket
11# list all sqs
12aws --endpoint-url=http://awsmock.local-dev.com:4576 sqs list-queues
13
14# create the secrets item
15awslocal secretsmanager create-secret \
16 --name MyTestSecret \
17 --description "My test secret created with the CLI." \
18 --secret-string "{\"user\":\"diegor\",\"password\":\"EXAMPLE-PASSWORD\"}"
In java code you can implement mock Bean:
1@Bean
2public AWSSecretsManager awsSecretsManager() {
3 AwsClientBuilder.EndpointConfiguration endpointConfiguration =
4 new AwsClientBuilder.EndpointConfiguration("http://awsmock.local-dev.com:4584",
5 "us-east-1");
6 return AWSSecretsManagerClientBuilder
7 .standard()
8 .withEndpointConfiguration(endpointConfiguration)
9 .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("testKey", "testSecret")))
10 .build();
11}
When you use S3 service functions, construct sdk client instance. Must be set 'use_path_style_endpoint' => true
(this PHP key-value settings in clientSdk arguments) .withPathStyleAccessEnabled(true)
the java clientSdk implementations
When your code needs to be compatible with both native mocks and production environments, you can set the environment variables, which will not be set in production.
1if (!empty(env('AWS_MOCK_SM_URL', ''))) {
2 $args['endpoint'] = env('AWS_MOCK_SM_URL');
3 }
4$client = new SecretsManagerClient($args);
The new version localstack
- use local aws command don’t need include the –endpoint-url use this python package:
python3 -m pip install awscli-local