This repository contains Infrastructure as Code (IaC) for deploying and managing a web infrastructure on Oracle Cloud Infrastructure (OCI) using Terraform and Ansible.
The infrastructure consists of:
- Virtual Cloud Network (VCN) with public subnet
- Two web servers (web-01 and web-02) running on VM.Standard.E2.1.Micro instances
- MySQL database instance configured for WordPress
- Ansible configuration for automated instance provisioning
graph TB
Internet --> IG[Internet Gateway]
IG --> PublicSubnet
subgraph VCN[Virtual Cloud Network]
PublicSubnet --> web1[Web Server 1]
PublicSubnet --> web2[Web Server 2]
PublicSubnet --> MySQL[MySQL Database]
end
-
OCI Setup
- An Oracle Cloud Infrastructure account
- OCI CLI configured with required credentials
- User with appropriate permissions
- API signing key and fingerprint
-
Required Software
- Terraform (>= 1.0.0)
- Ansible (>= 2.9)
- Git
- VCN with customizable CIDR
- Internet Gateway for public access
- Public subnet for web servers
- Security lists for network traffic control
- Two VM.Standard.E2.1.Micro instances
- Ubuntu-based OS
- Public IP addresses
- Configured with monitoring and management plugins
- MySQL DB System (Free Tier)
- 50GB storage
- Automated backup enabled
- Delete protection enabled
- Secure connections with system-generated certificates
- Ansible playbooks for automated provisioning
- Common role for basic system setup
- Package installation and updates
- User management and SSH key setup
- Secure credential management using Ansible Vault
-
Create a vault password file:
echo "your-secure-password" > ansible/.vault_pass chmod 600 ansible/.vault_pass
-
Store sensitive data:
- SSH keys
- Database credentials
- User passwords
All sensitive data is managed in
ansible/group_vars/all/vault.yml -
Encrypt the vault file:
cd ansible ansible-vault encrypt group_vars/all/vault.yml -
To edit vault contents:
ansible-vault edit group_vars/all/vault.yml
Note: Never commit .vault_pass to version control. Add it to .gitignore.
-
Clone the Repository
git clone <repository-url> cd oci-tf
-
Configure Variables
cp terraform.tfvars.example terraform.tfvars # Edit terraform.tfvars with your OCI credentials and preferences -
Initialize Terraform
terraform init
-
Deploy Infrastructure
terraform plan # Review the changes terraform apply # Deploy the infrastructure
-
Configure Ansible
cd ansible # Update hosts file with instance IP addresses ansible-playbook -i hosts provision.yml
provider.tf: OCI provider configurationvcn.tf: Network configurationcompute.tf: Web server instances configurationmysql.tf: Database configurationansible/: Configuration management filesprovision.yml: Main playbookroles/: Task definitions for server setup
- Encrypted transit for instance communication
- Security lists controlling inbound/outbound traffic
- Secure database connections
- SSH key-based authentication
- Regular system updates via Ansible
- Use
terraform planto review infrastructure changes - Regular
terraform applyfor infrastructure updates - Ansible playbooks for system configuration updates
- Monitor instances through OCI Console
To remove all resources:
terraform destroyNote: This will permanently delete all resources. Ensure data is backed up if needed.