This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
vpc.go
100 lines (93 loc) · 3.66 KB
/
vpc.go
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
package aws
import (
"fmt"
"strconv"
cfn "github.com/crewjam/go-cloudformation"
)
// MakeVPC creates a VPC with a subnet for each of up to four availability zones.
func MakeVPC(parameters *Parameters, t *cfn.Template) error {
t.AddResource("Vpc", cfn.EC2VPC{
CidrBlock: cfn.String("10.0.0.0/16"),
InstanceTenancy: cfn.String("default"),
EnableDnsSupport: cfn.Bool(true),
EnableDnsHostnames: cfn.Bool(true),
Tags: []cfn.ResourceTag{
cfn.ResourceTag{Key: cfn.String("application"), Value: cfn.Ref("Application").String()},
cfn.ResourceTag{Key: cfn.String("Name"), Value: cfn.String(parameters.DnsName)},
},
})
cidrBlocks := []string{
"10.0.0.0/18",
"10.0.64.0/18",
"10.0.128.0/18",
"10.0.192.0/18",
}
for index := 0; index < 3; index++ {
t.AddResource(fmt.Sprintf("VpcSubnet%d", index), cfn.EC2Subnet{
CidrBlock: cfn.String(cidrBlocks[index]),
AvailabilityZone: cfn.FindInMap("AvailablityZones", cfn.Ref("AWS::Region"), cfn.String(strconv.Itoa(index))),
VpcId: cfn.Ref("Vpc"),
Tags: []cfn.ResourceTag{
cfn.ResourceTag{Key: cfn.String("application"), Value: cfn.Ref("Application").String()},
cfn.ResourceTag{Key: cfn.String("Name"), Value: cfn.String(fmt.Sprintf("%s-%d", parameters.DnsName, index))},
},
})
t.AddResource(fmt.Sprintf("VpcSubnetRouteTableAssociation%d", index), cfn.EC2SubnetRouteTableAssociation{
SubnetId: cfn.Ref(fmt.Sprintf("VpcSubnet%d", index)).String(),
RouteTableId: cfn.Ref("VpcRouteTable").String(),
})
t.AddResource(fmt.Sprintf("VpcSubnetAcl%d", index), cfn.EC2SubnetNetworkAclAssociation{
NetworkAclId: cfn.Ref("VpcNetworkAcl").String(),
SubnetId: cfn.Ref(fmt.Sprintf("VpcSubnet%d", index)).String(),
})
parameters.VpcSubnets = append(parameters.VpcSubnets,
cfn.Ref(fmt.Sprintf("VpcSubnet%d", index)).String())
}
t.AddResource("VpcInternetGateway", cfn.EC2InternetGateway{
Tags: []cfn.ResourceTag{
cfn.ResourceTag{Key: cfn.String("application"), Value: cfn.Ref("Application").String()},
cfn.ResourceTag{Key: cfn.String("Name"), Value: cfn.String(fmt.Sprintf("InternetGateway-%s", parameters.DnsName))},
},
})
t.AddResource("VpcInternetGatewayAttachment", cfn.EC2VPCGatewayAttachment{
VpcId: cfn.Ref("Vpc").String(),
InternetGatewayId: cfn.Ref("VpcInternetGateway").String(),
})
t.Resources["VpcDefaultRoute"] = &cfn.Resource{
DependsOn: []string{"VpcInternetGatewayAttachment"},
Properties: cfn.EC2Route{
DestinationCidrBlock: cfn.String("0.0.0.0/0"),
RouteTableId: cfn.Ref("VpcRouteTable").String(),
GatewayId: cfn.Ref("VpcInternetGateway").String(),
},
}
t.AddResource("VpcRouteTable", cfn.EC2RouteTable{
VpcId: cfn.Ref("Vpc").String(),
})
t.AddResource("VpcNetworkAcl", cfn.EC2NetworkAcl{
VpcId: cfn.Ref("Vpc").String(),
})
t.AddResource("VpcNetworkAclAllowAllEgress", cfn.EC2NetworkAclEntry{
CidrBlock: cfn.String("0.0.0.0/0"),
Egress: cfn.Bool(true),
Protocol: cfn.Integer(-1),
RuleAction: cfn.String("allow"),
RuleNumber: cfn.Integer(100),
NetworkAclId: cfn.Ref("VpcNetworkAcl").String(),
})
t.AddResource("VpcNetworkAclAllowAllIngress", cfn.EC2NetworkAclEntry{
CidrBlock: cfn.String("0.0.0.0/0"),
Protocol: cfn.Integer(-1),
RuleAction: cfn.String("allow"),
RuleNumber: cfn.Integer(100),
NetworkAclId: cfn.Ref("VpcNetworkAcl").String(),
})
t.AddResource("VpcDhcpOptions", cfn.EC2DHCPOptions{
DomainNameServers: cfn.StringList(cfn.String("AmazonProvidedDNS")),
})
t.AddResource("VpcDhpcOptionsAssociation", cfn.EC2VPCDHCPOptionsAssociation{
VpcId: cfn.Ref("Vpc").String(),
DhcpOptionsId: cfn.Ref("VpcDhcpOptions").String(),
})
return nil
}