Skip to content

Commit 44416b0

Browse files
authored
chore: refactor lib for single region deployment (#6)
1 parent 42d0243 commit 44416b0

File tree

18 files changed

+50
-112
lines changed

18 files changed

+50
-112
lines changed

API.md

Lines changed: 0 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/service-quotas-metric-publisher.monitor.ts

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { IServiceQuota } from './service-quotas-metric-publisher';
2020
interface ServiceQuotaApplied extends IServiceQuota {
2121
quotaName: string | undefined;
2222
value: number | undefined;
23-
region: string | undefined;
2423
}
2524

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

40-
if (!process.env.REGIONS_TO_MONITOR) {
41-
throw new Error('REGIONS_TO_MONITOR environment variable not set');
42-
}
43-
4439
const cwNamespace: string = process.env.CW_NAMESPACE;
4540
const serviceQuotas: IServiceQuota[] = JSON.parse(process.env.SERVICE_QUOTAS_LIST);
46-
const regionsToMonitor: string[] = JSON.parse(process.env.REGIONS_TO_MONITOR);
4741

4842
// Call the getServiceQuota API to get the information about the quota
4943
const servicesQuotasApplied: ServiceQuotaApplied[] = [];
50-
for (const region of regionsToMonitor) {
51-
console.log(`Getting service quotas for region ${region}`);
52-
const servicequotas = new ServiceQuotasClient({ region });
53-
for (const serviceQuota of serviceQuotas) {
54-
console.log(`Getting service quota for ${serviceQuota.serviceCode} - ${serviceQuota.quotaCode}`);
55-
const params: GetServiceQuotaCommandInput = {
56-
ServiceCode: serviceQuota.serviceCode,
57-
QuotaCode: serviceQuota.quotaCode,
58-
};
59-
const command = new GetServiceQuotaCommand(params);
60-
const data = await servicequotas.send(command);
61-
if (!data.Quota) {
62-
console.error(`No quota found for ${serviceQuota.serviceCode} - ${serviceQuota.quotaCode}`);
63-
continue;
64-
}
65-
console.log(
66-
`Successfully called getServiceQuota for ${serviceQuota.serviceCode} - ${
67-
serviceQuota.quotaCode
68-
}.\n Data: ${JSON.stringify(data)}`,
69-
);
70-
servicesQuotasApplied.push({
71-
serviceCode: data.Quota?.ServiceCode as String,
72-
quotaName: data.Quota?.QuotaName as String,
73-
quotaCode: data.Quota?.QuotaCode as String,
74-
value: data.Quota?.Value,
75-
region: region,
76-
});
44+
const servicequotas = new ServiceQuotasClient();
45+
for (const serviceQuota of serviceQuotas) {
46+
console.log(`Getting service quota for ${serviceQuota.serviceCode} - ${serviceQuota.quotaCode}`);
47+
const params: GetServiceQuotaCommandInput = {
48+
ServiceCode: serviceQuota.serviceCode,
49+
QuotaCode: serviceQuota.quotaCode,
50+
};
51+
const command = new GetServiceQuotaCommand(params);
52+
const data = await servicequotas.send(command);
53+
if (!data.Quota || Object.keys(data.Quota).length === 0) {
54+
console.error(`No quota found for ${serviceQuota.serviceCode} - ${serviceQuota.quotaCode}`);
55+
continue;
7756
}
57+
console.log(
58+
`Successfully called getServiceQuota for ${serviceQuota.serviceCode} - ${
59+
serviceQuota.quotaCode
60+
}.\n Data: ${JSON.stringify(data)}`,
61+
);
62+
servicesQuotasApplied.push({
63+
serviceCode: data.Quota?.ServiceCode as String,
64+
quotaName: data.Quota?.QuotaName as String,
65+
quotaCode: data.Quota?.QuotaCode as String,
66+
value: data.Quota?.Value,
67+
});
7868
}
7969
// Publish the metric data to CloudWatch
8070
const cloudwatch = new CloudWatchClient();
@@ -83,7 +73,6 @@ export const monitor = async () => {
8373
{ Name: 'QuotaCode', Value: serviceQuota.quotaCode },
8474
{ Name: 'QuotaName', Value: serviceQuota.quotaName },
8575
{ Name: 'ServiceCode', Value: serviceQuota.serviceCode },
86-
{ Name: 'AwsRegion', Value: serviceQuota.region },
8776
];
8877
const metricData: MetricDatum[] = [
8978
{

src/service-quotas-metric-publisher.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,13 @@ export interface ServiceQuotasMetricPublisherProps {
3636
* @default ServiceQuotas[]
3737
*/
3838
readonly serviceQuotas?: IServiceQuota[];
39-
/**
40-
* The list of regions to monitor the quotas.
41-
* @default ['us-east-1']
42-
*/
43-
readonly regionsToMonitor?: string[];
4439
}
4540

4641
/**
4742
* A construct that creates an AWS Lambda function to publish applied service quotas metrics to CloudWatch.
4843
*/
4944
export class ServiceQuotasMetricPublisher extends Construct {
5045
readonly publishFrequency: number;
51-
readonly regionsToMonitor: string[];
5246
readonly handler: aws_lambda_nodejs.NodejsFunction;
5347
readonly rule: aws_events.Rule;
5448
readonly cwNamespace: string;
@@ -64,7 +58,6 @@ export class ServiceQuotasMetricPublisher extends Construct {
6458
constructor(scope: Construct, id: Namer, props: ServiceQuotasMetricPublisherProps) {
6559
super(scope, id.pascal);
6660
this.publishFrequency = props.publishFrequency ?? 1;
67-
this.regionsToMonitor = props.regionsToMonitor ?? ['us-east-1'];
6861
this.serviceQuotas = props.serviceQuotas ?? [];
6962
this.cwNamespace = props.cwNamespace ?? 'AWS/ServiceQuotaLimit';
7063
const myConstruct = this;
@@ -105,7 +98,6 @@ export class ServiceQuotasMetricPublisher extends Construct {
10598

10699
this.handler.addEnvironment('CW_NAMESPACE', props.cwNamespace ?? this.cwNamespace);
107100
this.handler.addEnvironment('SERVICE_QUOTAS_LIST', JSON.stringify(this.serviceQuotas));
108-
this.handler.addEnvironment('REGIONS_TO_MONITOR', JSON.stringify(props.regionsToMonitor ?? this.regionsToMonitor));
109101
this.rule = new aws_events.Rule(this, 'rule', {
110102
schedule: aws_events.Schedule.rate(Duration.minutes(this.publishFrequency)),
111103
});

test/integ.service-quotas-metric-publisher.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export class BaselineStack extends Stack {
1515
super(scope, id.pascal, props);
1616
this.lambdaFunction = new ServiceQuotasMetricPublisher(this, id, {
1717
publishFrequency: 1,
18-
regionsToMonitor: ['us-west-2'],
1918
cwNamespace: 'AWS/ServiceQuotaLimit',
2019
serviceQuotas: [
2120
{

test/integ.service-quotas-metric-publisher.ts.snapshot/HelperMonitorBaseline.assets.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
{
22
"version": "34.0.0",
33
"files": {
4-
"bd4479eeabd60939d305de637c84482dd0f2cb69729428a7372da67784eb3c63": {
4+
"b5ae93b59b14215a5db218f1bbf46b72944e9fa6d91538a1022fcf9c61910186": {
55
"source": {
6-
"path": "asset.bd4479eeabd60939d305de637c84482dd0f2cb69729428a7372da67784eb3c63",
6+
"path": "asset.b5ae93b59b14215a5db218f1bbf46b72944e9fa6d91538a1022fcf9c61910186",
77
"packaging": "zip"
88
},
99
"destinations": {
1010
"425845004253-us-west-2": {
1111
"bucketName": "cdk-hnb659fds-assets-425845004253-us-west-2",
12-
"objectKey": "bd4479eeabd60939d305de637c84482dd0f2cb69729428a7372da67784eb3c63.zip",
12+
"objectKey": "b5ae93b59b14215a5db218f1bbf46b72944e9fa6d91538a1022fcf9c61910186.zip",
1313
"region": "us-west-2",
1414
"assumeRoleArn": "arn:${AWS::Partition}:iam::425845004253:role/cdk-hnb659fds-file-publishing-role-425845004253-us-west-2"
1515
}
1616
}
1717
},
18-
"aac3d47de5229cb9dfb516ae5d8b3fe42131eff66a86e195003d9297ec00ca58": {
18+
"03c0679af00a26f3425b2d00c1ffb9c5ea048318e58a154fa62514470b891f9c": {
1919
"source": {
2020
"path": "HelperMonitorBaseline.template.json",
2121
"packaging": "file"
2222
},
2323
"destinations": {
2424
"425845004253-us-west-2": {
2525
"bucketName": "cdk-hnb659fds-assets-425845004253-us-west-2",
26-
"objectKey": "aac3d47de5229cb9dfb516ae5d8b3fe42131eff66a86e195003d9297ec00ca58.json",
26+
"objectKey": "03c0679af00a26f3425b2d00c1ffb9c5ea048318e58a154fa62514470b891f9c.json",
2727
"region": "us-west-2",
2828
"assumeRoleArn": "arn:${AWS::Partition}:iam::425845004253:role/cdk-hnb659fds-file-publishing-role-425845004253-us-west-2"
2929
}

test/integ.service-quotas-metric-publisher.ts.snapshot/HelperMonitorBaseline.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"Properties": {
5858
"Code": {
5959
"S3Bucket": "cdk-hnb659fds-assets-425845004253-us-west-2",
60-
"S3Key": "bd4479eeabd60939d305de637c84482dd0f2cb69729428a7372da67784eb3c63.zip"
60+
"S3Key": "b5ae93b59b14215a5db218f1bbf46b72944e9fa6d91538a1022fcf9c61910186.zip"
6161
},
6262
"Environment": {
6363
"Variables": {

test/integ.service-quotas-metric-publisher.ts.snapshot/MonitorBaselineSevicesQuotas.assets.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "34.0.0",
33
"files": {
4-
"022bca39f4e853a1dd8e2da7a597acf779a4939f2377dbc56bb20da26bb9e3b1": {
4+
"1a0598cc05b30d5f960a82f8e85e49a2525978dfe949e09bb5414f0d91c53cee": {
55
"source": {
6-
"path": "asset.022bca39f4e853a1dd8e2da7a597acf779a4939f2377dbc56bb20da26bb9e3b1",
6+
"path": "asset.1a0598cc05b30d5f960a82f8e85e49a2525978dfe949e09bb5414f0d91c53cee",
77
"packaging": "zip"
88
},
99
"destinations": {
1010
"425845004253-us-west-2": {
1111
"bucketName": "cdk-hnb659fds-assets-425845004253-us-west-2",
12-
"objectKey": "022bca39f4e853a1dd8e2da7a597acf779a4939f2377dbc56bb20da26bb9e3b1.zip",
12+
"objectKey": "1a0598cc05b30d5f960a82f8e85e49a2525978dfe949e09bb5414f0d91c53cee.zip",
1313
"region": "us-west-2",
1414
"assumeRoleArn": "arn:${AWS::Partition}:iam::425845004253:role/cdk-hnb659fds-file-publishing-role-425845004253-us-west-2"
1515
}
@@ -29,15 +29,15 @@
2929
}
3030
}
3131
},
32-
"651ca547119600e3824c1d17e56a2c2fef0664b0b05a2744b30611f3df6fa524": {
32+
"a0d393c8bae6105b34d2d9b8ed4d015ed1b6f5deb7fc769cf902b4cbc5664c10": {
3333
"source": {
3434
"path": "MonitorBaselineSevicesQuotas.template.json",
3535
"packaging": "file"
3636
},
3737
"destinations": {
3838
"425845004253-us-west-2": {
3939
"bucketName": "cdk-hnb659fds-assets-425845004253-us-west-2",
40-
"objectKey": "651ca547119600e3824c1d17e56a2c2fef0664b0b05a2744b30611f3df6fa524.json",
40+
"objectKey": "a0d393c8bae6105b34d2d9b8ed4d015ed1b6f5deb7fc769cf902b4cbc5664c10.json",
4141
"region": "us-west-2",
4242
"assumeRoleArn": "arn:${AWS::Partition}:iam::425845004253:role/cdk-hnb659fds-file-publishing-role-425845004253-us-west-2"
4343
}

test/integ.service-quotas-metric-publisher.ts.snapshot/MonitorBaselineSevicesQuotas.template.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,13 @@
7373
"Properties": {
7474
"Code": {
7575
"S3Bucket": "cdk-hnb659fds-assets-425845004253-us-west-2",
76-
"S3Key": "022bca39f4e853a1dd8e2da7a597acf779a4939f2377dbc56bb20da26bb9e3b1.zip"
76+
"S3Key": "1a0598cc05b30d5f960a82f8e85e49a2525978dfe949e09bb5414f0d91c53cee.zip"
7777
},
7878
"Environment": {
7979
"Variables": {
8080
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1",
8181
"CW_NAMESPACE": "AWS/ServiceQuotaLimit",
82-
"SERVICE_QUOTAS_LIST": "[{\"serviceCode\":\"elasticloadbalancing\",\"quotaCode\":\"L-7E6692B2\"}]",
83-
"REGIONS_TO_MONITOR": "[\"us-west-2\"]"
82+
"SERVICE_QUOTAS_LIST": "[{\"serviceCode\":\"elasticloadbalancing\",\"quotaCode\":\"L-7E6692B2\"}]"
8483
}
8584
},
8685
"Handler": "index.monitor",

test/integ.service-quotas-metric-publisher.ts.snapshot/asset.022bca39f4e853a1dd8e2da7a597acf779a4939f2377dbc56bb20da26bb9e3b1/index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

test/integ.service-quotas-metric-publisher.ts.snapshot/asset.1a0598cc05b30d5f960a82f8e85e49a2525978dfe949e09bb5414f0d91c53cee/index.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)