> ## Documentation Index
> Fetch the complete documentation index at: https://backdrift.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Infrastructure Export

> Take full control with Infrastructure-as-Code.

Backdrift generates standard **AWS CDK** (Cloud Development Kit) code. You can export this code and deploy it yourself, modify it, or use it as a learning resource.

## What is CDK?

AWS CDK is an open-source framework that lets you define cloud infrastructure using programming languages. Backdrift generates **Python CDK** code.

## Export the CDK Project

From the **Artifacts** tab, click **Export CDK** to download a ZIP file containing:

```
cdk-project/
├── app.py                    # CDK app entry point
├── requirements.txt          # Python dependencies
├── cdk.json                  # CDK configuration
├── stacks/
│   ├── cognito_stack.py      # Authentication
│   ├── dynamodb_stack.py     # Database tables
│   ├── lambda_stack.py       # API handlers
│   ├── api_gateway_stack.py  # REST API
│   ├── eventbridge_stack.py  # Scheduled jobs
│   ├── sqs_stack.py          # Async queues
│   ├── ses_stack.py          # Email
│   ├── s3_stack.py           # File storage
│   └── cloudwatch_stack.py   # Monitoring
├── handlers/
│   ├── crud.py               # CRUD operations
│   ├── health.py             # Health check
│   ├── authorizer.py         # JWT validation
│   └── jobs/                 # Job handlers
└── README.md                 # Deployment instructions
```

## Deploy Yourself

### Prerequisites

* Python 3.9+
* AWS CLI configured with credentials
* AWS CDK CLI installed

### Steps

```bash theme={null}
# Unzip the export
unzip cdk-export.zip
cd cdk-project

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # Linux/Mac
# or: .venv\Scripts\activate  # Windows

# Install dependencies
pip install -r requirements.txt

# Bootstrap CDK (first time only)
cdk bootstrap

# Preview changes
cdk diff

# Deploy
cdk deploy --all
```

## Why Export?

<CardGroup cols={2}>
  <Card title="No Lock-in" icon="lock-open">
    Your infrastructure is standard AWS resources. Take it with you anytime.
  </Card>

  <Card title="Customization" icon="wrench">
    Modify the generated code to add features Backdrift doesn't support yet.
  </Card>

  <Card title="Learning" icon="graduation-cap">
    Study how production infrastructure is built with CDK.
  </Card>

  <Card title="CI/CD Integration" icon="code-branch">
    Integrate with your existing deployment pipelines.
  </Card>
</CardGroup>

## What's Generated

### Cognito Stack

```python theme={null}
# User Pool with password policies
user_pool = cognito.UserPool(
    self, "UserPool",
    user_pool_name=f"{project_name}-users",
    self_sign_up_enabled=True,
    password_policy=cognito.PasswordPolicy(
        min_length=8,
        require_lowercase=True,
        require_uppercase=True,
        require_digits=True,
    ),
)
```

### DynamoDB Stack

```python theme={null}
# Table for each entity with GSIs
tasks_table = dynamodb.Table(
    self, "TasksTable",
    table_name=f"{project_name}-tasks",
    partition_key=dynamodb.Attribute(
        name="id",
        type=dynamodb.AttributeType.STRING
    ),
    billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST,
    point_in_time_recovery=True,
)

# Global Secondary Index for tenant queries
tasks_table.add_global_secondary_index(
    index_name="by-org",
    partition_key=dynamodb.Attribute(
        name="org_id",
        type=dynamodb.AttributeType.STRING
    ),
)
```

### Lambda Stack

```python theme={null}
# CRUD handlers with proper IAM permissions
create_task = lambda_.Function(
    self, "CreateTask",
    runtime=lambda_.Runtime.PYTHON_3_11,
    handler="crud.create_handler",
    code=lambda_.Code.from_asset("handlers"),
    environment={
        "TABLE_NAME": tasks_table.table_name,
    },
)
tasks_table.grant_write_data(create_task)
```

## Modifying the Export

Common modifications:

### Add a VPC

```python theme={null}
from aws_cdk import aws_ec2 as ec2

vpc = ec2.Vpc(self, "ApiVpc", max_azs=2)

# Update Lambda to run in VPC
create_task = lambda_.Function(
    self, "CreateTask",
    vpc=vpc,
    vpc_subnets=ec2.SubnetSelection(
        subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS
    ),
    # ... rest of config
)
```

### Add Custom Domain

```python theme={null}
from aws_cdk import aws_route53 as route53
from aws_cdk import aws_certificatemanager as acm

certificate = acm.Certificate(
    self, "ApiCert",
    domain_name="api.example.com",
    validation=acm.CertificateValidation.from_dns(hosted_zone)
)

api.add_domain_name(
    "CustomDomain",
    domain_name="api.example.com",
    certificate=certificate
)
```

### Add WAF

```python theme={null}
from aws_cdk import aws_wafv2 as waf

web_acl = waf.CfnWebACL(
    self, "ApiWaf",
    scope="REGIONAL",
    # ... WAF rules
)
```

## Stack Dependencies

The CDK project respects dependencies between stacks:

```
cognito_stack
     ↓
dynamodb_stack
     ↓
lambda_stack ← depends on tables and auth
     ↓
api_gateway_stack ← depends on Lambda functions
     ↓
amplify_stack ← depends on API URL
```

<Card title="Admin Dashboard" icon="gauge" href="/operations/admin-dashboard">
  Learn about managing your deployed infrastructure.
</Card>
