0
# Constants
1
2
Pre-defined constants for AWS regions, instance types, ports, pseudo parameters, and CloudFormation limits to improve template readability and reduce errors.
3
4
## Capabilities
5
6
### Pseudo Parameters
7
8
CloudFormation pseudo parameters that provide runtime values about the stack and AWS environment.
9
10
```python { .api }
11
# Pseudo Parameter String Constants
12
AWS_ACCOUNT_ID: str = "AWS::AccountId"
13
AWS_NOTIFICATION_ARNS: str = "AWS::NotificationARNs"
14
AWS_NO_VALUE: str = "AWS::NoValue"
15
AWS_PARTITION: str = "AWS::Partition"
16
AWS_REGION: str = "AWS::Region"
17
AWS_STACK_ID: str = "AWS::StackId"
18
AWS_STACK_NAME: str = "AWS::StackName"
19
AWS_URL_SUFFIX: str = "AWS::URLSuffix"
20
21
# Pre-defined Ref Objects for Pseudo Parameters
22
AccountId: Ref = Ref(AWS_ACCOUNT_ID)
23
NotificationARNs: Ref = Ref(AWS_NOTIFICATION_ARNS)
24
NoValue: Ref = Ref(AWS_NO_VALUE)
25
Partition: Ref = Ref(AWS_PARTITION)
26
Region: Ref = Ref(AWS_REGION)
27
StackId: Ref = Ref(AWS_STACK_ID)
28
StackName: Ref = Ref(AWS_STACK_NAME)
29
URLSuffix: Ref = Ref(AWS_URL_SUFFIX)
30
```
31
32
### CloudFormation Policy Constants
33
34
Constants for DeletionPolicy and UpdateReplacePolicy values.
35
36
```python { .api }
37
Delete: str = "Delete"
38
Retain: str = "Retain"
39
RetainExceptOnCreate: str = "RetainExceptOnCreate"
40
Snapshot: str = "Snapshot"
41
```
42
43
### Template Limits
44
45
CloudFormation template limits for resources, parameters, outputs, and mappings.
46
47
```python { .api }
48
MAX_MAPPINGS: int = 200
49
MAX_OUTPUTS: int = 200
50
MAX_PARAMETERS: int = 200
51
MAX_RESOURCES: int = 500
52
PARAMETER_TITLE_MAX: int = 255
53
```
54
55
### AWS Regions
56
57
All AWS regions with standardized constant names.
58
59
```python { .api }
60
# US Regions
61
US_EAST_1 = "us-east-1"
62
US_EAST_2 = "us-east-2"
63
US_WEST_1 = "us-west-1"
64
US_WEST_2 = "us-west-2"
65
66
# Europe Regions
67
EU_CENTRAL_1 = "eu-central-1"
68
EU_NORTH_1 = "eu-north-1"
69
EU_SOUTH_1 = "eu-south-1"
70
EU_WEST_1 = "eu-west-1"
71
EU_WEST_2 = "eu-west-2"
72
EU_WEST_3 = "eu-west-3"
73
74
# Asia Pacific Regions
75
AP_EAST_1 = "ap-east-1"
76
AP_NORTHEAST_1 = "ap-northeast-1"
77
AP_NORTHEAST_2 = "ap-northeast-2"
78
AP_NORTHEAST_3 = "ap-northeast-3"
79
AP_SOUTH_1 = "ap-south-1"
80
AP_SOUTHEAST_1 = "ap-southeast-1"
81
AP_SOUTHEAST_2 = "ap-southeast-2"
82
AP_SOUTHEAST_3 = "ap-southeast-3"
83
84
# Other Regions
85
AF_SOUTH_1 = "af-south-1"
86
CA_CENTRAL_1 = "ca-central-1"
87
CN_NORTH_1 = "cn-north-1"
88
CN_NORTHWEST_1 = "cn-northwest-1"
89
ME_CENTRAL_1 = "me-central-1"
90
ME_SOUTH_1 = "me-south-1"
91
SA_EAST_1 = "sa-east-1"
92
US_GOV_EAST_1 = "us-gov-east-1"
93
US_GOV_WEST_1 = "us-gov-west-1"
94
```
95
96
### Availability Zones
97
98
Availability zone constants for all AWS regions.
99
100
```python { .api }
101
# US East 1 Availability Zones
102
US_EAST_1A = "us-east-1a"
103
US_EAST_1B = "us-east-1b"
104
US_EAST_1C = "us-east-1c"
105
US_EAST_1D = "us-east-1d"
106
US_EAST_1E = "us-east-1e"
107
US_EAST_1F = "us-east-1f"
108
109
# US West 2 Availability Zones
110
US_WEST_2A = "us-west-2a"
111
US_WEST_2B = "us-west-2b"
112
US_WEST_2C = "us-west-2c"
113
US_WEST_2D = "us-west-2d"
114
115
# Additional zones available for all regions...
116
```
117
118
### EC2 Instance Types
119
120
Constants for all EC2 instance types across different families.
121
122
```python { .api }
123
# General Purpose - T2 Family
124
T2_NANO = "t2.nano"
125
T2_MICRO = "t2.micro"
126
T2_SMALL = "t2.small"
127
T2_MEDIUM = "t2.medium"
128
T2_LARGE = "t2.large"
129
T2_XLARGE = "t2.xlarge"
130
T2_2XLARGE = "t2.2xlarge"
131
132
# General Purpose - T3 Family
133
T3_NANO = "t3.nano"
134
T3_MICRO = "t3.micro"
135
T3_SMALL = "t3.small"
136
T3_MEDIUM = "t3.medium"
137
T3_LARGE = "t3.large"
138
T3_XLARGE = "t3.xlarge"
139
T3_2XLARGE = "t3.2xlarge"
140
141
# General Purpose - M5 Family
142
M5_LARGE = "m5.large"
143
M5_XLARGE = "m5.xlarge"
144
M5_2XLARGE = "m5.2xlarge"
145
M5_4XLARGE = "m5.4xlarge"
146
M5_8XLARGE = "m5.8xlarge"
147
M5_12XLARGE = "m5.12xlarge"
148
M5_16XLARGE = "m5.16xlarge"
149
M5_24XLARGE = "m5.24xlarge"
150
151
# Compute Optimized - C5 Family
152
C5_LARGE = "c5.large"
153
C5_XLARGE = "c5.xlarge"
154
C5_2XLARGE = "c5.2xlarge"
155
C5_4XLARGE = "c5.4xlarge"
156
C5_9XLARGE = "c5.9xlarge"
157
C5_12XLARGE = "c5.12xlarge"
158
C5_18XLARGE = "c5.18xlarge"
159
C5_24XLARGE = "c5.24xlarge"
160
161
# Memory Optimized - R5 Family
162
R5_LARGE = "r5.large"
163
R5_XLARGE = "r5.xlarge"
164
R5_2XLARGE = "r5.2xlarge"
165
R5_4XLARGE = "r5.4xlarge"
166
R5_8XLARGE = "r5.8xlarge"
167
R5_12XLARGE = "r5.12xlarge"
168
R5_16XLARGE = "r5.16xlarge"
169
R5_24XLARGE = "r5.24xlarge"
170
171
# Additional instance families available...
172
```
173
174
### RDS Instance Classes
175
176
Constants for RDS database instance classes.
177
178
```python { .api }
179
# General Purpose - db.t3 Family
180
DB_T3_MICRO = "db.t3.micro"
181
DB_T3_SMALL = "db.t3.small"
182
DB_T3_MEDIUM = "db.t3.medium"
183
DB_T3_LARGE = "db.t3.large"
184
DB_T3_XLARGE = "db.t3.xlarge"
185
DB_T3_2XLARGE = "db.t3.2xlarge"
186
187
# General Purpose - db.m5 Family
188
DB_M5_LARGE = "db.m5.large"
189
DB_M5_XLARGE = "db.m5.xlarge"
190
DB_M5_2XLARGE = "db.m5.2xlarge"
191
DB_M5_4XLARGE = "db.m5.4xlarge"
192
DB_M5_8XLARGE = "db.m5.8xlarge"
193
DB_M5_12XLARGE = "db.m5.12xlarge"
194
DB_M5_16XLARGE = "db.m5.16xlarge"
195
DB_M5_24XLARGE = "db.m5.24xlarge"
196
197
# Memory Optimized - db.r5 Family
198
DB_R5_LARGE = "db.r5.large"
199
DB_R5_XLARGE = "db.r5.xlarge"
200
DB_R5_2XLARGE = "db.r5.2xlarge"
201
DB_R5_4XLARGE = "db.r5.4xlarge"
202
DB_R5_8XLARGE = "db.r5.8xlarge"
203
DB_R5_12XLARGE = "db.r5.12xlarge"
204
DB_R5_16XLARGE = "db.r5.16xlarge"
205
DB_R5_24XLARGE = "db.r5.24xlarge"
206
```
207
208
### Network Constants
209
210
Common networking constants for CIDR blocks, ports, and protocols.
211
212
```python { .api }
213
# CIDR Blocks
214
QUAD_ZERO = "0.0.0.0/0"
215
VPC_CIDR_16 = "10.0.0.0/16"
216
VPC_CIDR_20 = "10.0.0.0/20"
217
PRIVATE_SUBNET_CIDR_24 = "10.0.1.0/24"
218
PUBLIC_SUBNET_CIDR_24 = "10.0.2.0/24"
219
220
# Common Ports
221
SSH_PORT = 22
222
TELNET_PORT = 23
223
SMTP_PORT = 25
224
DNS_PORT = 53
225
HTTP_PORT = 80
226
POP3_PORT = 110
227
NTP_PORT = 123
228
IMAP_PORT = 143
229
SNMP_PORT = 161
230
HTTPS_PORT = 443
231
SMTPS_PORT = 465
232
IMAPS_PORT = 993
233
POP3S_PORT = 995
234
MYSQL_PORT = 3306
235
POSTGRESQL_PORT = 5432
236
REDIS_PORT = 6379
237
HTTP_ALT_PORT = 8080
238
HTTPS_ALT_PORT = 8443
239
240
# Protocol Constants
241
TCP = "tcp"
242
UDP = "udp"
243
ICMP = "icmp"
244
ALL_PROTOCOLS = "-1"
245
```
246
247
### CloudWatch Logs Retention
248
249
Valid retention periods for CloudWatch Logs.
250
251
```python { .api }
252
LOGS_ALLOWED_RETENTION_DAYS = [
253
1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653
254
]
255
```
256
257
## Usage Examples
258
259
### Using Pseudo Parameters
260
261
```python
262
from troposphere import Template, Output, Sub
263
from troposphere import Region, AccountId, StackName
264
265
template = Template()
266
267
# Use pseudo parameters in string substitution
268
template.add_output(Output(
269
"StackInfo",
270
Value=Sub("Stack ${StackName} in region ${Region} for account ${AccountId}", {
271
"StackName": StackName,
272
"Region": Region,
273
"AccountId": AccountId
274
}),
275
Description="Stack information"
276
))
277
278
# Direct usage
279
bucket_name = Sub("${AWS::StackName}-${AWS::AccountId}-bucket")
280
```
281
282
### Using Instance Type Constants
283
284
```python
285
from troposphere import Template, Parameter
286
from troposphere.constants import T2_MICRO, T2_SMALL, T2_MEDIUM, M5_LARGE
287
from troposphere.ec2 import Instance
288
289
template = Template()
290
291
# Parameter with instance type constants
292
instance_type = template.add_parameter(Parameter(
293
"InstanceType",
294
Type="String",
295
Default=T2_MICRO,
296
AllowedValues=[T2_MICRO, T2_SMALL, T2_MEDIUM, M5_LARGE],
297
Description="EC2 instance type"
298
))
299
300
# Use in conditional logic
301
instance = template.add_resource(Instance(
302
"MyInstance",
303
ImageId="ami-0abcdef1234567890",
304
InstanceType=If("IsProduction", M5_LARGE, T2_MICRO)
305
))
306
```
307
308
### Using Network Constants
309
310
```python
311
from troposphere import Template
312
from troposphere.constants import HTTP_PORT, HTTPS_PORT, SSH_PORT, QUAD_ZERO
313
from troposphere.ec2 import SecurityGroup, SecurityGroupRule
314
315
template = Template()
316
317
# Security group with port constants
318
web_sg = template.add_resource(SecurityGroup(
319
"WebServerSecurityGroup",
320
GroupDescription="Security group for web servers",
321
SecurityGroupIngress=[
322
{
323
"IpProtocol": "tcp",
324
"FromPort": HTTP_PORT,
325
"ToPort": HTTP_PORT,
326
"CidrIp": QUAD_ZERO
327
},
328
{
329
"IpProtocol": "tcp",
330
"FromPort": HTTPS_PORT,
331
"ToPort": HTTPS_PORT,
332
"CidrIp": QUAD_ZERO
333
},
334
{
335
"IpProtocol": "tcp",
336
"FromPort": SSH_PORT,
337
"ToPort": SSH_PORT,
338
"CidrIp": "10.0.0.0/8"
339
}
340
]
341
))
342
```
343
344
### Using Region Constants
345
346
```python
347
from troposphere import Template, FindInMap
348
from troposphere.constants import US_EAST_1, US_WEST_2, EU_WEST_1
349
350
template = Template()
351
352
# Regional mappings with constants
353
template.add_mapping("RegionMap", {
354
US_EAST_1: {"AMI": "ami-0ff8a91507f77f867"},
355
US_WEST_2: {"AMI": "ami-0bdf93799014acdc4"},
356
EU_WEST_1: {"AMI": "ami-047bb4163c506cd98"}
357
})
358
359
# Use in resources
360
ami_id = FindInMap("RegionMap", Region, "AMI")
361
```
362
363
### Using Policy Constants
364
365
```python
366
from troposphere import Template
367
from troposphere.constants import Retain, Snapshot
368
from troposphere.ec2 import Instance, Volume
369
370
template = Template()
371
372
# Critical instance with retain policy
373
critical_instance = template.add_resource(Instance(
374
"CriticalInstance",
375
ImageId="ami-0abcdef1234567890",
376
InstanceType="m5.large"
377
))
378
critical_instance.DeletionPolicy = Retain
379
380
# Database volume with snapshot policy
381
db_volume = template.add_resource(Volume(
382
"DatabaseVolume",
383
Size=100,
384
VolumeType="gp3",
385
AvailabilityZone="us-east-1a"
386
))
387
db_volume.DeletionPolicy = Snapshot
388
```