-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): automatically determine region on EC2 instances (#9313)
Enables EC2 instances to automatically determine their current region by querying the Instance Metadata Service (IMDS). Both IMDSv1 and v2 are supported. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
5 changed files
with
205 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as AWS from 'aws-sdk'; | ||
import { AwsCliCompatible } from '../../lib/api/aws-auth/awscli-compatible'; | ||
import { withMockedClassSingleton } from '../util'; | ||
|
||
beforeEach(() => { | ||
// Set to paths that don't exist so the SDK doesn't accidentally load this config | ||
process.env.AWS_CONFIG_FILE = '/home/dummydummy/.bxt/config'; | ||
process.env.AWS_SHARED_CREDENTIALS_FILE = '/home/dummydummy/.bxt/credentials'; | ||
// Scrub some environment variables that might be set if we're running on CodeBuild which will interfere with the tests. | ||
delete process.env.AWS_REGION; | ||
delete process.env.AWS_DEFAULT_REGION; | ||
delete process.env.AWS_ACCESS_KEY_ID; | ||
delete process.env.AWS_SECRET_ACCESS_KEY; | ||
delete process.env.AWS_SESSION_TOKEN; | ||
}); | ||
|
||
test('on an EC2 instance, region lookup queries IMDS', async () => { | ||
return withMockedClassSingleton(AWS, 'MetadataService', async (mdService) => { | ||
mdService.request | ||
// First call for a token | ||
.mockImplementationOnce((_1, _2, cb) => { cb(undefined as any, 'token'); }) | ||
// Second call for the region | ||
.mockImplementationOnce((_1, _2, cb) => { cb(undefined as any, JSON.stringify({ region: 'some-region' })); }); | ||
|
||
const region = await AwsCliCompatible.region({ ec2instance: true }); | ||
expect(region).toEqual('some-region'); | ||
}); | ||
}); | ||
|