Infrastructure Automation with Terraform + Ansible
This project demonstrates fully automated infrastructure provisioning and configuration for a full-stack Spring Boot web application using:
- Terraform: to provision AWS infrastructure (VPC, Subnets, EC2 instances, Security Groups, SSH Key, etc.)
- Ansible: to install and configure required software on provisioned instances (Tomcat, Memcached, RabbitMQ, MongoDB)
- Shell Scripting: to automated the whole process just by single command .
Layer | Technology |
---|---|
Infrastructure Provisioning | Terraform |
Infrastructure Configuration | Ansible |
Cloud Provider | AWS |
Application Server | Apache Tomcat |
Database | MongoDB |
Caching | Memcached |
Messaging Queue | RabbitMQ |
Make sure you have following tools installed in your machine:
-
Terraform
-
Ansible
-
AWS CLI
-
Python Boto3 Module (for Ansible AWS Plugin)
pip install boto3 botocore
-
AWS Account with Access Key and Secret Key configured:
aws configure
-
VPC
-
Public Subnet
-
Private Subnet
-
Security Groups
-
EC2 Instances:
- Public EC2 (Tomcat Server)
- Private EC2 (Memcached)
- Private EC2 (RabbitMQ)
- Private EC2 (MongoDB)
-
SSH Key Pair
project-root/
│
├── terraform/
│ ├── # All Terraform code here
|
├── ansible/
│ ├── # All Ansible code here
|
└── project.pem (auto-generated by Terraform)
git clone https://github.com/Coding4Deep/Terraform-Ansible-Project.git
cd Terraform-Ansible-Project
* Update `provider.tf` with your AWS region.
* Replace `ami-xxxxxxxx` with valid Ubuntu AMI ID from your region.
terraform init
terraform plan
terraform apply
✅ Terraform will create the entire AWS infrastructure.
✅ After provisioning is done, Terraform automatically triggers Ansible via null_resource
to configure all servers.
Note: You may be prompted for approval during
terraform apply
.
Ensure you have valid AWS credentials locally (~/.aws/credentials
).
The file ansible/inventory/aws_ec2.yaml
uses AWS EC2 dynamic inventory plugin to auto-discover EC2 instances using tags.
You can test Ansible inventory:
cd ../ansible/
ansible-inventory --graph
You should see:
@all:
|--@aws_ec2:
|--@role_tomcat:
| |--springboot-public
|--@role_memcached:
| |--springboot-memcached
|--@role_rabbitmq:
| |--springboot-rabbitmq
|--@role_mongo:
| |--springboot-mongo
No IP addresses needed. Fully dynamic!
If you want to re-run Ansible manually anytime:
ansible-playbook playbooks/site.yml
This will execute all roles (tomcat, memcached, rabbitmq, mongo).
Component | Action |
---|---|
Terraform | Provisions VPC, Subnets, EC2 Instances, Security Groups, SSH Key |
Ansible | Dynamically discovers EC2 instances via AWS tags |
Tomcat Playbook | Installs Java, Downloads and Configures Apache Tomcat |
Memcached Playbook | Installs and Starts Memcached Service |
RabbitMQ Playbook | Installs and Starts RabbitMQ Server |
MongoDB Playbook | Adds official MongoDB repo, Installs MongoDB, Starts MongoDB |
- Use Bastion Host for secure SSH into private instances
- Jenkins CI/CD integration for full automation pipeline
- Monitoring with CloudWatch
- HTTPS with ACM and Load Balancer
Created by [Deepak Sagar]. Open-source project to help anyone learn cloud provisioning and automation.
This project is open-source and available under MIT License.