-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Toolkit: support profiles #517
Conversation
This adds support for AWS profiles to the CDK toolkit. At the same time, it overhauls how the AWS SDK is configured. The configuration via environment variables set at just the right time is removed, and we reimplement some parts of the SDK in an AWS CLI-compatible way to get a consistent view on the account ID and region based on the provided configuration. Fixes a bug in the AWS STS call where it would do two default credential lookups (down to one now). Fixes #480.
packages/aws-cdk/bin/cdk.ts
Outdated
@@ -49,6 +46,7 @@ async function parseCommandLineArguments() { | |||
.option('ignore-errors', { type: 'boolean', default: false, desc: 'Ignores synthesis errors, which will likely produce an invalid output' }) | |||
.option('json', { type: 'boolean', alias: 'j', desc: 'Use JSON output instead of YAML' }) | |||
.option('verbose', { type: 'boolean', alias: 'v', desc: 'Show debug logs' }) | |||
.option('profile', { type: 'string', desc: 'Use the indicated AWS profile' }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
“...to obtain information for the default environment”
packages/aws-cdk/bin/cdk.ts
Outdated
const aws = new SDK(); | ||
const aws = new SDK(argv.profile); | ||
// tslint:disable-next-line:no-console | ||
console.log("Account: ", await aws.defaultAccount(), " region: ", aws.defaultRegion()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use logger
packages/aws-cdk/lib/api/util/sdk.ts
Outdated
// Find the package.json from the main toolkit | ||
const pkg = (require.main as any).require('../package.json'); | ||
this.userAgent = `${pkg.name}/${pkg.version}`; | ||
|
||
// tslint:disable-next-line:no-console | ||
console.log(new Error().stack); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete
packages/aws-cdk/lib/api/util/sdk.ts
Outdated
console.log(new Error().stack); | ||
|
||
// tslint:disable-next-line:no-console | ||
console.log('Profile', profile); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logger
packages/aws-cdk/lib/api/util/sdk.ts
Outdated
const configFile = new SharedIniFile(toCheck.shift()); | ||
const section = configFile.getProfile(profile); | ||
region = section && section.region; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation
packages/aws-cdk/lib/api/util/sdk.ts
Outdated
* variables to be used to determine the region. | ||
*/ | ||
function getCLICompatibleDefaultRegion(profile: string | undefined): string | undefined { | ||
let region = process.env.AWS_REGION || process.env.AMAZON_REGION || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d move this just before the loop so it’s clear that these take precedence
* A reimplementation of JS AWS SDK's SharedIniFile class | ||
* | ||
* We need that class to parse the ~/.aws/config file to determine the correct | ||
* region at runtime, but unfortunately it is private upstream. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mention that we are using an undocumented api in the js sdk to actually parse the ini file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely wanted to open an issue, but I'd like to land this first so that I can point them to the code and say "see, this is what we've had to do to work around the API deficiencies".
Pointing to code is easier than describing :)
This adds support for AWS profiles to the CDK toolkit.
At the same time, it overhauls how the AWS SDK is configured. The
configuration via environment variables set at just the right time
is removed, and we reimplement some parts of the SDK in an
AWS CLI-compatible way to get a consistent view on the account ID
and region based on the provided configuration.
Fixes a bug in the AWS STS call where it would do two default
credential lookups (down to one now).
Fixes #480.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.