🏗️

LocalStackのdocker-composeと起動スクリプトさらす

2022/08/22に公開約3,000字2件のコメント

ご自由にお使いください。

ディレクトリ構成

.
├── docker
│   └── localstack
│       ├── schemas
│	│   └── Comment.json
│       ├── dynamodb.sh
│       ├── s3.sh
│       └── ses.sh
└── docker-compose.yml
docker-compose.yml
version: '3.7'

services:
  localstack:
    image: localstack/localstack:0.14.4
    ports:
      - 4566:4566 # すべてのサービスポート
      - 8000:8080 # dashboard
    volumes:
      # 初回起動スクリプト
      - ./docker/localstack:/docker-entrypoint-initaws.d
      # おまじない
      - /var/run/docker.sock:/var/run/docker.sock
      # データのマウント
      - 'localstack-data:/tmp/localstack'
    environment:
      # 有効にするサービス
      - SERVICES=s3,dynamodb,ses
      # s3のpresignedUrlを利用するためにはtestにしなければいけない
      # https://docs.localstack.cloud/integrations/aws-cli/#setting-up-local-region-and-credentials-to-run-localstack
      - AWS_ACCESS_KEY_ID=test
      - AWS_SECRET_ACCESS_KEY=test
      - AWS_DEFAULT_REGION=ap-northeast-1
      - DATA_DIR=/tmp/localstack/data
      - DISABLE_CORS_CHECKS=1

volumes:
  localstack-data:
    driver: 'local'
Comment.json
{
  "AttributeDefinitions": [
    { "AttributeName": "roomId", "AttributeType": "S" },
    { "AttributeName": "createdAt", "AttributeType": "N" }
  ],
  "TableName": "Comment",
  "KeySchema": [
    { "AttributeName": "roomId", "KeyType": "HASH" },
    { "AttributeName": "createdAt", "KeyType": "RANGE" }
  ],
  "BillingMode": "PROVISIONED",
  "ProvisionedThroughput": {
    "ReadCapacityUnits": 10,
    "WriteCapacityUnits": 10
  },
  "StreamSpecification": {
    "StreamEnabled": false
  },
  "SSESpecification": {
    "Enabled": false
  },
  "Tags": []
}
dynamodb.sh
#!/bin/sh

echo "DynamoDB setup start!"
echo "Creating DyanmoDB talbes..."

aws --endpoint-url=http://localstack:4566 dynamodb list-tables > /tmp/dynamotables.txt

if [ $? -ne 0 ]
then
echo -e "date : Error occurs while connecting aws dynamodb.. "
fi

for file in `\find /docker-entrypoint-initaws.d/schemas -name '*.json'`;
do
    TABLE_NAME="$(basename -- $file | sed 's/\.[^\.]*$//')"
    echo "Creating $TABLE_NAME"
    if grep -q $TABLE_NAME /tmp/dynamotables.txt; then
        echo "table exists! $TABLE_NAME"
    else
        aws --endpoint-url=http://localstack:4566 dynamodb create-table --cli-input-json file://$file
        echo "table created! $TABLE_NAME"
    fi
done
echo "DynamoDB setup done!"
s3.sh
#!/bin/sh

echo "S3 setup start!"
echo "Creating S3 bucket..."

aws  --endpoint-url=http://localstack:4566  s3 ls > /tmp/s3buckets.txt

if [ $? -ne 0 ]
then
echo -e "date : Error occurs while connecting aws s3.. "
fi

if grep -q your-bucket-name /tmp/s3buckets.txt; then
    echo 'bucket exists! your-bucket-name'
else
  aws --endpoint-url=http://localstack:4566 s3 mb s3://your-bucket-name/
  echo 'bucket created!'
fi
echo "S3 setup Done!"
ses.sh
#!/bin/sh

echo "SES setup start!"
echo "Verifying SES email..."
aws ses verify-email-identity --email-address dummy@example.com --endpoint-url=http://localstack:4566

echo "Verifyed SES email!"
echo "SES setup done!"

Discussion

クソコメで恐縮なのですが、Google検索で上の方に出てくるので残します。
この記事投稿より1ヶ月前、LocalstackのGAに合わせて、docker-composeの内容を変更するPRが出されてます。

https://github.com/localstack/localstack/actions/runs/2612161497/workflow
2022年8月末の時点でv1.1.0がリリースされており、本記事と設定がいくつか異なる箇所があるので最新版のdocker-compose.ymlの設定は公式リポジトリを御参照ください。
https://github.com/localstack/localstack/blob/master/docker-compose.yml
ログインするとコメントできます