
The chef, file, local-exec, puppet, remote-exec, provisioner invokes a script on a remote resource only after it is created. Thus, if you add provisioner code after “terraform apply”, it would not considers as a changes and terraform would flag no resources addition and change.
Thus, How to apply a provisioners changes in resources created. There are 2 method to apply provisioners even after “terraform apply”.
Adding provisioner sections to an existing (already provisioned) aws_instance is not something that terraform notices as a ‘change’, so the provisioner is not run during the next apply. The only way to run the provisioner is to destroy the instance and let terraform create it again.
Method 1 – Destroy and Recreate
Adding provisioner sections to an existing (already provisioned) aws_instance is not something that terraform notices as a ‘change’, so the provisioner is not run during the next apply. The only way to run the provisioner is to destroy the instance and let terraform create it again.
Method 2 – Using null_resource
The solution is to create a resource “null_resource” “nameYouWant” { } and then run your commands inside that. They will run after the initial resources are created:
resource "aws_instance" "consul" { | |
count = 3 | |
ami = "ami-ce5a9fa3" | |
instance_type = "t2.micro" | |
key_name = "ansible_aws" | |
tags { | |
Name = "consul" | |
} | |
} | |
resource "null_resource" "configure-consul-ips" { | |
count = 3 | |
connection { | |
user = "ubuntu" | |
private_key="${file("/home/ubuntu/.ssh/id_rsa")}" | |
agent = true | |
timeout = "3m" | |
} | |
provisioner "remote-exec" { | |
inline = [ | |
"sudo apt-get update", | |
"sudo apt-get install -y curl", | |
"sudo echo '${join("\n", aws_instance.consul.*.private_ip)}' > /home/ubuntu/test.txt" | |
] | |
} | |
} |










I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND