Skip to content

Commit

Permalink
Add function to fetch availability zone id (#2326) (backport #2390) (#…
Browse files Browse the repository at this point in the history
…2409)

Co-authored-by: roy-dydx <[email protected]>
Co-authored-by: Roy Li <[email protected]>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent 3eec191 commit 1dc5934
Show file tree
Hide file tree
Showing 5 changed files with 960 additions and 1 deletion.
1 change: 1 addition & 0 deletions indexer/packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"homepage": "https://github.com/dydxprotocol/indexer#readme",
"dependencies": {
"@aws-sdk/client-ec2": "^3.354.0",
"axios": "^1.2.1",
"big.js": "^6.2.1",
"bignumber.js": "^9.0.2",
Expand Down
51 changes: 51 additions & 0 deletions indexer/packages/base/src/az-id.ts
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 '';
}
}
1 change: 1 addition & 0 deletions indexer/packages/base/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const baseConfigSchema = {
STATSD_PORT: parseInteger({ default: 8125 }),
LOG_LEVEL: parseString({ default: 'debug' }),
ECS_CONTAINER_METADATA_URI_V4: parseString({ default: '' }),
AWS_REGION: parseString({ default: '' }),
};

export default parseSchema(baseConfigSchema);
1 change: 1 addition & 0 deletions indexer/packages/base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './bugsnag';
export * from './stats-util';
export * from './date-helpers';
export * from './instance-id';
export * from './az-id';

// Do this outside logger.ts to avoid a dependency cycle with logger transports that may trigger
// additional logging.
Expand Down
Loading

0 comments on commit 1dc5934

Please sign in to comment.