本地DynamoDB(基于LocalStack)运行异常求助
问题:LocalStack + cdklocal本地DynamoDB交互失败,仅重启电脑可恢复
环境与操作流程
使用CDK定义DynamoDB表及关联索引,通过LocalStack配合cdklocal做本地测试,运行环境为Mac Ventura 13.1系统。电脑重启后首次运行完全正常,启动堆栈的脚本startStack.sh内容如下:
docker-compose up -d echo "Waiting for 5 seconds" sleep 5 cd test-app cdklocal bootstrap echo "Waiting for 5 seconds" sleep 5 cdklocal deploy TestAppStack #cdklocal deploy TestAppStack/ops-table DYNAMO_ENDPOINT="http://localhost:4566/" dynamodb-admin & open http://0.0.0.0:8001 cd ..
test-app目录包含DynamoDB(及索引)的本地定义,首次执行cdklocal deploy无报错,确认CDK定义本身无问题。
docker-compose配置:
version: "3.8" services: localstack: container_name: AWS-DEVELOPMENT-WITH-LOCALSTACK image: localstack/localstack:latest network_mode: bridge ports: - "127.0.0.1:53:53" - "127.0.0.1:53:53/udp" - "127.0.0.1:443:443" - "127.0.0.1:4566:4566" - "127.0.0.1:4571:4571" - "127.0.0.1:${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}" environment: - DYNAMODB_SHARE_DB=1 - DISABLE_CORS_CHECKS=1 - SERVICES=s3,dynamodb,sns,sqs,firehose,kinesis,ses,sts,cloudformation - DEBUG=1 - DATA_DIR=/tmp/localstack/data - PORT_WEB_UI=8080 - LAMBDA_EXECUTOR=local - KINESIS_ERROR_PROBABILITY=1.0 - DOCKER_HOST=unix:///var/run/docker.sock - HOST_TMP_FOLDER=./.localstack volumes: - './.localstack:/var/lib/localstack' - '/var/run/docker.sock:/var/run/docker.sock'
异常现象
首次运行脚本时一切正常:dynamodb-admin窗口可正常打开,其他接口能与本地DynamoDB表正常交互。但一段时间后(触发原因暂未定位),所有与本地DynamoDB的交互均失败,报错信息如下:
Bootstrapping environment aws://000000000000/us-west-2... ❌ Environment aws://000000000000/us-west-2 failed bootstrapping: UnknownEndpoint: Inaccessible host: `localhost' at port `4566'. This service may not be available in the `us-west-2' region. at Request.ENOTFOUND_ERROR (/usr/local/lib/node_modules/aws-sdk/lib/event_listeners.js:611:46) at Request.callListeners (/usr/local/lib/node_modules/aws-sdk/lib/sequential_executor.js:106:20) at Request.emit (/usr/local/lib/node_modules/aws-sdk/lib/sequential_executor.js:78:10) at Request.emit (/usr/local/lib/node_modules/aws-sdk/lib/request.js:686:14) at error2 (/usr/local/lib/node_modules/aws-sdk/lib/event_listeners.js:443:22) at ClientRequest.<anonymous> (/usr/local/lib/node_modules/aws-sdk/lib/http/node.js:99:9) at ClientRequest.emit (node:events:513:28) at ClientRequest.emit (node:domain:489:12) at Socket.socketErrorListener (node:_http_client:494:9) at Socket.emit (node:events:513:28) { code: 'UnknownEndpoint', region: 'us-west-2', hostname: 'localhost', retryable: true, originalError: [Error], time: 2023-01-15T06:46:40.614Z } Inaccessible host: `localhost' at port `4566'. This service may not be available in the `us-west-2' region.
脚本会卡在以下步骤无法继续:
[16:52:01] Retrieved account ID 000000000000 from disk cache [16:52:01] Assuming role 'arn:aws:iam::000000000000:role/cdk-hnb659fds-deploy-role-000000000000-us-west-2'. [16:52:01] Assuming role failed: Inaccessible host: `localhost' at port `4566'. This service may not be available in the `us-west-2' region. [16:52:01] Could not assume role in target account using current credentials Inaccessible host: `localhost' at port `4566'. This service may not be available in the `us-west-2' region. . Please make sure that this role exists in the account. If it doesn't exist, (re)-bootstrap the environment with the right '--trust', using the latest version of the CDK CLI. current credentials could not be used to assume 'arn:aws:iam::000000000000:role/cdk-hnb659fds-deploy-role-000000000000-us-west-2', but are for the right account. Proceeding anyway. [16:52:01] Waiting for stack CDKToolkit to finish creating or updating...
重启Docker无法解决该问题,仅重启电脑可恢复正常。
可能原因与解决方法
1. 端口占用或Docker网络异常
- 原因:Mac系统长时间运行后,4566端口可能被其他进程抢占,或Docker桥接网络的DNS解析出现异常,导致localhost:4566无法访问。
- 解决:
- 执行
lsof -i :4566检查端口占用情况,若有其他进程占用则终止对应进程,再重启LocalStack容器。 - 执行
docker network prune清理无用网络,重启Docker后重新启动LocalStack容器。
- 执行
2. LocalStack持久化数据损坏
- 原因:
./.localstack目录存储LocalStack的持久化数据,长时间运行可能出现文件损坏,导致服务无法正常响应。 - 解决:
- 停止LocalStack容器:
docker-compose down - 删除本地数据目录:
rm -rf ./.localstack - 重新启动容器并执行cdklocal bootstrap和deploy流程。
- 停止LocalStack容器:
3. cdklocal缓存信息失效
- 原因:cdklocal会缓存环境凭证和端点信息,当LocalStack服务重启或网络变化后,缓存信息无法匹配导致连接失败。
- 解决:
- 执行
cdklocal clean清理本地缓存 - 执行
cdklocal bootstrap --force强制刷新环境配置。
- 执行
4. Mac Ventura系统网络设置变化
- 原因:Ventura系统的隐私安全设置可能后台修改网络规则,或系统休眠后网络接口重置,导致localhost解析异常。
- 解决:
- 执行
ping localhost验证解析是否正常,若解析错误则修改/etc/hosts文件,添加127.0.0.1 localhost。 - 关闭系统“自动切换网络”功能,避免网络环境频繁变化影响Docker网络。
- 执行
5. LocalStack镜像版本不稳定
- 原因:使用
latest标签的镜像可能存在不稳定版本,长时间运行后出现内存泄漏或进程挂死。 - 解决:
- 修改docker-compose中的镜像字段为稳定版本,例如
localstack/localstack:2.2.0,重新拉取镜像并启动容器。
- 修改docker-compose中的镜像字段为稳定版本,例如
内容的提问来源于stack exchange,提问作者LNI
相关产品推荐
相关产品推荐

