목차

LocalStack

Port

docker-compose

version: "3"
 
services:
  localstack:
    image: localstack/localstack:0.12.6 # 혹시나 자동 버전업으로 인한 오류를 막기위해 tag 명시
    container_name: localstack
    ports:
      - "4566:4566"
    environment:
      - SERVICES=sns,sqs,s3
      - USE_SINGLE_REGION=1
      - DEFAULT_REGION=ap-northeast-2
      - HOSTNAME=localhost
      - HOSTNAME_EXTERNAL=loaclhost
      - DATA_DIR=/tmp/localstack/data
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack" # localstack 이 생성하는 파일을 직접 편집하고 싶을 때
      - "./localstack-init.d:/docker-entrypoint-initaws.d"
      - "/var/run/docker.sock:/var/run/docker.sock"
sleep 30
# awslocal 호출 --endpoint-url=http://localstack:4566 생략 가능
# localstack 은 docker-compose service 이름인데, 여기에 밑줄이 있으면 네트워크 접속이 안됐음.
# regtion 은 DEFAULT_REGION=ap-northeast-2 으로 자동 지정됨.
 
echo "## creating topics"
 
awslocal sns create-topic --name my-sns 
 
echo "## creating queues"
awslocal sqs create-queue --queue-name my-sqs
 
awslocal sns subscribe --topic-arn arn:aws:sns:ap-northeast-2:000000000000:my-sns \
  --protocol sqs --notification-endpoint http://localstack:4566/queue/my-sqs \

Mac OS

TMPDIR=/private$TMPDIR docker-compose up

localstack-full

arn

localstack 실행 후 초기화

# localstack 은 컨테이너 이름.
docker exec -it localstack /bin/bash
 
# 명령실행
awslocal ...

awslocal

# user 계정 영역에 설치
pip install --user awscli-local
alias awslocal="AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test AWS_DEFAULT_REGION=${DEFAULT_REGION:-$AWS_DEFAULT_REGION} aws --endpoint-url=http://${LOCALSTACK_HOST:-localhost}:${LOCALSTACK_PORT:-4566}"

# 환경변수 DEFAULT_REGION, LOCALSTACK_HOST, LOCALSTACK_PORT 를 지정한다.

Java SDK 설정

AWSStaticCredentialsProvider localstackCrendentialProvider =
        new AWSStaticCredentialsProvider(new BasicAWSCredentials("anykey", "anysecret"));
AwsClientBuilder.EndpointConfiguration localstackEndpointConfiguration =
        new AwsClientBuilder.EndpointConfiguration("http://localhost:4566", Regions.AP_NORTHEAST_2.name());
 
AmazonSNS amazonSNS =  AmazonSNSClient
    .standard()
    .withEndpointConfiguration(localstackEndpointConfiguration)
    .withCredentials(localstackCrendentialProvider)
    .build();

S3

AmazonS3 amazonS3 = AmazonS3ClientBuilder
    .standard()
    .withEndpointConfiguration(localstackEndpointConfiguration)
    .withCredentials(localstackCrendentialProvider)
    .withPathStyleAccessEnabled(true)
    .build();

참조