Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): region specified in ~/.aws/credentials is ignored #32133

Merged
merged 12 commits into from
Nov 14, 2024
9 changes: 7 additions & 2 deletions packages/aws-cdk/lib/api/aws-auth/awscli-compatible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createCredentialChain, fromEnv, fromIni, fromNodeProviderChain } from '
import { MetadataService } from '@aws-sdk/ec2-metadata-service';
import type { NodeHttpHandlerOptions } from '@smithy/node-http-handler';
import { loadSharedConfigFiles } from '@smithy/shared-ini-file-loader';
import { AwsCredentialIdentityProvider, Logger } from '@smithy/types';
import { AwsCredentialIdentityProvider, Logger, ParsedIniData } from '@smithy/types';
import * as promptly from 'promptly';
import type { SdkHttpOptions } from './sdk-provider';
import { readIfPossible } from './util';
Expand Down Expand Up @@ -149,7 +149,12 @@ export class AwsCliCompatible {
*/
async function getRegionFromIni(profile: string): Promise<string | undefined> {
const sharedFiles = await loadSharedConfigFiles({ ignoreCache: true });
return sharedFiles?.configFile?.[profile]?.region || sharedFiles?.configFile?.default?.region;
// https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-files.html
return getRegionFromIniFile(profile, sharedFiles.configFile) ?? getRegionFromIniFile(profile, sharedFiles.credentialsFile);
iliapolo marked this conversation as resolved.
Show resolved Hide resolved
}

function getRegionFromIniFile(profile: string, data?: ParsedIniData) {
return data?.[profile]?.region ?? data?.default?.region;
}

function tryGetCACert(bundlePath?: string) {
Expand Down
Loading