docker localstack, terraformを利用して s3のlocal環境を作成するの続きでどうしても、AWSでPush型Eventの環境を作りたくなり始めているだけ。
docker localstack, terraformを利用して sqsのlocal環境を作成する
table of contents
- sqsのresouceを作成する
- localstackにserviceを登録する
- apply
1. sqsのresouceを作成する
sqs用のterraformファイルとmain.tfを編集する。
main.tf
には、endpointsを定義。
terraform {
backend "local" {}
}
provider "aws" {
region = "ap-northeast-1"
access_key = "access_key"
secret_key = "secret_key"
s3_use_path_style = true
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
endpoints {
s3 = "http://localstack-main:4566"
sqs = "http://localstack-main:4566"
}
}
sqs.tf
には、SQSの定義を記載する。
terraformにほぼどおり。
resource "aws_sqs_queue" "example_queue" {
name = "example_queue"
delay_seconds = 0
max_message_size = 262144
message_retention_seconds = 1209600
receive_wait_time_seconds = 5
redrive_policy = jsonencode({
maxReceiveCount = 4,
deadLetterTargeArn = aws_sqs_queue.example_queue_deadletter.arn
})
}
resource "aws_sqs_queue" "example_queue_deadletter" {
name = "example_queue_deadletter"
}
resource "aws_sqs_queue_redrive_allow_policy" "example_queue_deadletter_policy" {
queue_url = aws_sqs_queue.example_queue_deadletter.id
redrive_allow_policy = jsonencode({
redrivePermission = "byQueue",
sourceQueueArns = [aws_sqs_queue.example_queue.arn]
})
}
2. localstackにserviceを登録する
servicesに追加する
compose.yaml
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
build:
context: ./localstack
dockerfile: dockerfile
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
environment:
# LocalStack configuration: https://docs.localstack.cloud/references/configuration/
- DEBUG=${DEBUG:-0}
- SERVICES=s3,sqs
volumes:
- "localstack_volume:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
3. apply
あとは、terraformでapplyを実施する
% terraform apply