-
Notifications
You must be signed in to change notification settings - Fork 125
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
Add function to fetch availability zone id (#2326) #2390
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||
import { DescribeAvailabilityZonesCommand, EC2Client } from '@aws-sdk/client-ec2'; | ||||||
|
||||||
import { axiosRequest } from './axios'; | ||||||
import config from './config'; | ||||||
import logger from './logger'; | ||||||
|
||||||
export async function getAvailabilityZoneId(): Promise<string> { | ||||||
if (config.ECS_CONTAINER_METADATA_URI_V4 !== '' && config.AWS_REGION !== '') { | ||||||
const taskUrl = `${config.ECS_CONTAINER_METADATA_URI_V4}/task`; | ||||||
try { | ||||||
const response = await axiosRequest({ | ||||||
method: 'GET', | ||||||
url: taskUrl, | ||||||
}) as { AvailabilityZone: string }; | ||||||
const client = new EC2Client({ region: config.AWS_REGION }); | ||||||
const command = new DescribeAvailabilityZonesCommand({ | ||||||
ZoneNames: [response.AvailabilityZone], | ||||||
}); | ||||||
try { | ||||||
const ec2Response = await client.send(command); | ||||||
const zoneId = ec2Response.AvailabilityZones![0].ZoneId!; | ||||||
logger.info({ | ||||||
at: 'az-id#getAvailabilityZoneId', | ||||||
message: `Got availability zone id ${zoneId}.`, | ||||||
}); | ||||||
return ec2Response.AvailabilityZones![0].ZoneId!; | ||||||
} catch (error) { | ||||||
logger.error({ | ||||||
at: 'az-id#getAvailabilityZoneId', | ||||||
message: 'Failed to fetch availabilty zone id from EC2. ', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typographical error in log message There's a typo in the log message: "availabilty" should be "availability". Apply the following diff to fix the typo: - message: 'Failed to fetch availabilty zone id from EC2. ',
+ message: 'Failed to fetch availability zone id from EC2. ', 📝 Committable suggestion
Suggested change
|
||||||
error, | ||||||
}); | ||||||
return ''; | ||||||
} | ||||||
} catch (error) { | ||||||
logger.error({ | ||||||
at: 'az-id#getAvailabilityZoneId', | ||||||
message: 'Failed to retrieve availability zone from metadata endpoint. No availabilty zone id found.', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typographical error in log message There's a typo in the log message: "availabilty" should be "availability". Apply the following diff to fix the typo: - message: 'Failed to retrieve availability zone from metadata endpoint. No availabilty zone id found.',
+ message: 'Failed to retrieve availability zone from metadata endpoint. No availability zone id found.', 📝 Committable suggestion
Suggested change
|
||||||
error, | ||||||
taskUrl, | ||||||
}); | ||||||
return ''; | ||||||
} | ||||||
} else { | ||||||
logger.error({ | ||||||
at: 'az-id#getAvailabilityZoneId', | ||||||
message: 'No metadata URI or region. No availabilty zone id found.', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typographical error in log message There's a typo in the log message: "availabilty" should be "availability". Apply the following diff to fix the typo: - message: 'No metadata URI or region. No availabilty zone id found.',
+ message: 'No metadata URI or region. No availability zone id found.', 📝 Committable suggestion
Suggested change
|
||||||
}); | ||||||
return ''; | ||||||
} | ||||||
} |
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.
Ensure
AvailabilityZones
data is valid before accessingThe code assumes that
ec2Response.AvailabilityZones
is defined and contains at least one element. Accessingec2Response.AvailabilityZones![0].ZoneId!
without proper checks may lead to runtime errors if the array isundefined
or empty. Please add a check to confirm thatec2Response.AvailabilityZones
exists and has at least one element before accessing it.Consider modifying the code as follows:
📝 Committable suggestion