-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
955 additions
and
2 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
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. ', | ||
error, | ||
}); | ||
return ''; | ||
} | ||
} catch (error) { | ||
logger.error({ | ||
at: 'az-id#getAvailabilityZoneId', | ||
message: 'Failed to retrieve availability zone from metadata endpoint. No availabilty zone id found.', | ||
error, | ||
taskUrl, | ||
}); | ||
return ''; | ||
} | ||
} else { | ||
logger.error({ | ||
at: 'az-id#getAvailabilityZoneId', | ||
message: 'No metadata URI or region. No availabilty zone id found.', | ||
}); | ||
return ''; | ||
} | ||
} |
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
Oops, something went wrong.