로컬에서 AWS 서비스 에뮬레이터를 사용해보자!
로컬에서 사용해보자!
찾아보거나 알게된 배경
celery 테크스펙을 작성하며, DEVOPS에 리소스 요청 전에 테스트하기 위해 찾아봤다.
요약
- Docker를 띄우거나, localstack을 pip으로 설치하여, 로컬에서 에뮬레이팅 할 수 있다.
- awscli 명령어와 호환된다. endpoint_url을 변경하여 요청하면, localstack에서 해당 동작이 일어난다.
- Command Example
$ aws --endpoint-url=http://127.0.0.1:4566 sqs get-queue-attributes --queue-url <http://127.0.0.1:4566/000000000000/default.fifo> --attribute-names All { "Attributes": { "ApproximateNumberOfMessages": "1", "ApproximateNumberOfMessagesNotVisible": "0", "ApproximateNumberOfMessagesDelayed": "0", "CreatedTimestamp": "1659342190", "DelaySeconds": "0", "LastModifiedTimestamp": "1659342190", "MaximumMessageSize": "262144", "MessageRetentionPeriod": "345600", "QueueArn": "arn:aws:sqs:ap-northeast-2:000000000000:default.fifo", "ReceiveMessageWaitTimeSeconds": "0", "VisibilityTimeout": "1800", "FifoQueue": "true" } }
- Command Example
- boto3에서 endpoint_url을 설정하여, localstack에 동작시킬 수 있다.
- Code Example
client = boto3.client( "sqs", aws_access_key_id="fake", aws_secret_access_key="fake", endpoint_url="<http://localhost:4566>", ) client.create_queue("hello")
- Code Example
- 링크에서 어떤 서비스들에 대해 에뮬레이팅 할 수 있는지 볼 수 있다. ( + 구현 정도 )
- 테스트 환경에서 localstack을 활용하면, 기존에 테스트하기 힘들었던 sqs와 같은 aws service와 엮여있는 부분에 대해 테스트 할 수 있다.
아래의 내용으로 docker compose로 실행시키고 해당 url로 요청하면 된다.
version: '3.1'
services:
localstack:
container_name: celery-localstack
image: localstack/localstack-full
ports:
- "4566-4599:4566-4599"
- "8080:8080"
environment:
- DEFAULT_REGION=ap-northeast-2
- DEBUG=1
- START_WEB=1
- DATA_DIR=/tmp/localstack/data
- DOCKER_HOST=unix:///var/run/docker.sock
- HOST_TMP_FOLDER=/tmp/localstack
- EXTRA_CORS_ALLOWED_ORIGINS=*
volumes:
- ".tmp/localstack:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
network_mode: 'bridge'