-
Notifications
You must be signed in to change notification settings - Fork 634
Description
Checkboxes for prior research
- I've gone through Developer Guide and API reference
- I've checked AWS Forums and StackOverflow.
- I've searched for previous similar issues and didn't find any solution.
Describe the bug
I am currently using amazon-ec2-metadata-mock to test using IMDSv2 via the latest @aws-sdk/ec2-metadata-service from my local machine. ec2-metadata-mock sets a default URL+PORT of http://localhost:1338 which works fine for me in a browser.
When I set the environment variable AWS_EC2_METADATA_SERVICE_ENDPOINT
, the URL is respected, but the port number is lost. This is because the two locations in the metadata-service code that call upon import_protocol_http.HttpRequest()
do not pass through the port value;
const request = new HttpRequest({
method: options.method || "GET",
headers: headers,
hostname: endpointUrl.hostname,
path: endpointUrl.pathname + path,
protocol: endpointUrl.protocol,
});
I am able to fix this manually by adding port: endpointUrl.port
to the outputted CJS.
Regression Issue
- Select this option if this issue appears to be a regression.
SDK version number
@aws-sdk/[email protected]
Which JavaScript Runtime is this issue in?
Node.js
Details of the browser/Node.js/ReactNative version
v22.18.0
Reproduction Steps
Download and run the application from amazon-ec2-metadata-mock, then execute the below code with NodeJS (changing the endpoint URL if necessary).
const { MetadataService } = require('@aws-sdk/ec2-metadata-service')
process.env.AWS_EC2_METADATA_SERVICE_ENDPOINT = 'http://localhost:1338'
async function getMetadata() {
try {
const metadataService = new MetadataService()
const instanceIdentity = JSON.parse(await metadataService.request('latest/dynamic/instance-identity/document', {}))
process.env.INSTANCE_ID = instanceIdentity.instanceId
process.exit()
}
catch (err) {
console.error('Error getting instance id: ', err)
process.exit(1)
}
}
getMetadata()
Observed Behavior
The port number in the environment variable AWS_EC2_METADATA_SERVICE_ENDPOINT
is ignored and he request fails because it cannot find the metadata service with the default port.
The returned error is "Error getting instance id: Error: Error making request to the metadata service: AggregateError"
Expected Behavior
The request should follow the port number as well as the URL set by the environment variable AWS_EC2_METADATA_SERVICE_ENDPOINT
and see the metadata service.
Possible Solution
Replace all cases of the following code:
const request = new HttpRequest({
method: options.method || "GET",
headers: headers,
hostname: endpointUrl.hostname,
path: endpointUrl.pathname + path,
protocol: endpointUrl.protocol,
});
with this code:
const request = new HttpRequest({
method: options.method || "GET",
headers: headers,
hostname: endpointUrl.hostname,
path: endpointUrl.pathname + path,
protocol: endpointUrl.protocol,
port: endpointUrl.port,
});
Additional Information/Context
No response