简体   繁体   English

如何解决:'无法解析要使用的 AWS 帐户。 必须在定义 CDK 时或通过环境配置它

[英]How to resolve: 'Unable to resolve AWS account to use. It must be either configured when you define your CDK or through the environment'

I am trying to run CDK commands to check the diff of my local and remote stack.我正在尝试运行 CDK 命令来检查我的本地和远程堆栈的差异。

I am using the following command.我正在使用以下命令。

cdk diff --profile saml

I am getting the following error message我收到以下错误消息

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

I am looking for ways to resolve this issue.我正在寻找解决此问题的方法。

Removing [profile default] from ~/.aws/config solved it for me.~/.aws/config中删除[profile default]为我解决了这个问题。

You need to specify your credentials.您需要指定您的凭据。 Check out this article: https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_credentials查看这篇文章: https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_credentials

It gives details on how to do this:它提供了有关如何执行此操作的详细信息:

Specifying Your Credentials and Region指定您的凭证和区域

You must specify your credentials and an AWS Region to use the AWS CDK CLI.您必须指定您的凭证和 AWS 区域才能使用 AWS CDK CLI。 The CDK looks for credentials and region in the following order: CDK 按以下顺序查找凭证和区域:

Using the --profile option to cdk commands.对 cdk 命令使用 --profile 选项。

Using environment variables.使用环境变量。

Using the default profile as set by the AWS Command Line Interface (AWS CLI).使用 AWS 命令行界面 (AWS CLI) 设置的默认配置文件。

You can set up a profile using the AWS CLI.您可以使用 AWS CLI 设置配置文件。 See https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html for details on how to do this.有关如何执行此操作的详细信息,请参阅https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html You can specify named profiles, such as a profile for each account, and then specify the profile name to use for your CDK call.您可以指定命名配置文件,例如每个帐户的配置文件,然后指定要用于您的 CDK 调用的配置文件名称。

In your .aws folder, create/add an empty file named credentials without the file extension.在您的.aws文件夹中,创建/添加一个名为credentials的空文件,不带文件扩展名。

In my case it had nothing to do with the proposed solutions.就我而言,它与提议的解决方案无关。
If you add the -v (verbose) argument to cdk command, you will see the actual error: Unable to determine the default AWS account: TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"如果您将-v (详细)参数添加到 cdk 命令,您将看到实际错误: Unable to determine the default AWS account: TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:" Unable to determine the default AWS account: TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"

I tried different versions of CDK and nothing, still the same error.我尝试了不同版本的 CDK,但没有,仍然是同样的错误。 After many tries I ended up with the root cause: node version.经过多次尝试,我最终找到了根本原因:节点版本。 With v15.2.1 I got that error, but after downgrading to v14.15.1 the issue was solved.使用 v15.2.1 我得到了这个错误,但在降级到 v14.15.1 后问题就解决了。

You should explicitly set your account and region when initializing your stacks.您应该在初始化堆栈时明确设置您的帐户和区域。 AWS CDK CLI provides two environment variables, CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION, to determine the target at synthesis time. AWS CDK CLI 提供了两个环境变量 CDK_DEFAULT_ACCOUNT 和 CDK_DEFAULT_REGION 来确定综合时的目标。

new MyDevStack(app, 'dev', { 
  env: { 
    account: process.env.CDK_DEFAULT_ACCOUNT, 
    region: process.env.CDK_DEFAULT_REGION 
}});

If you use these environment variables, the target account and region are fetched from your active AWS profile, eg --profile option.如果您使用这些环境变量,目标账户和区域将从您的活动 AWS 配置文件中获取,例如 --profile 选项。

https://docs.aws.amazon.com/cdk/latest/guide/environments.html https://docs.aws.amazon.com/cdk/latest/guide/environments.html

Finally I used following in C:\XXXX.aws\credentials最后我在 C:\XXXX.aws\credentials 中使用了以下内容

[default]
aws_access_key_id=XXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXX

and cmd:> cdk deploy --profile default和 cmd:> cdk deploy --profile default

However this didn't work.然而这并没有奏效。

[project1]
aws_access_key_id = ANOTHER_AWS_ACCESS_KEY_ID
aws_secret_access_key = ANOTHER_AWS_SECRET_ACCESS_KEY

and cmd:> cdk deploy --profile project1和 cmd:> cdk deploy --profile project1

Error: AWS region must be configured either when you configure your CDK stack or through the environment错误:必须在配置 CDK 堆栈时或通过环境配置 AWS 区域

You might be suffering from https://github.com/aws/aws-cdk/issues/5455您可能患有https://github.com/aws/aws-cdk/issues/5455

To work around, don't use a profile, grab access keys (your org should have a way) and export these to the environment要解决此问题,请不要使用配置文件,获取访问密钥(您的组织应该有办法)并将这些导出到环境

Not sure if this help, but in my organization we use AWS SSO, with over 90 AWS Accounts, with MFA, so it was impossible to manage CDK without AWS CLIv2 with profiles, and AWS SSO support.不确定这是否有帮助,但在我的组织中,我们使用具有 90 多个 AWS 账户和 MFA 的 AWS SSO,因此如果没有带有配置文件的 AWS CLIv2 和 AWS SSO 支持,就不可能管理 CDK。

I found a way to solve it, so till its officially relesed, you can use this, it works quite all right: MatsCloud blog - CDK with AWS SSO multi account multi profile我找到了解决它的方法,所以在正式发布之前,你可以使用它,它工作得很好: MatsCloud blog - CDK with AWS SSO multi account multi profile

in ~/.aws/config the tag [default] must be alone in a new row Example:~/.aws/config中,标签[default]必须单独位于新行中示例:

[someBasicUser] [一些基本用户]
[default] [默认]
aws_access_key_id = XXXXXXXXXXXXXX aws_access_key_id = XXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXX aws_secret_access_key = XXXXXXXXXXXXXXX

I my case I created new access key's in AWS fist, which I used then as descruped above.在我的情况下,我在 AWS 拳头中创建了新的访问密钥,然后我按照上面的描述使用了它。

aws configure aws_access_key_id aws_secret_access_key then enter your region aws 配置 aws_access_key_id aws_secret_access_key 然后输入您的区域

Then it worked.然后它起作用了。 So I assume when the error is "Unable to resolve AWS account" you have the issue new access keys, as the current one is obviously not valid anymore.因此,我假设当错误为“无法解析 AWS 帐户”时,您会遇到新的访问密钥问题,因为当前的访问密钥显然不再有效。

enter image description here在此处输入图像描述

I just came across this, because I had the same issue...我刚遇到这个,因为我有同样的问题......

using THE SAME profile idenitiefer fixed it for my:D使用相同的配置文件标识符为 my:D 修复了它

I was using an _ where as the profile name contained a - ...我使用的是_ ,因为配置文件名称包含- ...

The error is produced by CDK because it can't resolve valid AWS CLI credentials which allows it to resolve the account by making a call similar to该错误是由 CDK 产生的,因为它无法解析有效的 AWS CLI 凭证,这允许它通过调用类似于

aws sts get-caller-identity --profile profile_name

There are multiple ways to configure the AWS CLI with valid credentials so that CDK can interact with the CLI configuration to obtain credentials有多种方法可以使用有效凭证配置 AWS CLI,以便 CDK 可以与 CLI 配置交互以获取凭证

  1. In ~/.aws/credentials, which is easiest and least preferred/secure way due to using longterm creds, you can place longterm credentials assigned to an IAM user like this在 ~/.aws/credentials 中,由于使用长期凭证,这是最简单且最不推荐/最不安全的方式,您可以像这样放置分配给 IAM 用户的长期凭证
    [default]
    aws_access_key_id=AKIAI44QH8DHBEXAMPLE
    aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
  1. Using credential process https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sourcing-external.html使用凭证过程https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sourcing-external.html

In ~/.aws/config在 ~/.aws/config

[profile developer]
credential_process = /opt/bin/awscreds-custom --username helen
  1. Using a more secure tool like https://github.com/99designs/aws-vault使用更安全的工具,例如https://github.com/99designs/aws-vault

  2. Using AWS CLI integration with AWS SSO.使用 AWS CLI 与 AWS SSO 的集成。 This setup allows for SSO across multiple accounts and supports multiple MFA strategies including biometric.此设置允许跨多个帐户进行 SSO,并支持包括生物识别在内的多种 MFA 策略。 The problem with this is that the current version of the CDK has not been updated to use the latest version of the Node AWS SDK so it does not know how to retrieve credentials.问题在于当前版本的 CDK 尚未更新为使用最新版本的 Node AWS SDK,因此它不知道如何检索凭证。 Hopefully a future release of CDK will resolve this but its been almost 2 years希望未来的 CDK 版本能够解决这个问题,但已经快 2 年了

In ~/.aws/config在 ~/.aws/config

[profile sso_profile]
sso_start_url = https://sso_url.awsapps.com/start
sso_region = us-east-2
sso_account_id =
sso_role_name = AWSAdministratorAccess
region = us-east-2
output=json
 

Fortunately there is a decent workaround which works seamlessly once configured.幸运的是,有一个不错的解决方法,一旦配置就可以无缝运行。 This involves utilizing a python lib that can expose an SSO profile as a credentials process which is supported by current CDK.这涉及使用 python 库,该库可以将 SSO 配置文件公开为当前 CDK 支持的凭证进程。 Install https://pypi.org/project/aws2-wrap/安装https://pypi.org/project/aws2-wrap/

pip3 install aws2-wrap==1.2.7

Then in ~/.aws/config add a wrapper profile that uses aws2-wrap to exposes as a credentials process然后在 ~/.aws/config 添加一个使用 aws2-wrap 的包装器配置文件作为凭证过程公开

[profile wrapped_sso_profile]
region = us-east-2
credential_process = aws2-wrap --process --profile sso_profile

AWS CLI v2 is capable of using AWS SSO to acquire credentials natively; AWS CLI v2 能够使用 AWS SSO 本地获取凭证; however the SDK that was used to develop AWS CDK is not capable of using AWS SSO natively to acquire the credentials.但是,用于开发 AWS CDK 的 SDK 无法原生使用 AWS SSO 来获取凭证。 Hence, "credential_process" must be configured in ~/.aws/config to acquire credentials.因此,必须在 ~/.aws/config 中配置“credential_process”以获取凭据。

While configuring "credential_process", I found they python package "aws2-wrap" (pip install aws2-wrap) as simple and straightforward.在配置“credential_process”时,我发现它们 python package “aws2-wrap”(pip install aws2-wrap)简单明了。 The other package "aws-sso-credential-process" required pipx which itself installed python@3.10 on my Mac.另一个 package "aws-sso-credential-process" 需要 pipx,它本身在我的 Mac 上安装了 python@3.10。

In my case I was running the command:就我而言,我正在运行命令:

npm run cdk diff my-stack --profile MyProfile

However it worked when I changed to use但是,当我更改为使用时它起作用了

npx cdk diff my-stack --profile MyProfile

This is because npm run x does not pass through --parameters to child processes.这是因为npm run x不会通过--parameters传递给子进程。

I just created a new profile.我刚刚创建了一个新的个人资料。

aws configure --profile profile2

aws_access_key_id=XXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXX
region = us-west-2
output=text

cdk deploy --profile profile2

I tried various things and this finally helped.我尝试了各种方法,这终于有所帮助。 Just a quick note, I never added an output until the last one.只是一个简短的说明,直到最后一个,我才添加了 output。 Maybe a completely filled out profile helped as well.也许完整填写的个人资料也有帮助。

reference: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html参考: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html

sudo was killing me... sudo 杀了我...

I was getting the same error for cdk deploy.我在 cdk deploy 中遇到了同样的错误。 I kept running:我一直在跑:

$ sudo cdk deploy --profile default

and getting the same error.并得到同样的错误。 I eventually used -v and read every line and realized it wasn't using my .aws/credentials file, but was looking for /root/.aws/credentials .我最终使用 -v 并阅读了每一行,并意识到它没有使用我的.aws/credentials文件,而是在寻找/root/.aws/credentials

Removed sudo and everything worked just fine.删除了 sudo ,一切正常。

$ cdk deploy --profile default

In my case, when I specified my credentials using aws configure , when I pasted the apiKey it included an special character in the beginning of the credential causing this issue.就我而言,当我使用aws configure指定我的凭据时,当我粘贴 apiKey 时,它在导致此问题的凭据开头包含一个特殊字符。 So I edited the ./aws/credentials file and removed it.所以我编辑了./aws/credentials文件并将其删除。

I tried everything and in the end it was the unadjusted clock of WSL2我什么都试过了,最后还是WSL2的未调时钟

Fixed with固定在

sudo ntpdate pool.ntp.org

For me it seemed the CDK had cached a bad session and wouldn't clear it.对我来说,CDK 似乎缓存了一个错误的 session 并且不会清除它。 I deleted the following directory in my home directory我在我的主目录中删除了以下目录

    rm -Rf ~/.cdk/cache

That worked for me as it forced creation of a new cache.这对我有用,因为它强制创建了一个新的缓存。 (be careful you're deleting the correct dir:) (小心你删除了正确的目录:)

in my case, i change region name to region code就我而言,我将区域名称更改为区域代码

from: Asia Pacific (Singapore) to: ap-southeast-1从:亚太地区(新加坡)到:ap-southeast-1

I use AWS SSO and sometimes experience this issue when my session has expired.我使用 AWS SSO,有时会在我的 session 过期时遇到此问题。 Calling aws sso login will still seem like it's working fine for a minute or two, but calling cdk deploy (or something similar) already fails.调用aws sso login似乎仍然可以正常工作一两分钟,但调用cdk deploy (或类似的东西)已经失败。

Just type只需键入

aws configure aws 配置

in your shell and then type在你的 shell 然后输入

aws_access_key_id aws_access_key_id

aws_secret_access_key aws_secret_access_key

then enter your region然后输入您的地区

if you are not confirm with your region sign in to Console如果您没有确认您所在的地区,请登录控制台

then have a look at the end of url-bar im sure you find it.然后看看 url-bar 的结尾,我确定你找到了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法解析要使用的 AWS 帐户。 必须在定义 CDK 时或通过环境进行配置 - ERROR: Unable to resolve AWS account to use. It must be either configured when you define your CDK or through the environment 无法解析要使用的 AWS 账户。 它必须在您定义 CDK 堆栈时进行配置,或者通过环境进行配置 - Unable to resolve AWS account to use. It must be either configured when you define your CDK Stack, or through the environment 在 docker 容器中运行 CDK 时无法解析要使用的 AWS 账户 - Unable to resolve AWS account to use when running CDK in a docker container 安装 AWS CDK 构造库时出现“ERESOLVE 无法解析依赖关系树” - “ERESOLVE unable to resolve dependency tree” when installing AWS CDK construct library AWS CDK 解析错误:无法使用循环引用解析 object 树 - AWS CDK Resolution error: Unable to resolve object tree with circular reference 如何使用 AWS-CDK 定义“AWS Backup”跨账户复制作业 - How to define an "AWS Backup" Cross-Account-CopyJob with AWS-CDK 如何将其他开发人员添加到您的AWS账户中 - How do you add additional developers to be part of your AWS account 无法从 CDK 定义“FARGATE”AWS Batch 计算环境 - Can't define 'FARGATE' AWS Batch compute environment from CDK 当我获得 AWS 账户的临时凭证授权时,如何在 CDK 中指定账户 ID? - How can I specify Account ID in CDK when I'm authorized with temp credentials to the AWS account? AWS CDK:如何在另一个账户中访问 VPC - AWS CDK: How to access VPC in another account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM