forked from caktus/aws-web-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
351 lines (323 loc) · 9.97 KB
/
cache.py
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from troposphere import (
And,
Condition,
Equals,
GetAtt,
If,
Join,
Not,
Or,
Output,
Ref,
Tags,
constants,
ec2,
elasticache
)
from .common import (
cmk_arn,
dont_create_value,
use_aes256_encryption,
use_aes256_encryption_cond,
use_cmk_arn
)
from .security_groups import container_security_group
from .template import template
from .utils import ParameterWithDefaults as Parameter
from .vpc import (
primary_az,
private_subnet_a,
private_subnet_b,
secondary_az,
vpc
)
NODE_TYPES = [
dont_create_value,
'cache.t2.micro',
'cache.t2.small',
'cache.t2.medium',
'cache.m3.medium',
'cache.m3.large',
'cache.m3.xlarge',
'cache.m3.2xlarge',
'cache.m4.large',
'cache.m4.xlarge',
'cache.m4.2xlarge',
'cache.m4.4xlarge',
'cache.m4.10xlarge',
'cache.r4.large',
'cache.r4.xlarge',
'cache.r4.2xlarge',
'cache.r4.4xlarge',
'cache.r4.8xlarge',
'cache.r4.16xlarge',
'cache.r3.large',
'cache.r3.xlarge',
'cache.r3.2xlarge',
'cache.r3.4xlarge',
'cache.r3.8xlarge',
]
cache_node_type = template.add_parameter(
Parameter(
"CacheNodeType",
Default=dont_create_value,
Description="Cache instance type",
Type="String",
AllowedValues=NODE_TYPES,
ConstraintDescription="must select a valid cache node type.",
),
group="Memcached",
label="Instance Type",
)
using_memcached_condition = "UsingMemcached"
template.add_condition(using_memcached_condition, Not(Equals(Ref(cache_node_type), dont_create_value)))
redis_node_type = template.add_parameter(
Parameter(
"RedisNodeType",
Default=dont_create_value,
Description="Redis instance type",
Type="String",
AllowedValues=NODE_TYPES,
ConstraintDescription="must select a valid cache node type.",
),
group="Redis",
label="Instance Type",
)
using_redis_condition = "UsingRedis"
template.add_condition(using_redis_condition, Not(Equals(Ref(redis_node_type), dont_create_value)))
# Parameter constraints (MinLength, AllowedPattern, etc.) don't allow a blank value,
# so we use a special "blank" do-not-create value
auth_token_dont_create_value = 'DO_NOT_CREATE_AUTH_TOKEN'
redis_auth_token = template.add_parameter(
Parameter(
"RedisAuthToken",
NoEcho=True,
Default=auth_token_dont_create_value,
Description="The password used to access a Redis ReplicationGroup (required for HIPAA).",
Type="String",
MinLength="16",
MaxLength="128",
AllowedPattern="[ !#-.0-?A-~]*", # see http://www.catonmat.net/blog/my-favorite-regex/
ConstraintDescription="must consist of 16-128 printable ASCII "
"characters except \"/\", \"\"\", or \"@\"."
),
group="Redis",
label="AuthToken",
)
using_auth_token_condition = "AuthTokenCondition"
template.add_condition(using_auth_token_condition,
Not(Equals(Ref(redis_auth_token), auth_token_dont_create_value)))
redis_version = template.add_parameter(
Parameter(
"RedisVersion",
Default="",
Description="Redis version to use. See available versions: aws elasticache describe-cache-engine-versions",
Type="String",
),
group="Redis",
label="Redis Version",
)
redis_num_cache_clusters = Ref(template.add_parameter(
Parameter(
"RedisNumCacheClusters",
Description="The number of clusters this replication group initially has.",
Type="Number",
Default="1",
),
group="Redis",
label="Number of node groups",
))
redis_snapshot_retention_limit = Ref(template.add_parameter(
Parameter(
"RedisSnapshotRetentionLimit",
Default="0",
Description="The number of days for which ElastiCache retains automatic snapshots before deleting them."
"For example, if you set SnapshotRetentionLimit to 5, a snapshot that was taken today is "
"retained for 5 days before being deleted. 0 = automatic backups are disabled for this cluster.",
Type="Number",
),
group="Redis",
label="Snapshow retention limit",
))
redis_automatic_failover = template.add_parameter(
Parameter(
"RedisAutomaticFailover",
Description="Specifies whether a read-only replica is automatically promoted to read/write primary if "
"the existing primary fails.",
Type="String",
AllowedValues=["true", "false"],
Default="false",
),
group="Redis",
label="Enable automatic failover",
)
redis_uses_automatic_failover = "RedisAutomaticFailoverCondition"
template.add_condition(redis_uses_automatic_failover, Equals(Ref(redis_automatic_failover), "true"))
secure_redis_condition = "SecureRedisCondition"
template.add_condition(secure_redis_condition,
And(Condition(using_redis_condition), Condition(use_aes256_encryption_cond)))
using_either_cache_condition = "EitherCacheCondition"
template.add_condition(using_either_cache_condition,
Or(Condition(using_memcached_condition), Condition(using_redis_condition)))
# Subnet and security group shared by both clusters
cache_subnet_group = elasticache.SubnetGroup(
"CacheSubnetGroup",
template=template,
Description="Subnets available for the cache instance",
Condition=using_either_cache_condition,
SubnetIds=[Ref(private_subnet_a), Ref(private_subnet_b)],
)
cache_security_group = ec2.SecurityGroup(
'CacheSecurityGroup',
template=template,
GroupDescription="Cache security group.",
Condition=using_either_cache_condition,
VpcId=Ref(vpc),
SecurityGroupIngress=[
If(
using_memcached_condition,
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort=constants.MEMCACHED_PORT,
ToPort=constants.MEMCACHED_PORT,
SourceSecurityGroupId=Ref(container_security_group),
),
Ref("AWS::NoValue"),
),
If(
using_redis_condition,
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort=constants.REDIS_PORT,
ToPort=constants.REDIS_PORT,
SourceSecurityGroupId=Ref(container_security_group),
),
Ref("AWS::NoValue"),
),
],
Tags=Tags(
Name=Join("-", [Ref("AWS::StackName"), "cache"]),
),
)
cache_cluster = elasticache.CacheCluster(
"CacheCluster",
template=template,
Engine="memcached",
CacheNodeType=Ref(cache_node_type),
Condition=using_memcached_condition,
NumCacheNodes=1,
Port=constants.MEMCACHED_PORT,
VpcSecurityGroupIds=[Ref(cache_security_group)],
CacheSubnetGroupName=Ref(cache_subnet_group),
Tags=Tags(
Name=Join("-", [Ref("AWS::StackName"), "cache"]),
),
)
redis_replication_group = elasticache.ReplicationGroup(
"RedisReplicationGroup",
template=template,
AtRestEncryptionEnabled=use_aes256_encryption,
AutomaticFailoverEnabled=Ref(redis_automatic_failover),
AuthToken=If(using_auth_token_condition, Ref(redis_auth_token), Ref("AWS::NoValue")),
Engine="redis",
EngineVersion=Ref(redis_version),
CacheNodeType=Ref(redis_node_type),
CacheSubnetGroupName=Ref(cache_subnet_group),
Condition=using_redis_condition,
NumCacheClusters=redis_num_cache_clusters,
Port=constants.REDIS_PORT,
PreferredCacheClusterAZs=If(redis_uses_automatic_failover,
[Ref(primary_az), Ref(secondary_az)],
Ref("AWS::NoValue")),
ReplicationGroupDescription="Redis ReplicationGroup",
SecurityGroupIds=[Ref(cache_security_group)],
SnapshotRetentionLimit=redis_snapshot_retention_limit,
TransitEncryptionEnabled=use_aes256_encryption,
KmsKeyId=If(use_cmk_arn, Ref(cmk_arn), Ref("AWS::NoValue")),
Tags=Tags(
Name=Join("-", [Ref("AWS::StackName"), "redis"]),
),
)
cache_address = If(
using_memcached_condition,
GetAtt(cache_cluster, 'ConfigurationEndpoint.Address'),
"",
)
cache_port = If(
using_memcached_condition,
GetAtt(cache_cluster, 'ConfigurationEndpoint.Port'),
"",
)
cache_url = If(
using_memcached_condition,
Join("", [
"memcached://",
cache_address,
":",
cache_port,
]),
"",
)
template.add_output([
Output(
"CacheAddress",
Description="The DNS address for the cache node/cluster.",
Value=cache_address,
Condition=using_memcached_condition,
),
Output(
"CachePort",
Description="The port number for the cache node/cluster.",
Value=GetAtt(cache_cluster, 'ConfigurationEndpoint.Port'),
Condition=using_memcached_condition,
),
Output(
"CacheURL",
Description="URL to connect to the cache node/cluster.",
Value=cache_url,
Condition=using_memcached_condition,
),
])
redis_address = If(
using_redis_condition,
GetAtt(redis_replication_group, 'PrimaryEndPoint.Address'),
"",
)
redis_port = If(
using_redis_condition,
GetAtt(redis_replication_group, 'PrimaryEndPoint.Port'),
"",
)
redis_url = If(
using_redis_condition,
Join("", [
"redis",
If(secure_redis_condition, "s", ""),
"://",
If(using_auth_token_condition, ":_PASSWORD_@", ""),
redis_address,
":",
redis_port,
]),
"",
)
template.add_output([
Output(
"RedisAddress",
Description="The DNS address for the Redis node/cluster.",
Value=redis_address,
Condition=using_redis_condition,
),
Output(
"RedisPort",
Description="The port number for the Redis node/cluster.",
Value=redis_port,
Condition=using_redis_condition,
),
Output(
"RedisURL",
Description="URL to connect to the Redis node/cluster.",
Value=redis_url,
Condition=using_redis_condition,
),
])