Day 19 - Understanding Terraform Provisioners with AWS EC2 and Nginx
- Get link
- X
- Other Apps
Introduction
For Day 19 of my challenge, I explored Terraform Provisioners using AWS EC2.
In this demo, I used:
- local-exec
- file provisioner
- remote-exec
The goal was to:
- Deploy an EC2 instance
- Copy a shell script into the server
- Install nginx automatically
- Validate the deployment from the browser
This exercise also helped me understand why HashiCorp considers provisioners a “last resort” approach in production environments.
Architecture Diagram
Types of Terraform Provisioners
local-exec
provisioner "local-exec" {
command = "echo ${self.public_ip} >> inventory.txt"
}
file Provisioner
provisioner "file" {
source = "welcome.sh"
destination = "/tmp/welcome.sh"
}
remote-exec
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/welcome.sh",
"sudo /tmp/welcome.sh"
]
}
Terraform Deployment
Initialize Terraform
terraform init
Validate Terraform Configuration
terraform validate
Review Terraform Plan
terraform plan
Deploy Infrastructure
terraform apply
Validation Steps
Validate local-exec
cat inventory.txt
Validate file Provisioner
ls -l /tmp/welcome.sh
Validate remote-exec
sudo systemctl status nginx
curl localhost
Browser Validation
Open:
http://54.82.86.210/
Why Provisioners Are Considered Last Resort
Terraform is designed mainly for infrastructure provisioning.
Provisioners introduce procedural execution into a declarative infrastructure workflow, which can create:
- SSH dependency failures
- Timing issues
- Non-idempotent behavior
- Harder troubleshooting
- More fragile automation
Because of this, HashiCorp recommends alternatives such as:
- user_data
- cloud-init
- Packer
- AWS Systems Manager
- Ansible
This exercise helped me understand the practical difference between infrastructure provisioning and server configuration management.
Key Learnings
- local-exec runs on the local machine
- file provisioner copies files into EC2
- remote-exec executes commands inside EC2 through SSH
- Provisioners execute during resource creation
- Provisioners do not rerun automatically after script changes
- SSH connectivity is critical for remote-exec and file provisioners
- Terraform is best suited for infrastructure provisioning
- user_data and cloud-init are usually better production approaches
Cleanup
terraform destroy
Conclusion
Day 19 provided practical experience with Terraform Provisioners using AWS EC2 and nginx.
I successfully automated:
- EC2 deployment
- Script transfer
- nginx installation
- Web server validation
More importantly, I learned why provisioners should be used carefully and why production environments usually prefer immutable infrastructure and dedicated configuration management tools.
Video Reference
- Get link
- X
- Other Apps
Comments
Post a Comment