Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1a3ee57

Browse files
committedFeb 16, 2022
feat: added network configuration array
1 parent 90550e6 commit 1a3ee57

File tree

5 files changed

+130
-130
lines changed

5 files changed

+130
-130
lines changed
 

‎package.json

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

‎src/constructs/autoScalingGroup.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ import { CfnTargetGroup, NetworkTargetGroupProps } from '@aws-cdk/aws-elasticloa
44
import * as iam from '@aws-cdk/aws-iam';
55
import { Effect, PolicyStatement, ServicePrincipal } from '@aws-cdk/aws-iam';
66
import * as cdk from '@aws-cdk/core';
7+
import { LoadBalancerProps } from './network';
78

89
export interface InternalVPC {
910
readonly type: string;
1011
readonly vpcName: string;
1112
readonly vpcProps?: VpcProps;
1213
}
1314

15+
export interface NetworkProps {
16+
readonly protocol: string;
17+
readonly port: number;
18+
readonly healthCheckPath: string;
19+
readonly sslEnabled: boolean;
20+
readonly host: string;
21+
readonly lbArn: string;
22+
}
23+
1424
export interface IngressRule {
1525
readonly sourceSG: string;
1626
readonly description?: string;
@@ -83,15 +93,17 @@ export interface AutoScalerProps {
8393
readonly tags?: CfnAutoScalingGroup.TagPropertyProperty[];
8494
readonly tgProps?: TargetGroupProps;
8595
readonly subnets: string[];
96+
readonly networkProps: NetworkProps[];
97+
readonly appName: string;
8698
}
8799

88100
export class AutoScaler extends cdk.Resource {
89-
public readonly targetGroupArn: string;
101+
public readonly loadBalancerProperties?: LoadBalancerProps[];
90102
constructor(scope: cdk.Construct, id: string, props: AutoScalerProps) {
91103
super(scope, id);
92104

93105
const launchTemplate = this.getLT(props.templateProps, props.asgName);
94-
const tgArn = this.getTG(props.tgProps, props.templateProps.vpc.vpcName);
106+
this.loadBalancerProperties = this.getTG(props.networkProps, props.templateProps.vpc.vpcName, props.appName);
95107

96108
new CfnAutoScalingGroup(this, props.asgName, {
97109
maxSize: props.maxSize,
@@ -102,16 +114,12 @@ export class AutoScaler extends cdk.Resource {
102114
launchTemplateId: launchTemplate.launchTemplateId,
103115
launchTemplateName: launchTemplate.launchTemplateName,
104116
},
105-
targetGroupArns: tgArn,
117+
targetGroupArns: this.loadBalancerProperties.map( (lb) => { return lb.targetGroupArn; } ),
106118
tags: props.tags,
107119
availabilityZones: this.getZones(props.subnets),
108120
vpcZoneIdentifier: props.subnets,
109121
healthCheckGracePeriod: 300,
110122
});
111-
if (tgArn.length) {
112-
this.targetGroupArn = tgArn[0];
113-
}
114-
this.targetGroupArn = '';
115123
}
116124

117125
private getVPC(props: InternalVPC) {
@@ -250,23 +258,32 @@ export class AutoScaler extends cdk.Resource {
250258
}
251259
}
252260

253-
private getTG(props: TargetGroupProps | undefined, vpcId: string) {
254-
if (props != undefined) {
255-
const tg = new CfnTargetGroup(this, props.name!, {
256-
name: props.name,
261+
private getTG(props: NetworkProps[], vpcId: string, appName: string) {
262+
let lbProps: LoadBalancerProps[] = [];
263+
props.forEach(t => {
264+
const tg = new CfnTargetGroup(this, appName + t.port.toString(), {
265+
name: appName + t.port?.toString(),
257266
healthCheckEnabled: true,
258-
healthCheckPath: props.healthPath!,
259-
...((props.protocol == 'GRPC') ? { protocol: 'HTTP' } : { protocol: props.protocol }),
260-
...((props.protocol == 'GRPC') ? { protocolVersion: 'GRPC' } : {}),
261-
healthCheckTimeoutSeconds: props.timeout!,
262-
healthCheckPort: String(props.port!),
263-
port: props.port!,
267+
healthCheckPath: t.healthCheckPath!,
268+
...((t.protocol == 'GRPC') ? { protocol: 'HTTP' } : { protocol: t.protocol }),
269+
...((t.protocol == 'GRPC') ? { protocolVersion: 'GRPC' } : {}),
270+
healthCheckTimeoutSeconds: 30,
271+
healthCheckPort: String(t.port!),
272+
port: t.port!,
264273
vpcId: vpcId,
265274
});
266-
return [tg.ref];
267-
} else {
268-
return [];
269-
}
275+
276+
lbProps.push({
277+
appName: appName,
278+
hostHeader: t.host,
279+
lbArn: t.lbArn,
280+
sslEnabled: t.sslEnabled,
281+
targetGroupArn: tg.ref,
282+
});
283+
284+
});
285+
286+
return lbProps;
270287
}
271288

272289
private getZones(subnets: string[]) {

‎src/constructs/microservice.ts

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { CfnAutoScalingGroup } from '@aws-cdk/aws-autoscaling';
22
import { EbsDeviceVolumeType } from '@aws-cdk/aws-ec2';
33
import { Effect, PolicyStatement, Role, ServicePrincipal } from '@aws-cdk/aws-iam';
44
import { Construct } from '@aws-cdk/core';
5-
import { InternalSG } from '..';
6-
import { AutoScaler, IngressRule, InternalRole } from './autoScalingGroup';
5+
import { AutoScaler, IngressRule, NetworkProps, InternalSG, TargetGroupProps, InternalRole } from './autoScalingGroup';
76
import { Deployment } from './deployment';
87
import { BalancerEntry } from './network';
98

@@ -15,24 +14,19 @@ export interface MicroServiceProps {
1514
readonly asgMaxSize?: string;
1615
readonly asgMinSize?: string;
1716
readonly instanceLabels?: CfnAutoScalingGroup.TagPropertyProperty[];
18-
readonly healthCheckPath?: string;
19-
readonly port?: number;
2017
readonly vpc: string;
2118
readonly subnets: string[];
22-
readonly protocol?: string;
2319
readonly diskSize?: number;
2420
readonly role: InternalRole;
2521
readonly tcpRules?: IngressRule[];
26-
readonly sslEnabled?: boolean;
27-
readonly host?: string;
28-
readonly lbArn?: string;
2922
readonly ami?: string;
3023
readonly sshKey: string;
3124
readonly diskType?: string;
3225
readonly createCodedeployApplication?: boolean;
3326
readonly deploymentPolicies?: string[];
3427
readonly applicationType?: string;
3528
readonly securityGroupProps?: InternalSG;
29+
readonly networkProps?: NetworkProps[];
3630
}
3731
export class MicroService extends Construct {
3832

@@ -42,23 +36,19 @@ export class MicroService extends Construct {
4236
public readonly asgMinSize?: string;
4337
public readonly env?: string;
4438
public readonly instanceLabels?: CfnAutoScalingGroup.TagPropertyProperty[];
45-
public readonly healthCheckPath?: string;
46-
public readonly port?: number;
47-
public readonly protocol?: string;
4839
public readonly diskSize?: number;
4940
public readonly vpc: string;
5041
public readonly role: InternalRole;
5142
public readonly tcpRules?: IngressRule[];
5243
public readonly subnets: string[];
53-
public readonly sslEnabled?: boolean;
54-
public readonly host?: string;
55-
public readonly lbArn?: string;
5644
public readonly sshKey: string;
5745
public readonly diskType?: string;
5846
public readonly createCodedeployApplication?: boolean;
5947
public readonly deploymentPolicies?: string[];
6048
public readonly applicationType?: string;
6149
public readonly securityGroupProps?: InternalSG;
50+
public readonly networkProps?: NetworkProps[];
51+
public readonly targetGroupProps: TargetGroupProps[];
6252

6353
constructor(scope: Construct, id: string, props: MicroServiceProps) {
6454
super(scope, id);
@@ -69,38 +59,28 @@ export class MicroService extends Construct {
6959
this.asgMinSize = props?.asgMinSize ?? '1';
7060
this.env = props?.env ?? 'development';
7161
this.instanceLabels = props?.instanceLabels;
72-
this.healthCheckPath = props?.healthCheckPath ?? '/v1/healthCheck';
73-
this.port = props.port ?? undefined;
74-
this.protocol = props.protocol ?? 'HTTP';
7562
this.diskSize = props.diskSize ?? 8;
7663
this.vpc = props.vpc;
7764
this.role = props.role;
7865
this.tcpRules = props.tcpRules ?? [];
7966
this.subnets = props.subnets;
80-
this.sslEnabled = props.sslEnabled;
81-
this.host = props.host;
82-
this.lbArn = props.lbArn;
8367
this.sshKey = props.sshKey;
8468
this.diskType = props.diskType;
8569
this.createCodedeployApplication = props.createCodedeployApplication ?? false;
8670
this.deploymentPolicies = props.deploymentPolicies ?? [];
8771
this.applicationType = props.applicationType ?? 'new';
8872
this.securityGroupProps = props.securityGroupProps;
73+
this.networkProps = props.networkProps ?? [];
74+
this.targetGroupProps = [];
75+
this.networkProps = props.networkProps ?? [];
8976

9077
const resourceNamePrefix = this.env + '-' + this.appName;
9178
const asg = new AutoScaler(this, resourceNamePrefix + '-as', {
9279
asgName: resourceNamePrefix + '-ASG',
80+
appName: resourceNamePrefix,
81+
networkProps: this.networkProps,
9382
maxSize: this.asgMaxSize,
9483
minSize: this.asgMinSize,
95-
tgProps: this.port ? {
96-
type: 'new',
97-
healthPath: this.healthCheckPath,
98-
protocol: this.protocol,
99-
port: this.port,
100-
name: ((resourceNamePrefix + '-TG').length >= 32)? this.env + '-' + this.appName + '-TG':resourceNamePrefix + '-TG',
101-
timeout: 10,
102-
thresholdCount: 2,
103-
}: undefined,
10484
templateProps: {
10585
instanceType: this.instanceType,
10686
detailedMonitoring: false,
@@ -144,17 +124,14 @@ export class MicroService extends Construct {
144124
tgName: resourceNamePrefix + '-TG',
145125
});
146126
dep.node.addDependency(depRole);
127+
dep.node.addDependency(asg);
147128
}
148129

149-
if (this.host && this.lbArn && asg.targetGroupArn != '') {
150-
const lbEntry = new BalancerEntry(this, resourceNamePrefix + '-lb', {
151-
appName: this.appName,
152-
hostHeader: this.host,
153-
targetGroupArn: asg.targetGroupArn,
154-
lbArn: this.lbArn,
155-
sslEnabled: this.sslEnabled ?? false,
130+
if (this.networkProps.length) {
131+
asg.loadBalancerProperties?.forEach(lbProp => {
132+
const lbEntry = new BalancerEntry(this, lbProp.hostHeader, lbProp);
133+
lbEntry.node.addDependency(asg);
156134
});
157-
lbEntry.node.addDependency(asg);
158135
}
159136
}
160137

‎test/hello.test.ts

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,62 @@ describe('my suite', () => {
44
});
55
});
66

7-
// import { SynthUtils } from '@aws-cdk/assert';
8-
// import { Stack } from '@aws-cdk/core';
7+
// import { App, Stack, StackProps } from '@aws-cdk/core';
8+
// import { Construct } from 'constructs';
99
// import { MicroService } from '../src/constructs/microservice';
1010

11-
// test('dlq creates an alarm', () => {
12-
// const stack = new Stack();
13-
// new MicroService(stack, 'UnknownAPI', {
14-
// appName: 'UnknownAPI',
15-
// env: 'development',
16-
// asgMaxSize: '1',
17-
// asgMinSize: '1',
18-
// diskSize: 20,
19-
// instanceLabels: [
20-
// {
21-
// key: 'NODE-VERSION',
22-
// propagateAtLaunch: true,
23-
// value: '12',
24-
// },
25-
// {
26-
// key: 'TYPE',
27-
// propagateAtLaunch: true,
28-
// value: 'application',
29-
// },
30-
// ],
31-
// instanceType: 't3.micro',
32-
// vpc: 'vpc-1234567',
33-
// port: 8000,
34-
// protocol: 'HTTP',
35-
// healthCheckPath: '/health',
36-
// subnets: ['subnet-987654321', 'subnet-12345678'],
37-
// tcpRules: [
38-
// {
39-
// sourceSG: 'sg-12345678',
40-
// description: 'ssh rule',
41-
// port: 22,
11+
12+
// class TestStack extends Stack {
13+
// constructor(scope: Construct, id: string, props?: StackProps) {
14+
// super(scope, id, props);
15+
// new MicroService(this, 'test', {
16+
// appName: 'test',
17+
// env: 'prod',
18+
// asgMaxSize: '1',
19+
// asgMinSize: '1',
20+
// diskSize: 20,
21+
// instanceLabels: [
22+
// {
23+
// key: 'SUDOERS_GROUPS_TAG',
24+
// propagateAtLaunch: true,
25+
// value: 'Developers',
26+
// },
27+
// ],
28+
// instanceType: 't3.micro',
29+
// vpc: 'vpc-1234567',
30+
// role: {
31+
// type: 'existing',
32+
// roleArn: 'arn:aws:iam::123456789233:instance-profile/API-DEV',
4233
// },
43-
// ],
44-
// host: 'abc-test-123.smallcase.com',
45-
// lbArn: 'arn:aws:elasticloadbalancing:ap-south-1:12345678910:loadbalancer/app/API-DEV-External',
46-
// sslEnabled: false,
47-
// sshKey: 'master-dev',
48-
// createCodedeployApplication: true,
49-
// role: {
50-
// type: 'new',
51-
// },
52-
// applicationType: 'new',
34+
// sshKey: 'master-dev',
35+
// subnets: ['subnet-12345678', 'subnet-123456789'],
36+
// tcpRules: [
37+
// {
38+
// sourceSG: 'sg-12345678',
39+
// description: 'ssh rule',
40+
// port: 22,
41+
// },
42+
// ],
43+
// networkProps: [
44+
// {
45+
// healthCheckPath: '/health',
46+
// host: 'abc-test-123.smallcase.com',
47+
// lbArn: 'arn:aws:elasticloadbalancing:ap-south-1:123456789233:loadbalancer/app/API-DEV-External',
48+
// sslEnabled: false,
49+
// port: 8000,
50+
// protocol: 'HTTP',
51+
// },
52+
// ],
53+
// createCodedeployApplication: true,
54+
// });
55+
// }
56+
// }
57+
58+
59+
// test('hello', () => {
60+
// const app = new App();
61+
// new TestStack(app, 'TestStack', {
62+
// env: { account: '123456789233', region: 'ap-south-1' },
5363
// });
54-
// expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot();
64+
// app.synth();
5565
// });

0 commit comments

Comments
 (0)
Please sign in to comment.