Skip to main content
This guide walks through deploying real-world applications with pyinfra, from basic web applications to complex multi-tier architectures. You’ll learn how to structure deployments, handle dependencies, and create reusable deployment patterns.

Deployment Structure

A well-organized deployment typically includes:

Simple Web Application

Let’s start with deploying a Python Flask application.
1

Create inventory

inventory.py
2

Create nginx template

templates/nginx.conf.j2
3

Create systemd service template

templates/app.service.j2
4

Create deploy file

deploy.py
5

Deploy

Use dry-run mode first to preview changes: pyinfra inventory.py deploy.py --dry

Multi-Tier Application

Deploy an application with separate web and database tiers:
inventory.py
deploy.py

Reusable Deploy Functions

Create modular, reusable deployment components:
tasks/web.py
Use in main deploy:
deploy.py

Handling Secrets

Never commit secrets to version control. Use environment variables or external secret management.

Using Environment Variables

inventory.py
Set before deploying:

Using .env Files

inventory.py

Database Migrations

Run database migrations as part of deployment:
deploy.py

Zero-Downtime Deployments

Deploy updates without downtime:
deploy.py

Complete Example: Django Application

Best Practices

Structure your deploys - Split complex deployments into modular functions using @deploy
Test locally first - Use @local or @docker connectors to test deploys before running on production
Use dry-run mode - Always check what will change with --dry before deploying to production
Keep secrets secure - Use environment variables or secret management tools, never commit secrets
Version your infrastructure - Commit inventory and deploy files to git for version control

Next Steps