Infrastructure as Code Guide
Table of Contents
- Overview
- Architecture
- Components
- Getting Started
- Prerequisites
- Common Workflows
- Related Documentation
- Troubleshooting
Overview
VoiceAssist V2 uses a comprehensive Infrastructure as Code (IaC) approach to manage all infrastructure components. This approach provides:
- Version Control: All infrastructure changes are tracked in Git
- Reproducibility: Environments can be created and destroyed consistently
- Automation: CI/CD pipelines automate infrastructure deployment
- HIPAA Compliance: Security controls are codified and enforced
- Cost Management: Infrastructure costs are predictable and optimized
IaC Stack
┌─────────────────────────────────────────────────────────────┐
│ GitHub Actions CI/CD │
│ (Orchestration, Testing, Security Scanning, Deployment) │
└─────────────────────────────────────────────────────────────┘
│
┌─────────────┴──────────────┐
│ │
┌───────▼────────┐ ┌───────▼────────┐
│ Terraform │ │ Ansible │
│ (Provision) │ │ (Configure) │
└───────┬────────┘ └───────┬────────┘
│ │
┌───────────┴──────────┐ ┌──────────┴───────────┐
│ │ │ │
┌───▼────┐ ┌──────────┐ │ ┌──▼─────┐ ┌──────────┐ │
│ VPC │ │ EKS │ │ │ Common │ │ Security │ │
│ │ │ │ │ │ │ │ │ │
└────────┘ └──────────┘ │ └────────┘ └──────────┘ │
│ │
┌────────┐ ┌──────────┐ │ ┌────────┐ ┌──────────┐ │
│ RDS │ │ Redis │ │ │ Docker │ │ K8s │ │
│ │ │ │ │ │ │ │ │ │
└────────┘ └──────────┘ │ └────────┘ └──────────┘ │
│ │
┌────────┐ ┌──────────┐ │ ┌────────┐ │
│ IAM │ │ Security │ │ │Monitor │ │
│ │ │ Groups │ │ │ │ │
└────────┘ └──────────┘ │ └────────┘ │
│ │
AWS Resources │ Server Configuration │
└──────────────────┴────────────────────────────┘
Architecture
High-Level Architecture
VoiceAssist infrastructure is organized into three layers:
-
Cloud Infrastructure Layer (Terraform)
- Network: VPC, subnets, routing, NAT gateways
- Compute: EKS cluster, node groups
- Data: RDS PostgreSQL, ElastiCache Redis
- Security: IAM roles, security groups, KMS encryption
- Monitoring: CloudWatch logs, VPC flow logs
-
Configuration Layer (Ansible)
- OS hardening and security configuration
- Docker installation and configuration
- Kubernetes tools setup
- Monitoring agents installation
- HIPAA compliance settings
-
Application Layer (Kubernetes/Docker Compose)
- Microservices deployment
- Service mesh configuration
- Application secrets management
- Application monitoring
Environment Structure
voiceassist/
├── development (dev)
│ ├── Single AZ deployment
│ ├── Reduced capacity
│ └── Cost-optimized settings
│
├── staging
│ ├── Multi-AZ deployment
│ ├── Production-like capacity
│ └── Full security controls
│
└── production
├── Multi-AZ high availability
├── Auto-scaling enabled
└── Maximum security & compliance
Components
Terraform Modules
The infrastructure is organized into reusable Terraform modules:
| Module | Purpose | Key Resources |
|---|---|---|
| VPC | Network infrastructure | VPC, subnets, NAT gateways, route tables, flow logs |
| EKS | Kubernetes cluster | EKS cluster, node groups, IRSA, add-ons |
| RDS | PostgreSQL database | RDS instance, subnet group, parameter group, backups |
| ElastiCache | Redis cache | Redis cluster, replication group, subnet group |
| IAM | Access control | Service roles, policies, IRSA roles |
| Security Groups | Network security | Security groups for EKS, RDS, Redis, ALB |
Ansible Roles
Server configuration is organized into Ansible roles:
| Role | Purpose | Key Tasks |
|---|---|---|
| common | Base system setup | Package updates, timezone, NTP, users |
| security | Security hardening | Firewall, fail2ban, SSH hardening, audit logs |
| docker | Container runtime | Docker installation, daemon config, user permissions |
| kubernetes | K8s tools | kubectl, helm, kubeconfig setup |
| monitoring | Observability | Prometheus node exporter, log forwarding |
CI/CD Pipelines
GitHub Actions workflows automate the entire deployment lifecycle:
| Workflow | Trigger | Purpose |
|---|---|---|
| CI Pipeline | PR/Push | Lint, test, security scan code |
| Terraform Plan | PR to main | Preview infrastructure changes |
| Terraform Apply | Merge to main | Apply infrastructure changes |
| Security Scan | PR/Push | Scan for vulnerabilities |
| Build & Deploy | Tag/Manual | Build images and deploy to K8s |
Getting Started
Quick Start
-
Clone the repository
git clone https://github.com/yourusername/voiceassist.git cd voiceassist -
Set up prerequisites (see Prerequisites)
-
Configure AWS credentials
export AWS_ACCESS_KEY_ID="your-access-key" export AWS_SECRET_ACCESS_KEY="your-secret-key" export AWS_REGION="us-east-1" -
Initialize Terraform
cd infrastructure/terraform terraform init -
Plan infrastructure
terraform plan -var-file="environments/dev.tfvars" -
Apply infrastructure
terraform apply -var-file="environments/dev.tfvars" -
Configure servers with Ansible
cd infrastructure/ansible ansible-playbook -i inventories/dev site.yml
Environment-Specific Deployment
See detailed guides for each tool:
Prerequisites
Required Tools
| Tool | Version | Purpose |
|---|---|---|
| Terraform | >= 1.6.0 | Infrastructure provisioning |
| Ansible | >= 2.15 | Server configuration |
| AWS CLI | >= 2.13 | AWS resource management |
| kubectl | >= 1.28 | Kubernetes management |
| helm | >= 3.12 | Kubernetes package management |
| Docker | >= 24.0 | Container runtime |
Installation
macOS (using Homebrew)
brew install terraform ansible awscli kubectl helm docker
Linux (Ubuntu/Debian)
# Terraform wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list sudo apt update && sudo apt install terraform # Ansible sudo apt install ansible # AWS CLI curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install # kubectl curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl # helm curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
AWS Account Setup
-
Create IAM user with required permissions
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ec2:*", "eks:*", "rds:*", "elasticache:*", "iam:*", "s3:*", "dynamodb:*", "secretsmanager:*", "kms:*", "logs:*", "cloudwatch:*" ], "Resource": "*" } ] } -
Configure AWS CLI
aws configure -
Create S3 bucket for Terraform state
aws s3 mb s3://voiceassist-terraform-state aws s3api put-bucket-versioning \ --bucket voiceassist-terraform-state \ --versioning-configuration Status=Enabled -
Create DynamoDB table for state locking
aws dynamodb create-table \ --table-name voiceassist-terraform-locks \ --attribute-definitions AttributeName=LockID,AttributeType=S \ --key-schema AttributeName=LockID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
GitHub Setup
-
Configure repository secrets
Navigate to:
Settings>Secrets and variables>ActionsRequired secrets:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYINFRACOST_API_KEY(optional, for cost estimation)CODECOV_TOKEN(optional, for code coverage)
-
Enable GitHub Actions
Ensure GitHub Actions is enabled in repository settings.
Common Workflows
Creating a New Environment
# 1. Create Terraform variables file cp infrastructure/terraform/environments/dev.tfvars infrastructure/terraform/environments/newenv.tfvars # 2. Edit variables for new environment vim infrastructure/terraform/environments/newenv.tfvars # 3. Create Ansible inventory mkdir -p infrastructure/ansible/inventories/newenv vim infrastructure/ansible/inventories/newenv/hosts.yml # 4. Plan infrastructure cd infrastructure/terraform terraform workspace new newenv terraform plan -var-file="environments/newenv.tfvars" # 5. Apply infrastructure terraform apply -var-file="environments/newenv.tfvars" # 6. Configure servers cd ../ansible ansible-playbook -i inventories/newenv site.yml
Updating Infrastructure
# 1. Make changes to Terraform code vim infrastructure/terraform/main.tf # 2. Format code terraform fmt -recursive # 3. Validate changes terraform validate # 4. Plan changes terraform plan -var-file="environments/production.tfvars" # 5. Apply changes (after review) terraform apply -var-file="environments/production.tfvars"
Rolling Back Changes
# Option 1: Revert Git commit and reapply git revert <commit-hash> terraform apply -var-file="environments/production.tfvars" # Option 2: Use Terraform state to restore previous version terraform state pull > backup.tfstate # Make manual corrections terraform state push backup.tfstate
Destroying Infrastructure
# Development environment terraform destroy -var-file="environments/dev.tfvars" # Production environment (requires confirmation) terraform destroy -var-file="environments/production.tfvars"
Related Documentation
- Terraform Guide - Detailed Terraform documentation
- Ansible Guide - Detailed Ansible documentation
- CI/CD Guide - CI/CD pipeline documentation
- Deployment Guide - Deployment procedures
- Architecture - System architecture documentation
- Security - Security controls and compliance
Troubleshooting
Terraform Issues
Issue: State file locked
Error: Error acquiring the state lock
Solution:
# Check DynamoDB for lock aws dynamodb get-item \ --table-name voiceassist-terraform-locks \ --key '{"LockID":{"S":"voiceassist/production/terraform.tfstate"}}' # Force unlock (use with caution) terraform force-unlock <lock-id>
Issue: Provider authentication failed
Error: error configuring Terraform AWS Provider: no valid credential sources
Solution:
# Verify AWS credentials aws sts get-caller-identity # Re-configure if needed aws configure
Ansible Issues
Issue: SSH connection failed
fatal: [host]: UNREACHABLE! => {"msg": "Failed to connect to the host"}
Solution:
# Test SSH connectivity ssh -i ~/.ssh/id_rsa ubuntu@<host-ip> # Verify SSH key permissions chmod 600 ~/.ssh/id_rsa # Check Ansible inventory ansible-inventory -i inventories/dev --list
Issue: Permission denied
fatal: [host]: FAILED! => {"msg": "Missing sudo password"}
Solution:
# Use --ask-become-pass flag ansible-playbook -i inventories/dev site.yml --ask-become-pass # Or configure passwordless sudo on target hosts
CI/CD Issues
Issue: Workflow fails with permission error
Error: The workflow is not permitted to access the repository
Solution:
- Check GitHub Actions permissions:
Settings>Actions>General - Enable "Read and write permissions"
- Re-run workflow
Issue: AWS credentials invalid in Actions
Error: The security token included in the request is invalid
Solution:
- Verify secrets:
Settings>Secrets and variables>Actions - Update
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY - Ensure IAM user has required permissions
Getting Help
-
Check logs
# Terraform logs TF_LOG=DEBUG terraform apply # Ansible logs ansible-playbook -vvv site.yml # GitHub Actions logs # Available in Actions tab of repository -
Review documentation
-
Contact team
- Create issue in GitHub repository
- Contact DevOps team via Slack
Best Practices
- Always use workspaces for different environments
- Store sensitive data in AWS Secrets Manager
- Use remote state backend (S3) for collaboration
- Enable state locking with DynamoDB
- Tag all resources consistently
- Use pre-commit hooks for code quality
- Review Terraform plans before applying
- Test infrastructure changes in dev/staging first
- Document all infrastructure changes in commit messages
- Use version constraints for providers and modules
Last Updated: 2025-11-21 Version: 2.0 Maintainer: DevOps Team