This project demonstrates how to use Terraform to provision AWS infrastructure and Ansible to configure the provisioned instances.
ansible/: Playbooks, inventories, and configurationterraform/: Infrastructure files and modules
Before using this project:
- Install
botofor dynamic inventory with AWS EC2:$ pip install boto $ chmod +x ansible/dynamic_inventory.sh - Set up your AWS credentials (e.g., using environment variables or AWS CLI configuration)
- Set variables in
terraform/terraform.tfvars(copy fromterraform.tfvars.example):pub_key_pathprivate_key_pathkey_name
- Initialize Terraform:
$ cd terraform && terraform init - Plan the infrastructure:
$ terraform plan - Apply the Terraform configuration:
$ terraform apply - Run Ansible playbooks:
$ cd ../ansible && ansible-playbook -i dynamic_inventory.sh site.yml - Destroy the infrastructure:
$ terraform destroy
main.tf: Defines the main infrastructure (AWS provider, modules)variables.tf: Declares input variablesoutputs.tf: Specifies output valuesdata.tf: Defines data sources (e.g., AMI lookup)backend.tf.example: Example backend configuration for remote state
base: Defines base security groupkey_pair: Manages SSH key pairweb: Provisions web serversdb: Provisions database serversvpc: Creates and configures the VPCalb: Sets up Application Load Balancerasg: Configures Auto Scaling Grouprds: Provisions RDS databases3: Creates and configures S3 bucketcloudfront: Sets up CloudFront distributionroute53: Manages Route53 DNS recordsiam: Configures IAM roles and policieskafka: Provisions an Amazon MSK (Managed Streaming for Kafka) clustersqs: Creates an Amazon SQS (Simple Queue Service) queue
Each module has its own main.tf, variables.tf, and outputs.tf files.
ansible.cfg: Ansible configuration filesite.yml: Main playbook that includes other playbooksdynamic_inventory.sh: Script that retrieves tfstate from S3 to generate inventoryplaybooks/:docker.yml: Playbook for Docker installationmysql.yml: Playbook for MySQL installationapache.yml: Playbook for Apache installationportainer.yml: Playbook for Portainer installationtailscale.yml: Playbook for Tailscale installation
This project sets up the following AWS resources:
- VPC and security groups
- EC2 instances for web and database servers
- SSH key pair for instance access
Ansible is used to configure the provisioned instances:
- Apache: Installs and configures Apache
- Mysql: Installs and configures MySQL
- Docker: Installs Docker and sets up containers
- Portainer: Installs Portainer for container management
- Tailscale: Installs Tailscale for secure networking
- Ensure your AWS credentials are properly configured
- Review and adjust security group rules as needed
- Customise Ansible playbooks to fit your specific requirements
MIT licensed by tooniez