简体   繁体   中英

Running CDK bootstrap against LocalStack fails with credentials error

I'm running LocalStack in docker and trying to deploy CDK resources into it.

LocalStack seems to run OK:

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

I can successfully deploy resources to it using 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

Credentials are stored in a named profile:

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

However, the problem I'm facing is when I try to introduce CDK to this. My stack is not using an environment. I want to keep it environment agnostic.

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

When I run cdklocal bootstrap or cdklocal bootstrap --profile LS it returns the following error:

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

From the docs I am expecting an environment agnostic stack to deploy the bootstrap resources into the default account and region.

I've also tried explicitly using the account 000000000000 as I've seen some people do with cdklocal bootstrap --profile LS aws://000000000000/eu-west-2 which results in this different error:


 ⏳  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

EDIT : Worth noting that the same issues occur if I bypass bootstrapping altogether and just run cdlocal deploy | cdklocal deploy --profile LS . I've also specified the environment in the CDK source code like this:

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 },
});

Context:

  • Mac OS Big Sur
  • ZSH 5.8
  • AWS version: aws-cli/2.2.40 Python/3.8.8 Darwin/20.6.0 exe/x86_64 prompt/off
  • CDK version 1.132.0

I've just spent e few hours debugging this same issue. When I ran cdk bootstrap --profile XXX -v (the -v flag shows more log info), I saw an error where it was trying to get a default AWS account from a cache file located at.cdk/chache/account_partitions.json

This file had a list for the other profiles in the following format:

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

  } 

When I added the info for my profile there, the bootstrap action completed.

I haven't figured out when and how this cache file is updated, but at least it resolved the first problem.

I know this is an old post, but it might help someone else...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM