/
index.ts
72 lines (54 loc) · 1.87 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import autoscaling = require('@aws-cdk/aws-autoscaling');
import ec2 = require('@aws-cdk/aws-ec2');
import elb = require('@aws-cdk/aws-elasticloadbalancing');
import cdk = require('@aws-cdk/cdk');
class AppWithVpc extends cdk.Stack {
constructor(parent: cdk.App, name: string, props?: cdk.StackProps) {
super(parent, name, props);
const vpc = new ec2.VpcNetwork(this, 'MyVpc');
const asg = new autoscaling.AutoScalingGroup(this, 'MyASG', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.XLarge),
machineImage: new ec2.AmazonLinuxImage()
});
const clb = new elb.LoadBalancer(this, 'LB', {
vpc,
internetFacing: true
});
clb.addListener({ externalPort: 80 });
clb.addTarget(asg);
}
}
interface MyAppProps extends cdk.StackProps {
infra: CommonInfrastructure
}
class MyApp extends cdk.Stack {
constructor(parent: cdk.App, name: string, props: MyAppProps) {
super(parent, name, props);
const vpc = ec2.VpcNetwork.import(this, 'VPC', props.infra.vpc);
const fleet = new autoscaling.AutoScalingGroup(this, 'MyASG', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.XLarge),
machineImage: new ec2.AmazonLinuxImage()
});
const clb = new elb.LoadBalancer(this, 'LB', {
vpc,
internetFacing: true
});
clb.addListener({ externalPort: 80 });
clb.addTarget(fleet);
}
}
class CommonInfrastructure extends cdk.Stack {
public vpc: ec2.VpcNetworkImportProps;
constructor(parent: cdk.App, name: string, props?: cdk.StackProps) {
super(parent, name, props);
const vpc = new ec2.VpcNetwork(this, 'VPC');
this.vpc = vpc.export();
}
}
const app = new cdk.App();
const infra = new CommonInfrastructure(app, 'infra');
new AppWithVpc(app, 'app-with-vpc');
new MyApp(app, 'my-app', { infra });
app.run();