This project demonstrates end-to-end infrastructure automation using Terraform for provisioning and Ansible for configuration management. The setup is designed to run in Spacelift, where the Terraform stack triggers the Ansible stack using hooks and shared context.
- Provisioning Tool: Terraform
- Configuration Management: Ansible
- CI/CD Orchestration: Spacelift
This automation project:
- Provisions EC2 instances in AWS using Terraform.
- Uses Spacelift to trigger the Ansible stack once Terraform is complete.
- Configures each EC2 with required packages using Ansible (e.g.,
htop
,nginx
). - Demonstrates secure SSH communication using passwordless login via mounted SSH keys.
project-root/
├── terraform/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
├── ansible/
│ ├── install_htop.yml
│ └── install_nginx.yml
├── id_rsa # PEM private key used for SSH
├── id_rsa.pub # Public key copied to EC2s
├── README.md
-
Region:
eu-north-1
-
AMI: Ubuntu-based
-
Resources:
- VPC, Subnets (optional)
- EC2 Instances with public IPs
- Key pair name to match SSH key
-
Reads public IPs from Terraform outputs
-
Uses mounted
id_rsa
to SSH into EC2s -
Installs:
htop
on all instancesnginx
on a specific subset
- SSH keypair (
id_rsa
,id_rsa.pub
) is generated manually. id_rsa
is mounted to Spacelift's Ansible stack.- Public key is added to EC2s via user data or
ssh-copy-id
.
To test this entire flow:
- Trigger the Terraform stack from Spacelift.
- Terraform provisions all infrastructure and outputs IPs.
- Spacelift hook triggers the Ansible stack.
- Ansible connects to instances using the mounted private key and runs its playbooks.
-
Valid AWS credentials configured in Spacelift
-
id_rsa
andid_rsa.pub
pair -
Required Spacelift Environment variables:
ANSIBLE_PRIVATE_KEY_FILE
→/mnt/workspace/id_rsa
ANSIBLE_REMOTE_USER
→ubuntu
- Ensure EC2 security group allows port 22 (SSH) and 80 (nginx).
- Use Terraform outputs to pass IPs to Ansible dynamically.
- This is ideal for automating dev/test environments quickly.
- Terraform and Ansible can work seamlessly together with the right trigger mechanism.
- SSH key format matters — use PEM (RSA) for compatibility.
- Spacelift simplifies complex multi-step workflows with stack dependencies and secure file mounts.
- Add provisioning for RDS, S3, or VPC networking.
- Use dynamic inventory scripts instead of static
inventory.ini
. - Monitor instance status using Ansible facts.
- Add CloudWatch or Cost alerts for budgeting.
MIT License
Author: Deepak B Project: Terraform-Ansible Infra Automation Status: Functional and extensible