Skip to content
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) (backport #2390) #2409

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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