How to resolve: 'Unable to resolve AWS account to use. It must be either configured when you define your CDK or through the environment'
Solution 1
Removing [profile default]
from ~/.aws/config
solved it for me.
Solution 2
You need to specify your credentials. Check out this article: 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. The CDK looks for credentials and region in the following order:
Using the --profile option to cdk commands.
Using environment variables.
Using the default profile as set by the AWS Command Line Interface (AWS CLI).
You can set up a profile using the AWS CLI. See https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html for details on how to do this. You can specify named profiles, such as a profile for each account, and then specify the profile name to use for your CDK call.
Solution 3
Finally I used following in C:\XXXX.aws\credentials
[default]
aws_access_key_id=XXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXX
and 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
Error: AWS region must be configured either when you configure your CDK stack or through the environment
Solution 4
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:"
I tried different versions of CDK and nothing, still the same error. 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.
Solution 5
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.
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, e.g. --profile option.
https://docs.aws.amazon.com/cdk/latest/guide/environments.html
Drew Gallagher
Updated on January 28, 2022Comments
-
Drew Gallagher over 2 years
I am trying to run CDK commands to check the diff of my local and remote stack.
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.