Infrastructure As Code

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

Banco de Dados

Cache

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

  1. Versionamento: Sempre versionar mudanças
  2. State remoto: Use S3 backend
  3. Workspaces: Separe ambientes
  4. Review: Revisar plan antes de apply
  5. Documentação: Documentar mudanças
  6. Testing: Testar em dev primeiro

Referências

🔊 Text-to-Speech

1.0x
1.0
Pronto para reproduzir