AWS CloudFormation creation library that facilitates building infrastructure templates programmatically in Python
Pre-defined constants for AWS regions, instance types, ports, pseudo parameters, and CloudFormation limits to improve template readability and reduce errors.
CloudFormation pseudo parameters that provide runtime values about the stack and AWS environment.
# Pseudo Parameter String Constants
AWS_ACCOUNT_ID: str = "AWS::AccountId"
AWS_NOTIFICATION_ARNS: str = "AWS::NotificationARNs"
AWS_NO_VALUE: str = "AWS::NoValue"
AWS_PARTITION: str = "AWS::Partition"
AWS_REGION: str = "AWS::Region"
AWS_STACK_ID: str = "AWS::StackId"
AWS_STACK_NAME: str = "AWS::StackName"
AWS_URL_SUFFIX: str = "AWS::URLSuffix"
# Pre-defined Ref Objects for Pseudo Parameters
AccountId: Ref = Ref(AWS_ACCOUNT_ID)
NotificationARNs: Ref = Ref(AWS_NOTIFICATION_ARNS)
NoValue: Ref = Ref(AWS_NO_VALUE)
Partition: Ref = Ref(AWS_PARTITION)
Region: Ref = Ref(AWS_REGION)
StackId: Ref = Ref(AWS_STACK_ID)
StackName: Ref = Ref(AWS_STACK_NAME)
URLSuffix: Ref = Ref(AWS_URL_SUFFIX)Constants for DeletionPolicy and UpdateReplacePolicy values.
Delete: str = "Delete"
Retain: str = "Retain"
RetainExceptOnCreate: str = "RetainExceptOnCreate"
Snapshot: str = "Snapshot"CloudFormation template limits for resources, parameters, outputs, and mappings.
MAX_MAPPINGS: int = 200
MAX_OUTPUTS: int = 200
MAX_PARAMETERS: int = 200
MAX_RESOURCES: int = 500
PARAMETER_TITLE_MAX: int = 255All AWS regions with standardized constant names.
# US Regions
US_EAST_1 = "us-east-1"
US_EAST_2 = "us-east-2"
US_WEST_1 = "us-west-1"
US_WEST_2 = "us-west-2"
# Europe Regions
EU_CENTRAL_1 = "eu-central-1"
EU_NORTH_1 = "eu-north-1"
EU_SOUTH_1 = "eu-south-1"
EU_WEST_1 = "eu-west-1"
EU_WEST_2 = "eu-west-2"
EU_WEST_3 = "eu-west-3"
# Asia Pacific Regions
AP_EAST_1 = "ap-east-1"
AP_NORTHEAST_1 = "ap-northeast-1"
AP_NORTHEAST_2 = "ap-northeast-2"
AP_NORTHEAST_3 = "ap-northeast-3"
AP_SOUTH_1 = "ap-south-1"
AP_SOUTHEAST_1 = "ap-southeast-1"
AP_SOUTHEAST_2 = "ap-southeast-2"
AP_SOUTHEAST_3 = "ap-southeast-3"
# Other Regions
AF_SOUTH_1 = "af-south-1"
CA_CENTRAL_1 = "ca-central-1"
CN_NORTH_1 = "cn-north-1"
CN_NORTHWEST_1 = "cn-northwest-1"
ME_CENTRAL_1 = "me-central-1"
ME_SOUTH_1 = "me-south-1"
SA_EAST_1 = "sa-east-1"
US_GOV_EAST_1 = "us-gov-east-1"
US_GOV_WEST_1 = "us-gov-west-1"Availability zone constants for all AWS regions.
# US East 1 Availability Zones
US_EAST_1A = "us-east-1a"
US_EAST_1B = "us-east-1b"
US_EAST_1C = "us-east-1c"
US_EAST_1D = "us-east-1d"
US_EAST_1E = "us-east-1e"
US_EAST_1F = "us-east-1f"
# US West 2 Availability Zones
US_WEST_2A = "us-west-2a"
US_WEST_2B = "us-west-2b"
US_WEST_2C = "us-west-2c"
US_WEST_2D = "us-west-2d"
# Additional zones available for all regions...Constants for all EC2 instance types across different families.
# General Purpose - T2 Family
T2_NANO = "t2.nano"
T2_MICRO = "t2.micro"
T2_SMALL = "t2.small"
T2_MEDIUM = "t2.medium"
T2_LARGE = "t2.large"
T2_XLARGE = "t2.xlarge"
T2_2XLARGE = "t2.2xlarge"
# General Purpose - T3 Family
T3_NANO = "t3.nano"
T3_MICRO = "t3.micro"
T3_SMALL = "t3.small"
T3_MEDIUM = "t3.medium"
T3_LARGE = "t3.large"
T3_XLARGE = "t3.xlarge"
T3_2XLARGE = "t3.2xlarge"
# General Purpose - M5 Family
M5_LARGE = "m5.large"
M5_XLARGE = "m5.xlarge"
M5_2XLARGE = "m5.2xlarge"
M5_4XLARGE = "m5.4xlarge"
M5_8XLARGE = "m5.8xlarge"
M5_12XLARGE = "m5.12xlarge"
M5_16XLARGE = "m5.16xlarge"
M5_24XLARGE = "m5.24xlarge"
# Compute Optimized - C5 Family
C5_LARGE = "c5.large"
C5_XLARGE = "c5.xlarge"
C5_2XLARGE = "c5.2xlarge"
C5_4XLARGE = "c5.4xlarge"
C5_9XLARGE = "c5.9xlarge"
C5_12XLARGE = "c5.12xlarge"
C5_18XLARGE = "c5.18xlarge"
C5_24XLARGE = "c5.24xlarge"
# Memory Optimized - R5 Family
R5_LARGE = "r5.large"
R5_XLARGE = "r5.xlarge"
R5_2XLARGE = "r5.2xlarge"
R5_4XLARGE = "r5.4xlarge"
R5_8XLARGE = "r5.8xlarge"
R5_12XLARGE = "r5.12xlarge"
R5_16XLARGE = "r5.16xlarge"
R5_24XLARGE = "r5.24xlarge"
# Additional instance families available...Constants for RDS database instance classes.
# General Purpose - db.t3 Family
DB_T3_MICRO = "db.t3.micro"
DB_T3_SMALL = "db.t3.small"
DB_T3_MEDIUM = "db.t3.medium"
DB_T3_LARGE = "db.t3.large"
DB_T3_XLARGE = "db.t3.xlarge"
DB_T3_2XLARGE = "db.t3.2xlarge"
# General Purpose - db.m5 Family
DB_M5_LARGE = "db.m5.large"
DB_M5_XLARGE = "db.m5.xlarge"
DB_M5_2XLARGE = "db.m5.2xlarge"
DB_M5_4XLARGE = "db.m5.4xlarge"
DB_M5_8XLARGE = "db.m5.8xlarge"
DB_M5_12XLARGE = "db.m5.12xlarge"
DB_M5_16XLARGE = "db.m5.16xlarge"
DB_M5_24XLARGE = "db.m5.24xlarge"
# Memory Optimized - db.r5 Family
DB_R5_LARGE = "db.r5.large"
DB_R5_XLARGE = "db.r5.xlarge"
DB_R5_2XLARGE = "db.r5.2xlarge"
DB_R5_4XLARGE = "db.r5.4xlarge"
DB_R5_8XLARGE = "db.r5.8xlarge"
DB_R5_12XLARGE = "db.r5.12xlarge"
DB_R5_16XLARGE = "db.r5.16xlarge"
DB_R5_24XLARGE = "db.r5.24xlarge"Common networking constants for CIDR blocks, ports, and protocols.
# CIDR Blocks
QUAD_ZERO = "0.0.0.0/0"
VPC_CIDR_16 = "10.0.0.0/16"
VPC_CIDR_20 = "10.0.0.0/20"
PRIVATE_SUBNET_CIDR_24 = "10.0.1.0/24"
PUBLIC_SUBNET_CIDR_24 = "10.0.2.0/24"
# Common Ports
SSH_PORT = 22
TELNET_PORT = 23
SMTP_PORT = 25
DNS_PORT = 53
HTTP_PORT = 80
POP3_PORT = 110
NTP_PORT = 123
IMAP_PORT = 143
SNMP_PORT = 161
HTTPS_PORT = 443
SMTPS_PORT = 465
IMAPS_PORT = 993
POP3S_PORT = 995
MYSQL_PORT = 3306
POSTGRESQL_PORT = 5432
REDIS_PORT = 6379
HTTP_ALT_PORT = 8080
HTTPS_ALT_PORT = 8443
# Protocol Constants
TCP = "tcp"
UDP = "udp"
ICMP = "icmp"
ALL_PROTOCOLS = "-1"Valid retention periods for CloudWatch Logs.
LOGS_ALLOWED_RETENTION_DAYS = [
1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653
]from troposphere import Template, Output, Sub
from troposphere import Region, AccountId, StackName
template = Template()
# Use pseudo parameters in string substitution
template.add_output(Output(
"StackInfo",
Value=Sub("Stack ${StackName} in region ${Region} for account ${AccountId}", {
"StackName": StackName,
"Region": Region,
"AccountId": AccountId
}),
Description="Stack information"
))
# Direct usage
bucket_name = Sub("${AWS::StackName}-${AWS::AccountId}-bucket")from troposphere import Template, Parameter
from troposphere.constants import T2_MICRO, T2_SMALL, T2_MEDIUM, M5_LARGE
from troposphere.ec2 import Instance
template = Template()
# Parameter with instance type constants
instance_type = template.add_parameter(Parameter(
"InstanceType",
Type="String",
Default=T2_MICRO,
AllowedValues=[T2_MICRO, T2_SMALL, T2_MEDIUM, M5_LARGE],
Description="EC2 instance type"
))
# Use in conditional logic
instance = template.add_resource(Instance(
"MyInstance",
ImageId="ami-0abcdef1234567890",
InstanceType=If("IsProduction", M5_LARGE, T2_MICRO)
))from troposphere import Template
from troposphere.constants import HTTP_PORT, HTTPS_PORT, SSH_PORT, QUAD_ZERO
from troposphere.ec2 import SecurityGroup, SecurityGroupRule
template = Template()
# Security group with port constants
web_sg = template.add_resource(SecurityGroup(
"WebServerSecurityGroup",
GroupDescription="Security group for web servers",
SecurityGroupIngress=[
{
"IpProtocol": "tcp",
"FromPort": HTTP_PORT,
"ToPort": HTTP_PORT,
"CidrIp": QUAD_ZERO
},
{
"IpProtocol": "tcp",
"FromPort": HTTPS_PORT,
"ToPort": HTTPS_PORT,
"CidrIp": QUAD_ZERO
},
{
"IpProtocol": "tcp",
"FromPort": SSH_PORT,
"ToPort": SSH_PORT,
"CidrIp": "10.0.0.0/8"
}
]
))from troposphere import Template, FindInMap
from troposphere.constants import US_EAST_1, US_WEST_2, EU_WEST_1
template = Template()
# Regional mappings with constants
template.add_mapping("RegionMap", {
US_EAST_1: {"AMI": "ami-0ff8a91507f77f867"},
US_WEST_2: {"AMI": "ami-0bdf93799014acdc4"},
EU_WEST_1: {"AMI": "ami-047bb4163c506cd98"}
})
# Use in resources
ami_id = FindInMap("RegionMap", Region, "AMI")from troposphere import Template
from troposphere.constants import Retain, Snapshot
from troposphere.ec2 import Instance, Volume
template = Template()
# Critical instance with retain policy
critical_instance = template.add_resource(Instance(
"CriticalInstance",
ImageId="ami-0abcdef1234567890",
InstanceType="m5.large"
))
critical_instance.DeletionPolicy = Retain
# Database volume with snapshot policy
db_volume = template.add_resource(Volume(
"DatabaseVolume",
Size=100,
VolumeType="gp3",
AvailabilityZone="us-east-1a"
))
db_volume.DeletionPolicy = SnapshotInstall with Tessl CLI
npx tessl i tessl/pypi-troposphere