liuxiaolong
2022-06-28 37714b1093c04061e636e5b1d27179652e671c0a
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
{
    "AWSTemplateFormatVersion": "2010-09-09",
 
    "Mappings" : {
        "AWSNATAMI" : {
          "us-east-1"      : { "AMI" : "ami-c6699baf" },
          "us-west-2"      : { "AMI" : "ami-52ff7262" },
          "us-west-1"      : { "AMI" : "ami-3bcc9e7e" },
          "eu-west-1"      : { "AMI" : "ami-0b5b6c7f" },
          "ap-southeast-1" : { "AMI" : "ami-02eb9350" },
          "ap-southeast-2" : { "AMI" : "ami-ab990e91" },
          "ap-northeast-1" : { "AMI" : "ami-14d86d15" },
          "sa-east-1"      : { "AMI" : "ami-0439e619" }
        },
 
        "AWSINSTAMI" : {
          "us-east-1"      : { "AMI" : "ami-a73264ce" },
          "us-west-2"      : { "AMI" : "ami-6aad335a" },
          "us-west-1"      : { "AMI" : "ami-acf9cde9" },
          "eu-west-1"      : { "AMI" : "ami-8e987ef9" },
          "ap-southeast-1" : { "AMI" : "ami-b84e04ea" },
          "ap-southeast-2" : { "AMI" : "ami-3d128f07" },
          "ap-northeast-1" : { "AMI" : "ami-3f32ac3e" },
          "sa-east-1"      : { "AMI" : "ami-35258228" }
        }
    },
 
    "Parameters": {
        "WebNodes": {
            "Type": "String",
            "Default": "5",
            "Description": "Number of web servers to launch."
        }
    },
 
    "Outputs": {
        "LoadBalancerIP": {
            "Value": { "Ref": "LoadBalancerIP" }
        }
    },
 
    "Resources": {
        "Gateway": {
            "Type": "AWS::EC2::InternetGateway"
        },
 
        "VPC": {
            "Type": "AWS::EC2::VPC",
            "Properties": {
                "CidrBlock": "10.0.0.0/16",
                "EnableDnsHostnames": true
            }
        },
 
        "VPCGateway": {
            "Type" : "AWS::EC2::VPCGatewayAttachment",
            "Properties" : {
                "InternetGatewayId" : { "Ref": "Gateway" },
                "VpcId": { "Ref": "VPC" }
            }
        },
 
        "PublicSubnet": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "CidrBlock": "10.0.0.0/24",
                "VpcId": { "Ref": "VPC" }
            }
        },
 
        "PrivateSubnet": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "CidrBlock": "10.0.1.0/24",
                "VpcId": { "Ref": "VPC" }
            }
        },
 
        "PrivateSubnetRoute": {
            "Type" : "AWS::EC2::SubnetRouteTableAssociation",
            "Properties" : {
                "RouteTableId" : { "Ref": "PrivateRouteTable" },
                "SubnetId" : { "Ref": "PrivateSubnet" }
            }
        },
 
        "PrivateRouteTable": {
            "Type": "AWS::EC2::RouteTable",
            "Properties": {
                "VpcId": { "Ref": "VPC" }
            }
        },
 
        "PrivateRouteGlobal": {
            "Type": "AWS::EC2::Route",
            "Properties": {
                "RouteTableId": { "Ref": "PrivateRouteTable" },
                "DestinationCidrBlock": "0.0.0.0/0",
                "InstanceId" : { "Ref" : "NATDevice" }
            },
            "DependsOn": "PublicRouteGlobal"
        },
 
        "PublicSubnetRoute": {
            "Type" : "AWS::EC2::SubnetRouteTableAssociation",
            "Properties" : {
                "RouteTableId" : { "Ref": "PublicRouteTable" },
                "SubnetId" : { "Ref": "PublicSubnet" }
            }
        },
 
        "PublicRouteTable": {
            "Type": "AWS::EC2::RouteTable",
            "Properties": {
                "VpcId": { "Ref": "VPC" }
            }
        },
 
        "PublicRouteGlobal": {
            "Type": "AWS::EC2::Route",
            "Properties": {
                "RouteTableId": { "Ref": "PublicRouteTable" },
                "DestinationCidrBlock": "0.0.0.0/0",
                "GatewayId": { "Ref": "Gateway" }
            }
        },
 
        "NATIPAddress": {
            "Type": "AWS::EC2::EIP",
            "Properties": {
                "Domain": "vpc",
                "InstanceId": { "Ref": "NATDevice" }
            },
            "DependsOn": "VPCGateway"
        },
 
        "NATDevice" : {
            "Type" : "AWS::EC2::Instance",
            "Properties" : {
                "SubnetId" : { "Ref" : "PublicSubnet" },
                "SourceDestCheck" : "false",
                "ImageId" : { "Fn::FindInMap" : [ "AWSNATAMI", { "Ref" : "AWS::Region" }, "AMI" ]},
                "SecurityGroupIds" : [
                    { "Ref" : "InstanceSecurityGroup" }
                ],
                "Tags": [
                    { "Key": "Name", "Value": "Serf Demo NAT Device" }
                ]
            }
        },
 
        "LoadBalancer": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "ImageId": { "Fn::FindInMap" : [ "AWSINSTAMI", { "Ref" : "AWS::Region" }, "AMI" ]},
                "PrivateIpAddress": "10.0.0.5",
                "SecurityGroupIds": [
                    {"Ref": "InstanceSecurityGroup"}
                ],
                "SubnetId": { "Ref": "PublicSubnet" },
                "Tags": [
                    { "Key": "Name", "Value": "Serf Demo LB" }
                ],
                "UserData": "IyEvYmluL2Jhc2gKTk9ERV9TRVRVUF9VUkw9Imh0dHBzOi8vcmF3LmdpdGh1Yi5jb20vaGFzaGljb3JwL3NlcmYvbWFzdGVyL2RlbW8vd2ViLWxvYWQtYmFsYW5jZXIvc2V0dXBfbG9hZF9iYWxhbmNlci5zaCIKClNFUkZfU0VUVVBfVVJMPSJodHRwczovL3Jhdy5naXRodWIuY29tL2hhc2hpY29ycC9zZXJmL21hc3Rlci9kZW1vL3dlYi1sb2FkLWJhbGFuY2VyL3NldHVwX3NlcmYuc2giCgojIFNldHVwIHRoZSBub2RlIGl0c2VsZgp3Z2V0IC1PIC0gJE5PREVfU0VUVVBfVVJMIHwgYmFzaAoKIyBTZXR1cCB0aGUgc2VyZiBhZ2VudApleHBvcnQgU0VSRl9ST0xFPSJsYiIKd2dldCAtTyAtICRTRVJGX1NFVFVQX1VSTCB8IGJhc2gK"
            },
            "DependsOn": "PublicRouteGlobal"
        },
 
        "LoadBalancerIP": {
            "Type" : "AWS::EC2::EIP",
            "Properties" : {
                "InstanceId" : { "Ref": "LoadBalancer" },
                "Domain" : "vpc"
            },
            "DependsOn": "VPCGateway"
        },
 
        "WebGroup": {
            "Type": "AWS::AutoScaling::AutoScalingGroup",
            "Properties": {
                "AvailabilityZones": [
                    { "Fn::GetAtt" : [ "PrivateSubnet", "AvailabilityZone" ] }
                ],
                "LaunchConfigurationName": { "Ref": "WebLaunchConfig" },
                "DesiredCapacity": { "Ref": "WebNodes" },
                "MinSize": { "Ref": "WebNodes" },
                "MaxSize": { "Ref": "WebNodes" },
                "VPCZoneIdentifier": [
                    { "Ref": "PrivateSubnet" }
                ]
            },
            "DependsOn": ["NATDevice", "NATIPAddress", "PrivateRouteGlobal"]
        },
 
        "WebLaunchConfig": {
            "Type": "AWS::AutoScaling::LaunchConfiguration",
            "Properties": {
                "ImageId": { "Fn::FindInMap" : [ "AWSINSTAMI", { "Ref" : "AWS::Region" }, "AMI" ]},
                "InstanceType": "m1.small",
                "SecurityGroups": [
                    {"Ref": "InstanceSecurityGroup"}
                ],
                "UserData": "IyEvYmluL2Jhc2gKTk9ERV9TRVRVUF9VUkw9Imh0dHBzOi8vcmF3LmdpdGh1Yi5jb20vaGFzaGljb3JwL3NlcmYvbWFzdGVyL2RlbW8vd2ViLWxvYWQtYmFsYW5jZXIvc2V0dXBfd2ViX3NlcnZlci5zaCIKClNFUkZfU0VUVVBfVVJMPSJodHRwczovL3Jhdy5naXRodWIuY29tL2hhc2hpY29ycC9zZXJmL21hc3Rlci9kZW1vL3dlYi1sb2FkLWJhbGFuY2VyL3NldHVwX3NlcmYuc2giCgojIFNldHVwIHRoZSBub2RlIGl0c2VsZgp3Z2V0IC1PIC0gJE5PREVfU0VUVVBfVVJMIHwgYmFzaAoKIyBTZXR1cCB0aGUgc2VyZiBhZ2VudApleHBvcnQgU0VSRl9ST0xFPSJ3ZWIiCndnZXQgLU8gLSAkU0VSRl9TRVRVUF9VUkwgfCBiYXNoCg=="
            }
        },
 
        "InstanceSecurityGroup": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": "Serf demo security group",
                "VpcId": { "Ref": "VPC" },
                "SecurityGroupIngress": [{
                    "IpProtocol": "icmp",
                    "FromPort": "-1",
                    "ToPort": "-1",
                    "CidrIp": "0.0.0.0/0"
                }, {
                    "IpProtocol": "tcp",
                    "FromPort": "22",
                    "ToPort": "22",
                    "CidrIp": "0.0.0.0/0"
                }, {
                    "IpProtocol": "tcp",
                    "FromPort": "80",
                    "ToPort": "80",
                    "CidrIp": "0.0.0.0/0"
                }, {
                    "IpProtocol": "tcp",
                    "FromPort": "9999",
                    "ToPort": "9999",
                    "CidrIp": "0.0.0.0/0"
                }]
            }
        },
 
        "InstanceSecurityGroupSelfRule": {
            "Type": "AWS::EC2::SecurityGroupIngress",
            "Properties": {
                "GroupId": { "Ref": "InstanceSecurityGroup" },
                "IpProtocol": "-1",
                "FromPort": "0",
                "ToPort": "65535",
                "SourceSecurityGroupId": { "Ref": "InstanceSecurityGroup" }
            }
        }
    }
}