Skip to content

Python

The InfraWeave Python SDK lets you provision and manage your cloud infrastructure directly from Python.

Installation:
pip install infraweave

Use Cases

  • Integration & resilience testing
    Write end-to-end tests, performance tests or disaster-recovery drills programmatically, then tear down resources automatically.

  • Advanced deployment logic Incorporate conditionals, loops, retries or custom functions—anything that’s awkward or impossible in static YAML.

  • Python-first workflows
    Leverage your favorite Python libraries, IDE tooling, and language features

Authentication & Discovery:

Once you’ve configured your InfraWeave credentials (via environment variables), every module and stack in your control plane becomes immediately available in the SDK. Simply import the package and begin defining or updating resources in code.

Example

In this example it is assumed S3Bucket with version 0.0.11-dev has been published.

If this is used to deploy in AWS, it requires that you provide authentication accordingly, e.g.

  • AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY + AWS_SESSION_TOKEN is set
  • or AWS_PROFILE is set
test.py
from infraweave import S3Bucket, Deployment
bucket_module = S3Bucket(
version='0.0.11-dev',
track="dev"
)
bucket1 = Deployment(
name="bucket1",
namespace="playground",
module=bucket_module,
region="us-west-2"
)
with bucket1:
bucket1.set_variables(
bucket_name="my-bucket12347ydfs3",
enable_acl=False
)
bucket1.apply()
# Run some tests here
# bucket1.destroy() is automatically called when finished (or on error)

Context-Manager Integration

InfraWeave integrates with Python’s context-manager protocol to encapsulate multiple Deployment instances within a single with statement, delivering two key advantages:

  1. Guaranteed cleanup
    No matter what happens inside the block—success, exception, even a keyboard interrupt—all deployments will have their destroy() called.

  2. Reverse-order teardown
    If you create A, then B, then C, they’re destroyed in the order C → B → A, ensuring that dependent resources are torn down safely in the correct order.

Integration Test Example

integration-test.py
from infraweave import VPC, EC2, Deployment
# 1) Prepare your modules
vpc_module = VPC(version="0.0.11-dev", track="dev")
ec2_module = EC2(version="0.1.2-dev", track="dev")
# 2) Instantiate Deployment objects
vpc = Deployment(
name="vpc-main",
namespace="playground",
module=vpc_module,
region="us-west-2"
)
ec2 = Deployment(
name="web-server",
namespace="playground",
module=ec2_module,
region="us-west-2"
)
# 3) Enter both contexts at once
with vpc, ec2:
# Configure & apply VPC
vpc.set_variables(cidr_block="10.0.0.0/16")
vpc.apply()
print("✅ VPC created")
# Configure & apply EC2
ec2.set_variables(instance_type="t3.micro", ami="ami-0abcdef1234567890")
ec2.apply()
print("✅ EC2 instance created")
# run your integration tests here
# Exiting the with-block automatically tears them down in reverse order:
# 1) ec2.destroy()
# 2) vpc.destroy()
print("🗑️ EC2 and VPC resources cleaned up automatically in reverse order")