Skip to content

Commit 8b7a6f8

Browse files
committed
feat(cdk): Generically adjust the distribution bucket CFN parameter
1 parent e0b946d commit 8b7a6f8

4 files changed

Lines changed: 69 additions & 37 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type { GuStack } from '@guardian/cdk/lib/constructs/core';
2+
3+
/**
4+
* Adjusts CloudFormation parameters in a given stack (if they exist).
5+
*
6+
* @param stack - The stack to adjust.
7+
* @param currentDefaultValue - The current default value to look for. Can be a prefix, or a full value.
8+
* @param desiredDefaultValue - The desired default value to replace with. Can be a prefix, or a full value.
9+
*/
10+
function adjustParameter(
11+
stack: GuStack,
12+
currentDefaultValue: string,
13+
desiredDefaultValue: string,
14+
) {
15+
const parameters = Object.values(stack.parameters)
16+
.filter((_) => !!_.default)
17+
.filter((_) => {
18+
const defaultValue = _.default as string;
19+
20+
// Use `startsWith` to allow for partial matches (i.e. prefixes)
21+
return defaultValue.startsWith(currentDefaultValue);
22+
});
23+
24+
parameters.forEach((parameter) => {
25+
parameter.default = (parameter.default as string).replace(
26+
currentDefaultValue,
27+
desiredDefaultValue,
28+
);
29+
parameter.allowedValues = [parameter.default];
30+
});
31+
}
32+
33+
/**
34+
* Adjusts VPC-related CloudFormation parameters in the given stack to use the `notifications` VPC.
35+
* @see // https://github.com/guardian/aws-account-setup/blob/67a516b65e2e151d69687fa61a8a1aa914e8b7c0/packages/cdk/lib/__snapshots__/aws-account-setup.test.ts.snap#L27280-L27327
36+
*/
37+
function adjustVpcParameters(stack: GuStack) {
38+
adjustParameter(
39+
stack,
40+
'/account/vpc/primary/',
41+
'/account/vpc/notifications/',
42+
);
43+
}
44+
45+
/**
46+
* Adjusts the artifact bucket parameter for n10n services.
47+
* In the Mobile account there are separate artifact buckets for different groups of applications,
48+
* so we can't use the account-wide default.
49+
*/
50+
function adjustArtifactBucketParameter(stack: GuStack) {
51+
adjustParameter(
52+
stack,
53+
'/account/services/artifact.bucket',
54+
'/account/services/artifact.bucket.n10n',
55+
);
56+
}
57+
58+
/**
59+
* Applications within the `mobile-n10n` stack do not use account (/GuCDK) defaults.
60+
* This adjusts CloudFormation parameters with values suitable for the `mobile-n10n` stack.
61+
*/
62+
export function adjustCloudformationParameters(stack: GuStack) {
63+
adjustVpcParameters(stack);
64+
adjustArtifactBucketParameter(stack);
65+
}

cdk/lib/registrations-db-proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
ProxyTarget,
1212
} from 'aws-cdk-lib/aws-rds';
1313
import { Secret } from 'aws-cdk-lib/aws-secretsmanager';
14-
import { adjustVpcParameters } from './vpc';
14+
import { adjustCloudformationParameters } from './mobile-n10n-compatibility';
1515

1616
interface DbProxyStackProps extends GuStackProps {
1717
appName: string;
@@ -110,6 +110,6 @@ export class RegistrationsDbProxy extends GuStack {
110110
});
111111
}
112112

113-
adjustVpcParameters(this);
113+
adjustCloudformationParameters(this);
114114
}
115115
}

cdk/lib/report.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import { AccessScope } from '@guardian/cdk/lib/constants';
33
import type { GuStackProps } from '@guardian/cdk/lib/constructs/core';
44
import { GuVpcParameter } from '@guardian/cdk/lib/constructs/core';
55
import { GuLoggingStreamNameParameter } from '@guardian/cdk/lib/constructs/core';
6-
import { GuDistributionBucketParameter } from '@guardian/cdk/lib/constructs/core';
76
import { GuStack } from '@guardian/cdk/lib/constructs/core';
87
import { GuCname } from '@guardian/cdk/lib/constructs/dns';
98
import { GuAllowPolicy } from '@guardian/cdk/lib/constructs/iam';
109
import type { App } from 'aws-cdk-lib';
1110
import { Duration } from 'aws-cdk-lib';
1211
import type { CfnAutoScalingGroup } from 'aws-cdk-lib/aws-autoscaling';
1312
import { InstanceClass, InstanceSize, InstanceType } from 'aws-cdk-lib/aws-ec2';
14-
import { adjustVpcParameters } from './vpc';
13+
import { adjustCloudformationParameters } from './mobile-n10n-compatibility';
1514

1615
export interface ReportProps extends GuStackProps {
1716
domainName:
@@ -95,14 +94,7 @@ export class Report extends GuStack {
9594
// Once the YAML template has been removed we should be able to drop this override
9695
vpcParameter.overrideLogicalId('GuCdkVpcId');
9796

98-
adjustVpcParameters(this);
99-
100-
// In the Mobile account there are separate artifact buckets for different groups of applications, so we can't use
101-
// the account-wide default
102-
const distBucketParameterName = '/account/services/artifact.bucket.n10n';
103-
const distBucketParameter = GuDistributionBucketParameter.getInstance(this);
104-
distBucketParameter.allowedValues = [distBucketParameterName];
105-
distBucketParameter.default = distBucketParameterName;
97+
adjustCloudformationParameters(this);
10698

10799
// In the Mobile account there are separate Kinesis streams for CODE and PROD, so we can't use the account-wide
108100
// default

cdk/lib/vpc.ts

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

0 commit comments

Comments
 (0)