在本地堆栈启动时创建DynamoDB表。

huangapple go评论91阅读模式
英文:

Creating DynamodDB table on start of the localstack

问题

使用Localstack与DynamoDB,并需要在启动时默认创建一些表格。然而,我遇到了一个奇怪的错误:

  1. /docker-entrypoint-initaws.d/init.sh: is a directory

docker-compose配置:

  1. localstack:
  2. container_name: localstack
  3. image: localstack/localstack:latest
  4. network_mode: bridge
  5. ports:
  6. - "4566:4566"
  7. - "8010:8010"
  8. environment:
  9. - EDGE_PORT=8010
  10. - SERVICES=s3,dynamodb
  11. - DEBUG=1
  12. - DATA_DIR=/tmp/localstack/data
  13. volumes:
  14. - "$PWD/.localstack/init.sh:/docker-entrypoint-initaws.d/init.sh"

init.sh 文件如下:

  1. awslocal dynamodb create-table \
  2. --table-name table-global \
  3. --attribute-definitions AttributeName=id,AttributeType=S \
  4. --key-schema AttributeName=id,KeyType=HASH \
  5. --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

我的环境:

  • 操作系统:MacOS Ventura
  • Docker 版本:v4.16.2

我尝试了多种选项,但仍然无法解决问题。我不确定我可能做错了什么。

英文:

Using Localstack with Dynamodb and need to create some tables by default on the start. However, I am getting the strange error that:

  1. /docker-entrypoint-initaws.d/init.sh: is a directory

docker-compose config:

  1. localstack:
  2. container_name: localstack
  3. image: localstack/localstack:latest
  4. network_mode: bridge
  5. ports:
  6. - "4566:4566"
  7. - "8010:8010"
  8. environment:
  9. - EDGE_PORT=8010
  10. - SERVICES=s3,dynamodb
  11. - DEBUG=1
  12. - DATA_DIR=/tmp/localstack/data
  13. volumes:
  14. - "$PWD/.localstack/init.sh:/docker-entrypoint-initaws.d/init.sh"

and the init.sh looks like:

  1. awslocal dynamodb create-table \
  2. --table-name table-global \
  3. --attribute-definitions AttributeName=id,AttributeType=S \
  4. --key-schema AttributeName=id,KeyType=HASH \
  5. --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

My stack:

  • OS: MacOS Ventura
  • Docker: v4.16.2

I have tried multiple options but it still does not work. I am not sure what I might be doing wrong.

答案1

得分: 2

我通过更改您的 docker-compose.yml 文件来修复了您的问题:

  1. version: "3.8"
  2. services:
  3. localstack:
  4. container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
  5. image: localstack/localstack
  6. ports:
  7. - "127.0.0.1:4566:4566"
  8. environment:
  9. - DEBUG=1
  10. - DOCKER_HOST=unix:///var/run/docker.sock
  11. volumes:
  12. - "$PWD/init-aws.sh:/etc/localstack/init/ready.d/init-aws.sh" # ready hook
  13. - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
  14. - "/var/run/docker.sock:/var/run/docker.sock"

init-aws.sh 文件如下:

  1. #!/bin/bash
  2. awslocal dynamodb create-table \
  3. --table-name table-global \
  4. --attribute-definitions AttributeName=id,AttributeType=S \
  5. --key-schema AttributeName=id,KeyType=HASH \
  6. --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

请确保首先对文件运行 chmod +x init-aws.sh

您可以通过以下命令验证此操作:

  1. $ awslocal dynamodb list-tables
  2. {
  3. "TableNames": [
  4. "table-global"
  5. ]
  6. }
英文:

I fixed your issue by changing your docker-compose.yml to:

  1. version: "3.8"
  2. services:
  3. localstack:
  4. container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
  5. image: localstack/localstack
  6. ports:
  7. - "127.0.0.1:4566:4566"
  8. environment:
  9. - DEBUG=1
  10. - DOCKER_HOST=unix:///var/run/docker.sock
  11. volumes:
  12. - "$PWD/init-aws.sh:/etc/localstack/init/ready.d/init-aws.sh" # ready hook
  13. - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
  14. - "/var/run/docker.sock:/var/run/docker.sock"

The init-aws.sh looks like:

  1. #!/bin/bash
  2. awslocal dynamodb create-table \
  3. --table-name table-global \
  4. --attribute-definitions AttributeName=id,AttributeType=S \
  5. --key-schema AttributeName=id,KeyType=HASH \
  6. --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

Make sure to run chmod +x init-aws.sh on the file first.

I can validate this via the following command:

  1. $ awslocal dynamodb list-tables
  2. {
  3. "TableNames": [
  4. "table-global"
  5. ]
  6. }

huangapple
  • 本文由 发表于 2023年3月7日 08:09:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定