-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathRedshiftClusterInVpc.py
More file actions
196 lines (173 loc) · 4.76 KB
/
RedshiftClusterInVpc.py
File metadata and controls
196 lines (173 loc) · 4.76 KB
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
# Converted from Redshift.template located at:
# http://aws.amazon.com/cloudformation/aws-cloudformation-templates/
from troposphere import Equals, GetAtt, If, Join, Output, Parameter, Ref, Template
from troposphere.ec2 import (
VPC,
InternetGateway,
SecurityGroup,
SecurityGroupRule,
Subnet,
VPCGatewayAttachment,
)
from troposphere.redshift import (
AmazonRedshiftParameter,
Cluster,
ClusterParameterGroup,
ClusterSubnetGroup,
)
t = Template()
t.set_version("2010-09-09")
t.set_description("AWS CloudFormation Sample Template: Redshift cluster in a VPC")
dbname = t.add_parameter(
Parameter(
"DatabaseName",
Description="The name of the first database to be created when the "
"redshift cluster is created",
Type="String",
Default="defaultdb",
AllowedPattern="([a-z]|[0-9])+",
)
)
clustertype = t.add_parameter(
Parameter(
"ClusterType",
Description="The type of the cluster",
Type="String",
Default="single-node",
AllowedValues=["single-node", "multi-node"],
)
)
numberofnodes = t.add_parameter(
Parameter(
"NumberOfNodes",
Description="The number of compute nodes in the redshift cluster. "
"When cluster type is specified as: 1) single-node, the NumberOfNodes "
"parameter should be specified as 1, 2) multi-node, the NumberOfNodes "
"parameter should be greater than 1",
Type="Number",
Default="1",
)
)
nodetype = t.add_parameter(
Parameter(
"NodeType",
Description="The node type to be provisioned for the redshift cluster",
Type="String",
Default="dw2.large",
)
)
masterusername = t.add_parameter(
Parameter(
"MasterUsername",
Description="The user name associated with the master user account for "
"the redshift cluster that is being created",
Type="String",
Default="defaultuser",
AllowedPattern="([a-z])([a-z]|[0-9])*",
NoEcho=True,
)
)
masteruserpassword = t.add_parameter(
Parameter(
"MasterUserPassword",
Description="The password associated with the master user account for the "
"redshift cluster that is being created.",
Type="String",
NoEcho=True,
)
)
conditions = {
"IsMultiNodeCluster": Equals(Ref("ClusterType"), "multi-node"),
}
for k in conditions:
t.add_condition(k, conditions[k])
redshiftcluster = t.add_resource(
Cluster(
"RedshiftCluster",
ClusterType=Ref("ClusterType"),
NumberOfNodes=If(
"IsMultiNodeCluster", Ref("NumberOfNodes"), Ref("AWS::NoValue")
),
NodeType=Ref("NodeType"),
DBName=Ref("DatabaseName"),
MasterUsername=Ref("MasterUsername"),
MasterUserPassword=Ref("MasterUserPassword"),
ClusterParameterGroupName=Ref("RedshiftClusterParameterGroup"),
VpcSecurityGroupIds=[Ref("SecurityGroup")],
ClusterSubnetGroupName=Ref("RedshiftClusterSubnetGroup"),
)
)
amazonredshiftparameter1 = AmazonRedshiftParameter(
"AmazonRedshiftParameter1",
ParameterName="enable_user_activity_logging",
ParameterValue="true",
)
redshiftclusterparametergroup = t.add_resource(
ClusterParameterGroup(
"RedshiftClusterParameterGroup",
Description="Cluster parameter group",
ParameterGroupFamily="redshift-1.0",
Parameters=[amazonredshiftparameter1],
)
)
redshiftclustersubnetgroup = t.add_resource(
ClusterSubnetGroup(
"RedshiftClusterSubnetGroup",
Description="Cluster subnet group",
SubnetIds=[Ref("Subnet")],
)
)
vpc = t.add_resource(
VPC(
"VPC",
CidrBlock="10.0.0.0/16",
)
)
subnet = t.add_resource(
Subnet(
"Subnet",
CidrBlock="10.0.0.0/24",
VpcId=Ref("VPC"),
)
)
internetgateway = t.add_resource(
InternetGateway(
"InternetGateway",
)
)
gatewayattachment = t.add_resource(
VPCGatewayAttachment(
"GatewayAttachment",
VpcId=Ref("VPC"),
InternetGatewayId=Ref("InternetGateway"),
)
)
securitygroup = t.add_resource(
SecurityGroup(
"SecurityGroup",
GroupDescription="Security Group",
SecurityGroupIngress=[
SecurityGroupRule(
"SecurityGroupIngress1",
CidrIp="10.0.0.0/16",
FromPort="80",
ToPort="80",
IpProtocol="tcp",
)
],
VpcId=Ref("VPC"),
)
)
t.add_output(
Output(
"ClusterEndpoint",
Value=Join(
":",
[
GetAtt(redshiftcluster, "Endpoint.Address"),
GetAtt(redshiftcluster, "Endpoint.Port"),
],
),
)
)
print(t.to_json())