Skip to content

Commit

Permalink
chore: refactor lib for single region deployment (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-clickup authored Jan 4, 2024
1 parent 42d0243 commit 44416b0
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 112 deletions.
25 changes: 0 additions & 25 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 23 additions & 34 deletions src/service-quotas-metric-publisher.monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { IServiceQuota } from './service-quotas-metric-publisher';
interface ServiceQuotaApplied extends IServiceQuota {
quotaName: string | undefined;
value: number | undefined;
region: string | undefined;
}

/**
Expand All @@ -37,44 +36,35 @@ export const monitor = async () => {
throw new Error('SERVICE_QUOTAS_LIST environment variable not set');
}

if (!process.env.REGIONS_TO_MONITOR) {
throw new Error('REGIONS_TO_MONITOR environment variable not set');
}

const cwNamespace: string = process.env.CW_NAMESPACE;
const serviceQuotas: IServiceQuota[] = JSON.parse(process.env.SERVICE_QUOTAS_LIST);
const regionsToMonitor: string[] = JSON.parse(process.env.REGIONS_TO_MONITOR);

// Call the getServiceQuota API to get the information about the quota
const servicesQuotasApplied: ServiceQuotaApplied[] = [];
for (const region of regionsToMonitor) {
console.log(`Getting service quotas for region ${region}`);
const servicequotas = new ServiceQuotasClient({ region });
for (const serviceQuota of serviceQuotas) {
console.log(`Getting service quota for ${serviceQuota.serviceCode} - ${serviceQuota.quotaCode}`);
const params: GetServiceQuotaCommandInput = {
ServiceCode: serviceQuota.serviceCode,
QuotaCode: serviceQuota.quotaCode,
};
const command = new GetServiceQuotaCommand(params);
const data = await servicequotas.send(command);
if (!data.Quota) {
console.error(`No quota found for ${serviceQuota.serviceCode} - ${serviceQuota.quotaCode}`);
continue;
}
console.log(
`Successfully called getServiceQuota for ${serviceQuota.serviceCode} - ${
serviceQuota.quotaCode
}.\n Data: ${JSON.stringify(data)}`,
);
servicesQuotasApplied.push({
serviceCode: data.Quota?.ServiceCode as String,
quotaName: data.Quota?.QuotaName as String,
quotaCode: data.Quota?.QuotaCode as String,
value: data.Quota?.Value,
region: region,
});
const servicequotas = new ServiceQuotasClient();
for (const serviceQuota of serviceQuotas) {
console.log(`Getting service quota for ${serviceQuota.serviceCode} - ${serviceQuota.quotaCode}`);
const params: GetServiceQuotaCommandInput = {
ServiceCode: serviceQuota.serviceCode,
QuotaCode: serviceQuota.quotaCode,
};
const command = new GetServiceQuotaCommand(params);
const data = await servicequotas.send(command);
if (!data.Quota || Object.keys(data.Quota).length === 0) {
console.error(`No quota found for ${serviceQuota.serviceCode} - ${serviceQuota.quotaCode}`);
continue;
}
console.log(
`Successfully called getServiceQuota for ${serviceQuota.serviceCode} - ${
serviceQuota.quotaCode
}.\n Data: ${JSON.stringify(data)}`,
);
servicesQuotasApplied.push({
serviceCode: data.Quota?.ServiceCode as String,
quotaName: data.Quota?.QuotaName as String,
quotaCode: data.Quota?.QuotaCode as String,
value: data.Quota?.Value,
});
}
// Publish the metric data to CloudWatch
const cloudwatch = new CloudWatchClient();
Expand All @@ -83,7 +73,6 @@ export const monitor = async () => {
{ Name: 'QuotaCode', Value: serviceQuota.quotaCode },
{ Name: 'QuotaName', Value: serviceQuota.quotaName },
{ Name: 'ServiceCode', Value: serviceQuota.serviceCode },
{ Name: 'AwsRegion', Value: serviceQuota.region },
];
const metricData: MetricDatum[] = [
{
Expand Down
8 changes: 0 additions & 8 deletions src/service-quotas-metric-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,13 @@ export interface ServiceQuotasMetricPublisherProps {
* @default ServiceQuotas[]
*/
readonly serviceQuotas?: IServiceQuota[];
/**
* The list of regions to monitor the quotas.
* @default ['us-east-1']
*/
readonly regionsToMonitor?: string[];
}

/**
* A construct that creates an AWS Lambda function to publish applied service quotas metrics to CloudWatch.
*/
export class ServiceQuotasMetricPublisher extends Construct {
readonly publishFrequency: number;
readonly regionsToMonitor: string[];
readonly handler: aws_lambda_nodejs.NodejsFunction;
readonly rule: aws_events.Rule;
readonly cwNamespace: string;
Expand All @@ -64,7 +58,6 @@ export class ServiceQuotasMetricPublisher extends Construct {
constructor(scope: Construct, id: Namer, props: ServiceQuotasMetricPublisherProps) {
super(scope, id.pascal);
this.publishFrequency = props.publishFrequency ?? 1;
this.regionsToMonitor = props.regionsToMonitor ?? ['us-east-1'];
this.serviceQuotas = props.serviceQuotas ?? [];
this.cwNamespace = props.cwNamespace ?? 'AWS/ServiceQuotaLimit';
const myConstruct = this;
Expand Down Expand Up @@ -105,7 +98,6 @@ export class ServiceQuotasMetricPublisher extends Construct {

this.handler.addEnvironment('CW_NAMESPACE', props.cwNamespace ?? this.cwNamespace);
this.handler.addEnvironment('SERVICE_QUOTAS_LIST', JSON.stringify(this.serviceQuotas));
this.handler.addEnvironment('REGIONS_TO_MONITOR', JSON.stringify(props.regionsToMonitor ?? this.regionsToMonitor));
this.rule = new aws_events.Rule(this, 'rule', {
schedule: aws_events.Schedule.rate(Duration.minutes(this.publishFrequency)),
});
Expand Down
1 change: 0 additions & 1 deletion test/integ.service-quotas-metric-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class BaselineStack extends Stack {
super(scope, id.pascal, props);
this.lambdaFunction = new ServiceQuotasMetricPublisher(this, id, {
publishFrequency: 1,
regionsToMonitor: ['us-west-2'],
cwNamespace: 'AWS/ServiceQuotaLimit',
serviceQuotas: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"version": "34.0.0",
"files": {
"bd4479eeabd60939d305de637c84482dd0f2cb69729428a7372da67784eb3c63": {
"b5ae93b59b14215a5db218f1bbf46b72944e9fa6d91538a1022fcf9c61910186": {
"source": {
"path": "asset.bd4479eeabd60939d305de637c84482dd0f2cb69729428a7372da67784eb3c63",
"path": "asset.b5ae93b59b14215a5db218f1bbf46b72944e9fa6d91538a1022fcf9c61910186",
"packaging": "zip"
},
"destinations": {
"425845004253-us-west-2": {
"bucketName": "cdk-hnb659fds-assets-425845004253-us-west-2",
"objectKey": "bd4479eeabd60939d305de637c84482dd0f2cb69729428a7372da67784eb3c63.zip",
"objectKey": "b5ae93b59b14215a5db218f1bbf46b72944e9fa6d91538a1022fcf9c61910186.zip",
"region": "us-west-2",
"assumeRoleArn": "arn:${AWS::Partition}:iam::425845004253:role/cdk-hnb659fds-file-publishing-role-425845004253-us-west-2"
}
}
},
"aac3d47de5229cb9dfb516ae5d8b3fe42131eff66a86e195003d9297ec00ca58": {
"03c0679af00a26f3425b2d00c1ffb9c5ea048318e58a154fa62514470b891f9c": {
"source": {
"path": "HelperMonitorBaseline.template.json",
"packaging": "file"
},
"destinations": {
"425845004253-us-west-2": {
"bucketName": "cdk-hnb659fds-assets-425845004253-us-west-2",
"objectKey": "aac3d47de5229cb9dfb516ae5d8b3fe42131eff66a86e195003d9297ec00ca58.json",
"objectKey": "03c0679af00a26f3425b2d00c1ffb9c5ea048318e58a154fa62514470b891f9c.json",
"region": "us-west-2",
"assumeRoleArn": "arn:${AWS::Partition}:iam::425845004253:role/cdk-hnb659fds-file-publishing-role-425845004253-us-west-2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"Properties": {
"Code": {
"S3Bucket": "cdk-hnb659fds-assets-425845004253-us-west-2",
"S3Key": "bd4479eeabd60939d305de637c84482dd0f2cb69729428a7372da67784eb3c63.zip"
"S3Key": "b5ae93b59b14215a5db218f1bbf46b72944e9fa6d91538a1022fcf9c61910186.zip"
},
"Environment": {
"Variables": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "34.0.0",
"files": {
"022bca39f4e853a1dd8e2da7a597acf779a4939f2377dbc56bb20da26bb9e3b1": {
"1a0598cc05b30d5f960a82f8e85e49a2525978dfe949e09bb5414f0d91c53cee": {
"source": {
"path": "asset.022bca39f4e853a1dd8e2da7a597acf779a4939f2377dbc56bb20da26bb9e3b1",
"path": "asset.1a0598cc05b30d5f960a82f8e85e49a2525978dfe949e09bb5414f0d91c53cee",
"packaging": "zip"
},
"destinations": {
"425845004253-us-west-2": {
"bucketName": "cdk-hnb659fds-assets-425845004253-us-west-2",
"objectKey": "022bca39f4e853a1dd8e2da7a597acf779a4939f2377dbc56bb20da26bb9e3b1.zip",
"objectKey": "1a0598cc05b30d5f960a82f8e85e49a2525978dfe949e09bb5414f0d91c53cee.zip",
"region": "us-west-2",
"assumeRoleArn": "arn:${AWS::Partition}:iam::425845004253:role/cdk-hnb659fds-file-publishing-role-425845004253-us-west-2"
}
Expand All @@ -29,15 +29,15 @@
}
}
},
"651ca547119600e3824c1d17e56a2c2fef0664b0b05a2744b30611f3df6fa524": {
"a0d393c8bae6105b34d2d9b8ed4d015ed1b6f5deb7fc769cf902b4cbc5664c10": {
"source": {
"path": "MonitorBaselineSevicesQuotas.template.json",
"packaging": "file"
},
"destinations": {
"425845004253-us-west-2": {
"bucketName": "cdk-hnb659fds-assets-425845004253-us-west-2",
"objectKey": "651ca547119600e3824c1d17e56a2c2fef0664b0b05a2744b30611f3df6fa524.json",
"objectKey": "a0d393c8bae6105b34d2d9b8ed4d015ed1b6f5deb7fc769cf902b4cbc5664c10.json",
"region": "us-west-2",
"assumeRoleArn": "arn:${AWS::Partition}:iam::425845004253:role/cdk-hnb659fds-file-publishing-role-425845004253-us-west-2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@
"Properties": {
"Code": {
"S3Bucket": "cdk-hnb659fds-assets-425845004253-us-west-2",
"S3Key": "022bca39f4e853a1dd8e2da7a597acf779a4939f2377dbc56bb20da26bb9e3b1.zip"
"S3Key": "1a0598cc05b30d5f960a82f8e85e49a2525978dfe949e09bb5414f0d91c53cee.zip"
},
"Environment": {
"Variables": {
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1",
"CW_NAMESPACE": "AWS/ServiceQuotaLimit",
"SERVICE_QUOTAS_LIST": "[{\"serviceCode\":\"elasticloadbalancing\",\"quotaCode\":\"L-7E6692B2\"}]",
"REGIONS_TO_MONITOR": "[\"us-west-2\"]"
"SERVICE_QUOTAS_LIST": "[{\"serviceCode\":\"elasticloadbalancing\",\"quotaCode\":\"L-7E6692B2\"}]"
}
},
"Handler": "index.monitor",
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -28752,8 +28752,7 @@ exports.handler = async () => {
Dimensions: [
{ Name: "QuotaCode", Value: "L-7E6692B2" },
{ Name: "QuotaName", Value: "Targets per Application Load Balancer" },
{ Name: "ServiceCode", Value: "elasticloadbalancing" },
{ Name: "AwsRegion", Value: "us-west-2" }
{ Name: "ServiceCode", Value: "elasticloadbalancing" }
]
},
Period: 60,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
}
}
},
"f25d15dfaccdf36c7e0c1efd2f7bd451f9a10403fb5d76028ba0b25f11636ada": {
"168b0e5dd9293011f12fc167ece44e961755d0b0505eb749f4e2a0dea0491ffb": {
"source": {
"path": "integDefaultTestDeployAssert946684A0.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "f25d15dfaccdf36c7e0c1efd2f7bd451f9a10403fb5d76028ba0b25f11636ada.json",
"objectKey": "168b0e5dd9293011f12fc167ece44e961755d0b0505eb749f4e2a0dea0491ffb.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"outputPaths": [
"Payload.body.message"
],
"salt": "1702184856324"
"salt": "1704386003296"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down
Loading

0 comments on commit 44416b0

Please sign in to comment.