Manually provisioning Magento infrastructure is a reliability risk. When a server dies, a manually provisioned setup can take hours to recreate. With Terraform, you can rebuild your entire AWS infrastructure from scratch in under 20 minutes. Here's our Terraform module structure for a production Magento deployment.
Module Structure
- modules/networking — VPC, subnets, security groups, NAT gateway
- modules/compute — EC2 launch template, Auto Scaling Group, ALB
- modules/database — RDS Aurora cluster, parameter groups, subnet groups
- modules/cache — ElastiCache Redis cluster, replication group
- modules/cdn — CloudFront distribution, cache behaviors, WAF association
- modules/storage — S3 bucket for media, EFS for shared files
- modules/secrets — Secrets Manager for DB credentials and API keys
State Management
Store Terraform state in S3 with DynamoDB for locking. Never commit state files to Git. Use workspaces to separate staging and production environments with the same module code. Enforce state locking — concurrent applies can corrupt infrastructure state and cause outages.
Secrets Handling
Never put secrets in Terraform variables or state files. Use AWS Secrets Manager: Terraform creates the secret resource, but the actual secret value is set via CLI or AWS Console outside of Terraform's management. EC2 instances retrieve secrets at boot time via the AWS SDK — no credentials in configuration files.