Skip to content

Commit ce14eb9

Browse files
authored
Merge pull request #1594 from guardian/aa/vpc-param-helper
refactor(cdk): Helper function to adjust CFN parameters for mobile-n10n
2 parents 46ba401 + 3ab035f commit ce14eb9

4 files changed

Lines changed: 86 additions & 33 deletions

File tree

cdk/lib/__snapshots__/registrations-db-proxy.test.ts.snap

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ exports[`The RegistrationDbProxy stack matches the snapshot on CODE 1`] = `
5252
},
5353
"Parameters": {
5454
"VpcId": {
55-
"Default": "/account/vpc/primary/id",
55+
"AllowedValues": [
56+
"/account/vpc/notifications/id",
57+
],
58+
"Default": "/account/vpc/notifications/id",
5659
"Description": "Virtual Private Cloud to run EC2 instances within. Should NOT be the account default VPC.",
5760
"Type": "AWS::SSM::Parameter::Value<AWS::EC2::VPC::Id>",
5861
},
5962
"registrationsdbproxyPrivateSubnets": {
60-
"Default": "/account/vpc/primary/subnets/private",
63+
"AllowedValues": [
64+
"/account/vpc/notifications/subnets/private",
65+
],
66+
"Default": "/account/vpc/notifications/subnets/private",
6167
"Description": "A list of private subnets",
6268
"Type": "AWS::SSM::Parameter::Value<List<AWS::EC2::Subnet::Id>>",
6369
},
@@ -332,12 +338,18 @@ exports[`The RegistrationDbProxy stack matches the snapshot on PROD 1`] = `
332338
},
333339
"Parameters": {
334340
"VpcId": {
335-
"Default": "/account/vpc/primary/id",
341+
"AllowedValues": [
342+
"/account/vpc/notifications/id",
343+
],
344+
"Default": "/account/vpc/notifications/id",
336345
"Description": "Virtual Private Cloud to run EC2 instances within. Should NOT be the account default VPC.",
337346
"Type": "AWS::SSM::Parameter::Value<AWS::EC2::VPC::Id>",
338347
},
339348
"registrationsdbproxyPrivateSubnets": {
340-
"Default": "/account/vpc/primary/subnets/private",
349+
"AllowedValues": [
350+
"/account/vpc/notifications/subnets/private",
351+
],
352+
"Default": "/account/vpc/notifications/subnets/private",
341353
"Description": "A list of private subnets",
342354
"Type": "AWS::SSM::Parameter::Value<List<AWS::EC2::Subnet::Id>>",
343355
},
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((parameter) => !!parameter.default)
17+
.filter((parameter) => {
18+
const defaultValue = parameter.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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ProxyTarget,
1212
} from 'aws-cdk-lib/aws-rds';
1313
import { Secret } from 'aws-cdk-lib/aws-secretsmanager';
14+
import { adjustCloudformationParameters } from './mobile-n10n-compatibility';
1415

1516
interface DbProxyStackProps extends GuStackProps {
1617
appName: string;
@@ -108,5 +109,7 @@ export class RegistrationsDbProxy extends GuStack {
108109
exportName: `RegistrationsDbProxyId-${props.stage}`,
109110
});
110111
}
112+
113+
adjustCloudformationParameters(this);
111114
}
112115
}

cdk/lib/report.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +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';
13+
import { adjustCloudformationParameters } from './mobile-n10n-compatibility';
1414

1515
export interface ReportProps extends GuStackProps {
1616
domainName:
@@ -94,34 +94,7 @@ export class Report extends GuStack {
9494
// Once the YAML template has been removed we should be able to drop this override
9595
vpcParameter.overrideLogicalId('GuCdkVpcId');
9696

97-
// https://github.com/guardian/aws-account-setup/blob/67a516b65e2e151d69687fa61a8a1aa914e8b7c0/packages/cdk/lib/__snapshots__/aws-account-setup.test.ts.snap#L27280-L27327
98-
const vpcParameterName = '/account/vpc/notifications/id';
99-
const privateSubnetsParameterName =
100-
'/account/vpc/notifications/subnets/private';
101-
const publicSubnetsParameterName =
102-
'/account/vpc/notifications/subnets/public';
103-
104-
vpcParameter.default = vpcParameterName;
105-
vpcParameter.allowedValues = [vpcParameterName];
106-
107-
const vpcSubnetsPrivate = this.parameters['reportPrivateSubnets'];
108-
if (vpcSubnetsPrivate) {
109-
vpcSubnetsPrivate.default = privateSubnetsParameterName;
110-
vpcSubnetsPrivate.allowedValues = [privateSubnetsParameterName];
111-
}
112-
113-
const vpcSubnetsPublic = this.parameters['reportPublicSubnets'];
114-
if (vpcSubnetsPublic) {
115-
vpcSubnetsPublic.default = publicSubnetsParameterName;
116-
vpcSubnetsPublic.allowedValues = [publicSubnetsParameterName];
117-
}
118-
119-
// In the Mobile account there are separate artifact buckets for different groups of applications, so we can't use
120-
// the account-wide default
121-
const distBucketParameterName = '/account/services/artifact.bucket.n10n';
122-
const distBucketParameter = GuDistributionBucketParameter.getInstance(this);
123-
distBucketParameter.allowedValues = [distBucketParameterName];
124-
distBucketParameter.default = distBucketParameterName;
97+
adjustCloudformationParameters(this);
12598

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

0 commit comments

Comments
 (0)