Why Infrastructure as Code Matters for AI Products
AI products often have more moving parts than standard SaaS: a containerised API service, a background worker for LLM processing, a vector database, a PostgreSQL database, an object storage bucket for document uploads, IAM roles for LLM API access, VPC networking for private communication between services, and CloudFront or an ALB for traffic distribution. Provisioning all of this correctly, with correct security group rules, IAM least-privilege policies, and encrypted storage, is complex enough that manual console clicks introduce errors and omissions. With Terraform, the desired state is declared in code and Terraform computes the plan to reach that state from the current state. Every change is visible as a diff before it is applied, can be reviewed in a pull request, and can be rolled back by reverting the code change. This makes infrastructure changes as safe and reviewable as application code changes.
Terraform Module Structure for AI Infrastructure
SpeedMVPs structures Terraform code in reusable modules rather than a single large configuration file. A typical AI product Terraform repository has a modules directory with modules for: networking (VPC, subnets, security groups), database (RDS PostgreSQL with pgvector support, parameter groups, backup configuration), container services (ECS cluster, ECS service, task definition, ALB, target group), storage (S3 bucket, bucket policy, CloudFront distribution), and IAM (roles and policies for ECS task execution, CI/CD deployment, developer access). Environment configurations (environments/staging and environments/production) call these modules with environment-specific variable values. This structure means adding a new environment is a matter of creating a new environment directory with the appropriate variable values, not duplicating all the module definitions.
Terraform State Management and Remote Backend
Terraform tracks the current state of managed infrastructure in a state file. This state file must be stored in a shared, versioned backend so all developers and CI/CD pipelines see the same state. SpeedMVPs configures Terraform's S3 backend with DynamoDB state locking for AWS projects: the state file is stored in an S3 bucket, and DynamoDB prevents concurrent applies from two processes simultaneously modifying the same infrastructure. For GCP projects, the GCS backend serves the same purpose. Remote state storage is configured from the start of the project, never as a retrofit, because migrating Terraform state after infrastructure has been provisioned using local state is error-prone.
IAM Least Privilege for AI Services
IAM (Identity and Access Management) configuration is where many cloud deployments introduce unnecessary security risk by granting broad permissions for convenience. An ECS task running an AI API service needs specific permissions: read access to specific S3 buckets for document retrieval, invoke permissions for Bedrock if using AWS managed LLMs, and read access to Secrets Manager for API keys. SpeedMVPs writes Terraform IAM policy documents with the minimum permissions required for each service, tested by attempting to run the service with the defined permissions rather than granting wildcard access and trusting it works. For UK financial services and NHS deployments, well-defined IAM permissions are also an auditable security control that satisfies FCA operational resilience and NHS DSPT requirements.
Terraform in CI/CD and Plan Review
SpeedMVPs integrates Terraform into the CI/CD pipeline with a plan-and-apply workflow. On a pull request that modifies Terraform files, GitHub Actions runs terraform plan and posts the plan output as a PR comment, so reviewers can see exactly what infrastructure changes will be applied before approving the PR. On merge to main, the pipeline runs terraform apply automatically (or with a required manual approval step for production). This approach ensures infrastructure changes go through the same code review process as application changes, rather than being applied from a developer's local machine without oversight. Terraform Cloud or Atlantis can provide more advanced plan management if needed.
Terraform for Compliance and Auditability
For AI products in regulated UK industries, Terraform provides auditability that console-click provisioning does not. Every infrastructure change has a git commit with an author, a timestamp, and a diff. Compliance questions such as when was encryption enabled on this database, who changed the security group rule, or can we reproduce the production environment from scratch can be answered from the git history. For NHS Digital DSPT compliance, MHRA software as a medical device guidance, and FCA operational resilience, having infrastructure defined as version-controlled code with an approval workflow is a significant compliance advantage. SpeedMVPs can provide a Terraform compliance summary on request for regulated industry deployments.