Skip to content

Commit 332ce4d

Browse files
authored
fix(cli): wmic not found on modern Windows systems (#17070)
Apparently Microsoft stopped shipping `wmic.exe` in certain situations (modern systems?). We rely on this to detect whether we are on an EC2 instance, so that we do or do not configure the IMDS credential provider (we try to avoid the IMDS credential provider if unnecessary, because in certain network setups it may hang for a long time failing to connect to `169.254.169.254`). If calling `wmic` fails, just assume we're not on an EC2 instance and proceed. Fixes #16419. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7886607 commit 332ce4d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages/aws-cdk/lib/api/aws-auth/awscli-compatible.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,18 @@ async function isEc2Instance() {
177177
let instance = false;
178178
if (process.platform === 'win32') {
179179
// https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/identify_ec2_instances.html
180-
const result = await util.promisify(child_process.exec)('wmic path win32_computersystemproduct get uuid', { encoding: 'utf-8' });
181-
// output looks like
182-
// UUID
183-
// EC2AE145-D1DC-13B2-94ED-01234ABCDEF
184-
const lines = result.stdout.toString().split('\n');
185-
instance = lines.some(x => matchesRegex(/^ec2/i, x));
180+
try {
181+
const result = await util.promisify(child_process.exec)('wmic path win32_computersystemproduct get uuid', { encoding: 'utf-8' });
182+
// output looks like
183+
// UUID
184+
// EC2AE145-D1DC-13B2-94ED-01234ABCDEF
185+
const lines = result.stdout.toString().split('\n');
186+
instance = lines.some(x => matchesRegex(/^ec2/i, x));
187+
} catch (e) {
188+
// Modern machines may not have wmic.exe installed. No reason to fail, just assume it's not an EC2 instance.
189+
debug(`Checking using WMIC failed, assuming NOT an EC2 instance: ${e.message} (pass --ec2creds to force)`);
190+
instance = false;
191+
}
186192
} else {
187193
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
188194
const files: Array<[string, RegExp]> = [

0 commit comments

Comments
 (0)