Cloud Expert
Terraform — Advanced Patterns¶
TerraformIaCHCLState Management 5 min read
Remote state, locking, moved blocks, import and CI/CD pipeline.
Remote State¶
terraform {
backend "s3" {
bucket = "mycompany-tf-state"
key = "prod/infra.tfstate"
region = "eu-west-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
Import and Moved¶
# Import (TF 1.5+)
import {
to = aws_s3_bucket.main
id = "my-existing-bucket"
}
# Moved — refactoring without destroy
moved {
from = aws_instance.web
to = module.compute.aws_instance.web
}
CI/CD¶
name: Terraform
on: [pull_request, push]
jobs:
terraform:
steps:
- run: terraform init
- run: terraform fmt -check
- run: terraform plan -out=tfplan
- run: terraform apply -auto-approve tfplan
if: github.ref == 'refs/heads/main'
Summary¶
Advanced TF = remote state + CI/CD + import for brownfield + moved for refactoring.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.