Terraform Null Resource¶
Step-01: Introduction¶
- Understand about Null Provider
- Understand about Null Resource
- Understand about Time Provider
- Usecase: Force a resource to update based on a changed null_resource
- Create
time_sleepresource to wait for 90 seconds after EC2 Instance creation - Create Null resource with required provisioners
- File Provisioner: Copy apps/app1 folder to /tmp
- Remote Exec Provisioner: Copy app1 folder from /tmp to /var/www/htnl
- Over the process we will learn about
- null_resource
- time_sleep resource
- We will also learn how to Force a resource to update based on a changed null_resource using
timestamp functionandtriggersinnull_resource
Step-02: Define null provider in Terraform Settings Block¶
- Update null provider info listed below in c1-versions.tf
Step-03: Define Time Provider in Terraform Settings Block¶
- Update time provider info listed below in c1-versions.tf
Step-04: Create / Review the c4-ec2-instance.tf terraform configuration¶
Step-04-01: Create Time Sleep Resource¶
- This resource will wait for 90 seconds after EC2 Instance creation.
- This wait time will give EC2 Instance to provision the Apache Webserver and create all its relevant folders
- Primarily if we want to copy static content we need Apache webserver static folder
/var/www/html
Step-04-02: Create Null Resource¶
- Create Null resource with
triggerswithtimestamp()function which will trigger for everyterraform apply - This
Null Resourcewill help us to sync the static content from our local folder to EC2 Instnace as and when required. - Also only changes will applied using only
null_resourcewhenterraform applyis run. In other words, when static content changes, how will we sync those changes to EC2 Instance using terraform - This is one simple solution. - Primarily the focus here is to learn the following
- null_resource
- null_resource trigger
- How trigger works based on timestamp() function ?
- Provisioners in Null Resource
# Sync App1 Static Content to Webserver using Provisioners resource "null_resource" "sync_app1_static" { depends_on = [ time_sleep.wait_90_seconds ] triggers = { always-update = timestamp() } # Connection Block for Provisioners to connect to EC2 Instance connection { type = "ssh" host = aws_instance.my-ec2-vm.public_ip user = "ec2-user" password = "" private_key = file("private-key/terraform-key.pem") } # Copies the app1 folder to /tmp provisioner "file" { source = "apps/app1" destination = "/tmp" } # Copies the /tmp/app1 folder to Apache Webserver /var/www/html directory provisioner "remote-exec" { inline = [ "sudo cp -r /tmp/app1 /var/www/html" ] } }
Step-05: Execute Terraform Commands¶
# Terraform Initialize
terraform init
# Terraform Validate
terraform validate
# Terraform Format
terraform fmt
# Terraform Plan
terraform plan
# Terraform Apply
terraform apply -auto-approve
# Verify
ssh -i private-key/terraform-key.pem ec2-user@<PUBLIC-IP>
ls -lrt /tmp
ls -lrt /tmp/app1
ls -lrt /var/www/html
ls -lrt /var/www/html/app1
http://<public-ip>/app1/file1.html
http://<public-ip>/app1/file2.html
Step-06: Create new file locally in app1 folder¶
- Create a new file named
file3.html - Also updated
file1.htmlwith some additional info - file3.html
- file1.html
Step-07: Execute Terraform plan and apply commands¶
# Terraform Plan
terraform plan
Observation: You should see changes for "null_resource.sync_app1_static" because trigger will have new timestamp when you fired the terraform plan command
# Terraform Apply
terraform apply -auto-approve
# Verify
chmod 400 private-key/terraform-key.pem
ssh -i private-key/terraform-key.pem ec2-user@<PUBLIC-IP>
ls -lrt /tmp
ls -lrt /tmp/app1
ls -lrt /var/www/html
ls -lrt /var/www/html/app1
http://<public-ip>/app1/file1.html
http://<public-ip>/app1/file3.html
Step-08: Clean-Up Resources & local working directory¶
# Terraform Destroy
terraform destroy -auto-approve
# Delete Terraform files
rm -rf .terraform*
rm -rf terraform.tfstate*
References¶
🎉 New Course
Ultimate DevOps Real-World Project Implementation on AWS
$15.99
$84.99
81% OFF
MARCH2026
Enroll Now on Udemy →
🎉 Offer