Infraestrutura como Código - C-Suite
Este documento descreve a implementação de Infraestrutura como Código (IaC) usando Terraform para o ecossistema C-Suite.
Visão Geral
Terraform permite versionar, reproduzir e gerenciar infraestrutura AWS de forma declarativa.
Estrutura
terraform/
├── main.tf # Recursos principais
├── variables.tf # Variáveis
├── outputs.tf # Outputs
├── README.md # Documentação
└── environments/ # Configurações por ambiente
├── dev.tfvars
├── staging.tfvars
└── production.tfvars
Recursos Provisionados
Networking
- VPC com subnets públicas e privadas
- Internet Gateway
- Route Tables
- Security Groups
Banco de Dados
- RDS MySQL
- Subnet Groups
- Security Groups
- Backup automático
Cache
- ElastiCache Redis
- Subnet Groups
- Security Groups
Uso
Inicializar
cd terraform
terraform init
Planejar Mudanças
terraform plan -var-file=environments/dev.tfvars
Aplicar Mudanças
terraform apply -var-file=environments/dev.tfvars
Destruir (CUIDADO!)
terraform destroy -var-file=environments/dev.tfvars
Ambientes
Development
# environments/dev.tfvars
environment = "dev"
aws_region = "us-east-1"
db_instance_class = "db.t3.micro"
redis_node_type = "cache.t2.micro"
Staging
# environments/staging.tfvars
environment = "staging"
aws_region = "us-east-1"
db_instance_class = "db.t3.small"
redis_node_type = "cache.t3.micro"
Production
# environments/production.tfvars
environment = "production"
aws_region = "us-east-1"
db_instance_class = "db.t3.large"
redis_node_type = "cache.t3.small"
enable_backup = true
backup_retention_days = 30
State Management
Backend S3
backend "s3" {
bucket = "csuite-terraform-state"
key = "terraform.tfstate"
region = "us-east-1"
}
Workspaces
# Criar workspace
terraform workspace new dev
# Selecionar workspace
terraform workspace select dev
# Listar workspaces
terraform workspace list
Segurança
Secrets Management
NUNCA commite secrets:
# .gitignore
*.tfvars
*.tfstate
*.tfstate.backup
.terraform/
Usar AWS Secrets Manager
data "aws_secretsmanager_secret_version" "db_password" {
secret_id = "csuite/db/password"
}
variable "db_password" {
default = data.aws_secretsmanager_secret_version.db_password.secret_string
}
CI/CD Integration
GitHub Actions
- name: Terraform Plan
run: |
cd terraform
terraform init
terraform plan -var-file=environments/${{ env.ENVIRONMENT }}.tfvars
- name: Terraform Apply
if: github.ref == 'refs/heads/main'
run: |
cd terraform
terraform apply -auto-approve -var-file=environments/production.tfvars
Best Practices
- Versionamento: Sempre versionar mudanças
- State remoto: Use S3 backend
- Workspaces: Separe ambientes
- Review: Revisar plan antes de apply
- Documentação: Documentar mudanças
- Testing: Testar em dev primeiro
Referências
- Terraform Documentation
- AWS Provider
- Consulte
terraform/README.mdpara detalhes