0

我在 docker 中运行 LocalStack 并尝试将 CDK 资源部署到其中。

LocalStack 似乎运行正常:

docker ps
CONTAINER ID   IMAGE                          COMMAND                  CREATED      STATUS      PORTS                                                                                                                                                                                                             NAMES
1cbda0d0c6c5   localstack/localstack:latest   "docker-entrypoint.sh"   2 days ago   Up 2 days   127.0.0.1:53->53/tcp, 127.0.0.1:443->443/tcp, 127.0.0.1:4510-4530->4510-4530/tcp, 127.0.0.1:4566->4566/tcp, 127.0.0.1:4571->4571/tcp, 127.0.0.1:53->53/udp, 5678/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   localstack_main

我可以使用 awslocal 成功地将资源部署到它:

infra> awslocal s3api create-bucket --bucket my-bucket
Location: /my-bucket

infra> awslocal s3api list-buckets
Buckets:
- CreationDate: '2021-11-15T10:28:03+00:00'
  Name: my-bucket
Owner:
  DisplayName: webfile
  ID: bcaf1ffd86f41161ca5fb16fd081034f

凭据存储在命名配置文件中:

infra> echo $AWS_PROFILE
LS

infra> cat ~/.aws/config
[default]
region=eu-west-2
output=yaml

[profile LS]
region=eu-west-2
output=yaml

infra> cat ~/.aws/credentials
[default]
aws_access_key_id=test
aws_secret_access_key=test

[LS]
aws_access_key_id=test
aws_secret_access_key=test

但是,我面临的问题是当我尝试向此引入 CDK 时。我的堆栈没有使用环境。我想让它与环境无关。

const app = new cdk.App();
new InfrastructureStack(app, 'my-stack', {});

当我运行cdklocal bootstrapcdklocal bootstrap --profile LS它返回以下错误:

Unable to resolve AWS account to use. It must be either configured when you define your CDK Stack, or through the environment

从文档中,我期望一个与环境无关的堆栈将引导资源部署到默认帐户和区域中。

我还尝试过明确使用帐户 000000000000,因为我看到有些人这样做cdklocal bootstrap --profile LS aws://000000000000/eu-west-2会导致这种不同的错误:


 ⏳  Bootstrapping environment aws://000000000000/eu-west-2...
 ❌  Environment aws://000000000000/eu-west-2 failed bootstrapping: Error: Need to perform AWS calls for account 000000000000, but no credentials have been configured
    at SdkProvider.forEnvironment (/Users/willem/.nvm/versions/node/v14.16.1/lib/node_modules/aws-cdk/lib/api/aws-auth/sdk-provider.ts:149:46)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at Function.lookup (/Users/willem/.nvm/versions/node/v14.16.1/lib/node_modules/aws-cdk/lib/api/bootstrap/deploy-bootstrap.ts:30:17)
    at Bootstrapper.legacyBootstrap (/Users/willem/.nvm/versions/node/v14.16.1/lib/node_modules/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts:60:21)
    at /Users/willem/.nvm/versions/node/v14.16.1/lib/node_modules/aws-cdk/lib/cdk-toolkit.ts:463:24
    at async Promise.all (index 0)
    at CdkToolkit.bootstrap (/Users/willem/.nvm/versions/node/v14.16.1/lib/node_modules/aws-cdk/lib/cdk-toolkit.ts:460:5)
    at initCommandLine (/Users/willem/.nvm/versions/node/v14.16.1/lib/node_modules/aws-cdk/bin/cdk.ts:267:9)
Need to perform AWS calls for account 000000000000, but no credentials have been configured

编辑:值得注意的是,如果我完全绕过引导并运行,也会出现同样的问题cdlocal deploycdklocal deploy --profile LS. 我还在 CDK 源代码中指定了这样的环境:

const {
  CDK_DEFAULT_ACCOUNT = '0000000000',
  CDK_DEFAULT_REGION = 'eu-west-2',
} = process.env;

new InfrastructureStack(app, 'my-stack', {
  env: { account: CDK_DEFAULT_ACCOUNT, region: CDK_DEFAULT_REGION },
});

语境:

  • Mac OS 大苏尔
  • ZSH 5.8
  • AWS版本:aws-cli/2.2.40 Python/3.8.8 Darwin/20.6.0 exe/x86_64 prompt/off
  • CDK 版本 1.132.0
4

1 回答 1

0

我刚刚花了几个小时调试同样的问题。当我运行 cdk bootstrap --profile XXX -v(-v 标志显示更多日志信息)时,我看到一个错误,它试图从位于 .cdk/chache/account_partitions.json 的缓存文件中获取默认 AWS 帐户

该文件有一个其他配置文件的列表,格式如下:

"AccessKey": {
      "accountId": "awsAccountNumber",
      "partition": "aws"

  } 

当我在那里为我的个人资料添加信息时,引导操作完成。

我还没有弄清楚这个缓存文件何时以及如何更新,但至少它解决了第一个问题。

我知道这是一个旧帖子,但它可能对其他人有所帮助......

于 2022-02-16T16:27:52.417 回答