AWS CloudFormation creation library that facilitates building infrastructure templates programmatically in Python
Limited OpenStack Heat template support for organizations using hybrid cloud deployments with both AWS and OpenStack infrastructure.
Basic OpenStack Heat template functionality for hybrid cloud environments.
class HeatTemplate:
def __init__(self, heat_template_version: str = None):
"""
OpenStack Heat template.
Args:
heat_template_version: Heat template version (e.g., "2013-05-23")
"""
def add_resource(self, resource) -> resource:
"""
Add resource to Heat template.
Args:
resource: OpenStack resource object
Returns:
The added resource
"""
def to_dict(self) -> Dict[str, Any]:
"""
Convert template to dictionary format.
Returns:
dict: Heat template as dictionary
"""
def to_yaml(self) -> str:
"""
Convert template to YAML string.
Returns:
str: Heat template as YAML
"""OpenStack compute resources for virtual machines and compute management.
class Server:
resource_type = "OS::Nova::Server"
def __init__(self, title: str, image: str, flavor: str,
networks: list = None, key_name: str = None, **kwargs):
"""
Nova server (virtual machine).
Args:
title: Resource logical name
image: Image ID or name
flavor: Flavor ID or name
networks: List of networks to attach
key_name: Key pair name for SSH access
"""
class KeyPair:
resource_type = "OS::Nova::KeyPair"
def __init__(self, title: str, name: str, public_key: str = None, **kwargs):
"""
Nova key pair for SSH access.
Args:
title: Resource logical name
name: Key pair name
public_key: Public key content (auto-generated if not provided)
"""
class Flavor:
resource_type = "OS::Nova::Flavor"
def __init__(self, title: str, ram: int, vcpus: int, disk: int, **kwargs):
"""
Nova flavor (instance type).
Args:
title: Resource logical name
ram: RAM in MB
vcpus: Number of virtual CPUs
disk: Disk size in GB
"""
class ServerGroup:
resource_type = "OS::Nova::ServerGroup"
def __init__(self, title: str, policies: list, **kwargs):
"""
Nova server group for anti-affinity/affinity policies.
Args:
title: Resource logical name
policies: List of policies ('anti-affinity', 'affinity', etc.)
"""OpenStack networking resources for virtual networks, subnets, routers, and security groups.
class Net:
resource_type = "OS::Neutron::Net"
def __init__(self, title: str, name: str = None, admin_state_up: bool = True, **kwargs):
"""
Neutron network.
Args:
title: Resource logical name
name: Network name
admin_state_up: Administrative state
"""
class Subnet:
resource_type = "OS::Neutron::Subnet"
def __init__(self, title: str, network_id: str, cidr: str,
ip_version: int = 4, **kwargs):
"""
Neutron subnet.
Args:
title: Resource logical name
network_id: Network ID or reference
cidr: CIDR block for subnet
ip_version: IP version (4 or 6)
"""
class Router:
resource_type = "OS::Neutron::Router"
def __init__(self, title: str, external_gateway_info: dict = None, **kwargs):
"""
Neutron router.
Args:
title: Resource logical name
external_gateway_info: External network gateway configuration
"""
class RouterInterface:
resource_type = "OS::Neutron::RouterInterface"
def __init__(self, title: str, router_id: str, subnet_id: str, **kwargs):
"""
Neutron router interface.
Args:
title: Resource logical name
router_id: Router ID or reference
subnet_id: Subnet ID or reference
"""
class SecurityGroup:
resource_type = "OS::Neutron::SecurityGroup"
def __init__(self, title: str, description: str, name: str = None,
rules: list = None, **kwargs):
"""
Neutron security group.
Args:
title: Resource logical name
description: Security group description
name: Security group name
rules: List of security group rules
"""
class SecurityGroupRule:
resource_type = "OS::Neutron::SecurityGroupRule"
def __init__(self, title: str, security_group_id: str, direction: str,
protocol: str = None, port_range_min: int = None,
port_range_max: int = None, remote_ip_prefix: str = None, **kwargs):
"""
Neutron security group rule.
Args:
title: Resource logical name
security_group_id: Security group ID or reference
direction: Traffic direction ('ingress' or 'egress')
protocol: Protocol ('tcp', 'udp', 'icmp')
port_range_min: Minimum port number
port_range_max: Maximum port number
remote_ip_prefix: Remote IP prefix (CIDR)
"""
class FloatingIP:
resource_type = "OS::Neutron::FloatingIP"
def __init__(self, title: str, floating_network_id: str,
port_id: str = None, **kwargs):
"""
Neutron floating IP.
Args:
title: Resource logical name
floating_network_id: External network ID
port_id: Port ID to associate with (optional)
"""
class Port:
resource_type = "OS::Neutron::Port"
def __init__(self, title: str, network_id: str, security_groups: list = None,
fixed_ips: list = None, **kwargs):
"""
Neutron port.
Args:
title: Resource logical name
network_id: Network ID or reference
security_groups: List of security group IDs
fixed_ips: List of fixed IP configurations
"""from troposphere.openstack.heat import HeatTemplate
from troposphere.openstack.nova import Server, KeyPair
from troposphere.openstack.neutron import Net, Subnet, SecurityGroup
# Create Heat template
template = HeatTemplate(heat_template_version="2015-10-15")
# Create network
network = template.add_resource(Net(
"private_network",
name="private-net"
))
# Create subnet
subnet = template.add_resource(Subnet(
"private_subnet",
network_id={"get_resource": "private_network"},
cidr="192.168.1.0/24",
ip_version=4
))
# Create security group
security_group = template.add_resource(SecurityGroup(
"web_security_group",
description="Security group for web servers",
rules=[
{
"direction": "ingress",
"protocol": "tcp",
"port_range_min": 22,
"port_range_max": 22,
"remote_ip_prefix": "0.0.0.0/0"
},
{
"direction": "ingress",
"protocol": "tcp",
"port_range_min": 80,
"port_range_max": 80,
"remote_ip_prefix": "0.0.0.0/0"
}
]
))
# Create key pair
keypair = template.add_resource(KeyPair(
"web_keypair",
name="web-key"
))
# Create server
server = template.add_resource(Server(
"web_server",
image="ubuntu-20.04",
flavor="m1.small",
key_name={"get_resource": "web_keypair"},
networks=[{"network": {"get_resource": "private_network"}}],
security_groups=[{"get_resource": "web_security_group"}]
))
# Generate Heat template
heat_yaml = template.to_yaml()
print(heat_yaml)from troposphere.openstack.heat import HeatTemplate
from troposphere.openstack.nova import Server
from troposphere.openstack.neutron import *
template = HeatTemplate()
# Database tier network
db_network = template.add_resource(Net(
"database_network",
name="db-tier"
))
db_subnet = template.add_resource(Subnet(
"database_subnet",
network_id={"get_resource": "database_network"},
cidr="10.0.1.0/24"
))
# Application tier network
app_network = template.add_resource(Net(
"application_network",
name="app-tier"
))
app_subnet = template.add_resource(Subnet(
"application_subnet",
network_id={"get_resource": "application_network"},
cidr="10.0.2.0/24"
))
# Web tier network (public)
web_network = template.add_resource(Net(
"web_network",
name="web-tier"
))
web_subnet = template.add_resource(Subnet(
"web_subnet",
network_id={"get_resource": "web_network"},
cidr="10.0.3.0/24"
))
# Router for external connectivity
router = template.add_resource(Router(
"main_router",
external_gateway_info={
"network": "public"
}
))
# Connect subnets to router
template.add_resource(RouterInterface(
"web_router_interface",
router_id={"get_resource": "main_router"},
subnet_id={"get_resource": "web_subnet"}
))
# Security groups
db_sg = template.add_resource(SecurityGroup(
"database_sg",
description="Database security group",
rules=[
{
"direction": "ingress",
"protocol": "tcp",
"port_range_min": 3306,
"port_range_max": 3306,
"remote_group_id": {"get_resource": "application_sg"}
}
]
))
app_sg = template.add_resource(SecurityGroup(
"application_sg",
description="Application security group",
rules=[
{
"direction": "ingress",
"protocol": "tcp",
"port_range_min": 8080,
"port_range_max": 8080,
"remote_group_id": {"get_resource": "web_sg"}
}
]
))
web_sg = template.add_resource(SecurityGroup(
"web_sg",
description="Web security group",
rules=[
{
"direction": "ingress",
"protocol": "tcp",
"port_range_min": 80,
"port_range_max": 80,
"remote_ip_prefix": "0.0.0.0/0"
},
{
"direction": "ingress",
"protocol": "tcp",
"port_range_min": 443,
"port_range_max": 443,
"remote_ip_prefix": "0.0.0.0/0"
}
]
))
# Servers
db_server = template.add_resource(Server(
"database_server",
image="mysql-8.0",
flavor="m1.medium",
networks=[{"network": {"get_resource": "database_network"}}],
security_groups=[{"get_resource": "database_sg"}]
))
app_server = template.add_resource(Server(
"application_server",
image="tomcat-9",
flavor="m1.large",
networks=[{"network": {"get_resource": "application_network"}}],
security_groups=[{"get_resource": "application_sg"}]
))
web_server = template.add_resource(Server(
"web_server",
image="nginx-latest",
flavor="m1.small",
networks=[{"network": {"get_resource": "web_network"}}],
security_groups=[{"get_resource": "web_sg"}]
))# Example showing potential integration patterns
# (Note: This is conceptual - actual integration depends on specific setup)
from troposphere import Template as AWSTemplate
from troposphere.openstack.heat import HeatTemplate
from troposphere.ec2 import Instance as AWSInstance
from troposphere.openstack.nova import Server as OpenStackServer
# AWS template
aws_template = AWSTemplate(Description="AWS resources")
aws_instance = aws_template.add_resource(AWSInstance(
"AWSWebServer",
ImageId="ami-0abcdef1234567890",
InstanceType="t3.micro"
))
# OpenStack template
heat_template = HeatTemplate(heat_template_version="2015-10-15")
openstack_server = heat_template.add_resource(OpenStackServer(
"OpenStackDBServer",
image="mysql-8.0",
flavor="m1.medium"
))
# Generate both templates
aws_json = aws_template.to_json()
heat_yaml = heat_template.to_yaml()
print("AWS CloudFormation Template:")
print(aws_json)
print("\nOpenStack Heat Template:")
print(heat_yaml)Note: OpenStack support in Troposphere is limited compared to AWS functionality. For production OpenStack deployments, consider using Heat templates directly or specialized OpenStack orchestration tools. The Troposphere OpenStack support is primarily intended for organizations that need to maintain templates for both AWS and OpenStack environments using a similar Python-based approach.
Install with Tessl CLI
npx tessl i tessl/pypi-troposphere