|
| 1 | +--- |
| 2 | +title: "Infrastructure Generation" |
| 3 | +description: "How Suga transforms projects into production-ready Terraform" |
| 4 | +--- |
| 5 | + |
| 6 | +When you run `suga build`, Suga transforms your high-level project specification into production-ready Terraform code. This page explains how this transformation works, what gets generated, and how to customize the output. |
| 7 | + |
| 8 | +## The Build Process |
| 9 | + |
| 10 | +The `suga build` command orchestrates a multi-step process: |
| 11 | + |
| 12 | +<Steps> |
| 13 | + <Step title="Load Project"> |
| 14 | + Suga reads your `suga.yaml` file and validates the configuration: |
| 15 | + |
| 16 | + ```yaml title="suga.yaml" |
| 17 | + target: suga/aws@1 |
| 18 | + name: my-app |
| 19 | + |
| 20 | + services: |
| 21 | + api: |
| 22 | + subtype: lambda |
| 23 | + dev: |
| 24 | + script: npm run dev |
| 25 | + |
| 26 | + buckets: |
| 27 | + subtype: s3 |
| 28 | + uploads: |
| 29 | + access: |
| 30 | + api: [read, write] |
| 31 | + ``` |
| 32 | + </Step> |
| 33 | +
|
| 34 | + <Step title="Fetch Platform"> |
| 35 | + Suga retrieves the target platform specification from the registry: |
| 36 | +
|
| 37 | + ```bash |
| 38 | + # Platform: suga/aws@1 |
| 39 | + - Services: lambda, fargate |
| 40 | + - Buckets: s3 |
| 41 | + - Databases: neon |
| 42 | + - Entrypoints: cloudfront |
| 43 | + - Infrastructure: vpc, loadbalancer, security-groups |
| 44 | + ``` |
| 45 | + </Step> |
| 46 | +
|
| 47 | + <Step title="Map Resources"> |
| 48 | + Each project resource is mapped to a platform blueprint, by the selected subtype: |
| 49 | +
|
| 50 | + ```yaml suga.yaml |
| 51 | + services: |
| 52 | + api: |
| 53 | + subtype: fargate |
| 54 | + |
| 55 | + buckets: |
| 56 | + uploads: |
| 57 | + subtype: s3 |
| 58 | + ``` |
| 59 | +
|
| 60 | + ``` |
| 61 | + Project Platform Blueprint Plugin |
| 62 | + ------- ------------------ ------ |
| 63 | + service "api" → services.fargate → suga/aws/fargate |
| 64 | + bucket "uploads" → buckets.s3 → suga/aws/s3-bucket |
| 65 | + ``` |
| 66 | + |
| 67 | + </Step> |
| 68 | + |
| 69 | + <Step title="Generate Terraform Output"> |
| 70 | + Suga then uses Terraform CDK to resolve the modules, variables and other configuration to produce a Terraform stack. |
| 71 | + |
| 72 | + ``` |
| 73 | + terraform/stacks/my-app/ |
| 74 | + ├── cdk.tf.json # Main Terraform Entrypoint |
| 75 | + └── .terraform/ # Plugin Terraform modules |
| 76 | + └── modules/ |
| 77 | + ├── api/ |
| 78 | + ├── api_image/ |
| 79 | + ├── uploads_bucket/ |
| 80 | + └── ... |
| 81 | + ``` |
| 82 | + </Step> |
| 83 | +</Steps> |
| 84 | + |
| 85 | +## Module Naming |
| 86 | + |
| 87 | +Suga generates consistent module names for application resources: |
| 88 | + |
| 89 | +### Terraform Module Names |
| 90 | + |
| 91 | +Pattern: `{resource_name}_{submodule_name}` |
| 92 | + |
| 93 | +```hcl |
| 94 | +module "api" # Service named "api" |
| 95 | +module "api_image" # Container image submodule for the "api" service |
| 96 | +module "uploads" # Bucket named "uploads" |
| 97 | +``` |
| 98 | + |
| 99 | +## Dependency Management |
| 100 | + |
| 101 | +Suga automatically manages resource dependencies: |
| 102 | + |
| 103 | +### Explicit Dependencies |
| 104 | + |
| 105 | +Defined by platform: |
| 106 | + |
| 107 | +```yaml title="Platform blueprint" |
| 108 | +services: |
| 109 | + fargate: |
| 110 | + depends_on: |
| 111 | + - ${infra.aws_vpc} |
| 112 | + - ${infra.aws_lb} |
| 113 | +``` |
| 114 | +
|
| 115 | +Results in: |
| 116 | +
|
| 117 | +```hcl title="Generated Terraform" |
| 118 | +module "service_api" { |
| 119 | + # ... |
| 120 | + depends_on = [ |
| 121 | + module.aws_vpc, |
| 122 | + module.aws_lb |
| 123 | + ] |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### Implicit Dependencies |
| 128 | + |
| 129 | +Created by resource references: |
| 130 | + |
| 131 | +```hcl title="Automatic dependency" |
| 132 | +module "service_api" { |
| 133 | + vpc_id = module.aws_vpc.vpc_id # Creates implicit dependency |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Multi-Environment Support |
| 138 | + |
| 139 | +The same generated Terraform can deploy to multiple environments using Terraform workspaces and environment-specific variable files. This allows you to maintain a single infrastructure definition while customizing configuration for dev, staging, and production. |
| 140 | + |
| 141 | +<Card title="Terraform Environment Management" icon="layers" href="/deploy/terraform-configuration#environment-management" horizontal> |
| 142 | + Learn how to manage multiple environments with Terraform workspaces and variables |
| 143 | +</Card> |
| 144 | + |
| 145 | +See the [CLI reference](/cli/build) for complete build options. |
| 146 | + |
| 147 | +## Getting Help |
| 148 | + |
| 149 | +If you encounter issues during build: |
| 150 | + |
| 151 | +1. Review platform documentation in the [platform browser](https://app.addsuga.com/browse/platforms) |
| 152 | +2. Contact [Suga support](/support) |
| 153 | + |
| 154 | +## Best Practices |
| 155 | + |
| 156 | +### 1. Version Control Generated Terraform |
| 157 | + |
| 158 | +Commit generated Terraform to version control: |
| 159 | + |
| 160 | +```bash |
| 161 | +git add terraform/ |
| 162 | +git commit -m "Update infrastructure" |
| 163 | +``` |
| 164 | + |
| 165 | +Benefits: |
| 166 | +- Track infrastructure changes over time |
| 167 | +- Review Terraform diffs in pull requests |
| 168 | +- Rollback if needed |
| 169 | + |
| 170 | +### 2. Validate Before Deploying |
| 171 | + |
| 172 | +Always validate and preview: |
| 173 | + |
| 174 | +```bash |
| 175 | +suga build |
| 176 | +cd terraform/stacks/my-app |
| 177 | +terraform init |
| 178 | +terraform validate |
| 179 | +terraform plan # Review before apply |
| 180 | +``` |
| 181 | + |
| 182 | +## Learn More |
| 183 | + |
| 184 | +<CardGroup cols={2}> |
| 185 | + <Card title="Deployment Guide" icon="rocket" href="/deploy/overview"> |
| 186 | + Understand the deployment process |
| 187 | + </Card> |
| 188 | + |
| 189 | + <Card title="Build Command" icon="hammer" href="/cli/build"> |
| 190 | + Complete CLI reference for `suga build` |
| 191 | + </Card> |
| 192 | + |
| 193 | + <Card title="Platform Development" icon="layers" href="/guides/build-platform"> |
| 194 | + Learn how platforms define infrastructure |
| 195 | + </Card> |
| 196 | + |
| 197 | + <Card title="Terraform Configuration" icon="code" href="/deploy/terraform-configuration"> |
| 198 | + Best practices for Terraform configuration |
| 199 | + </Card> |
| 200 | +</CardGroup> |
0 commit comments