{"id":7535,"date":"2019-10-30T06:13:03","date_gmt":"2019-10-30T06:13:03","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=7535"},"modified":"2021-11-15T11:25:07","modified_gmt":"2021-11-15T11:25:07","slug":"how-to-run-provisioners-code-after-resources-is-created-in-terraform","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-run-provisioners-code-after-resources-is-created-in-terraform\/","title":{"rendered":"Terraform: run code after resources is created using using remote-exec provisioners"},"content":{"rendered":"\n<p>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 \u201cterraform apply\u201d, it would not considers as a changes and terraform would flag no resources addition and change.<\/p>\n\n\n\n<p>Thus, How to apply a provisioners changes in resources created. There are 2 method to apply provisioners even after \u201cterraform apply\u201d. <\/p>\n\n\n\n<p>Adding provisioner sections to an existing (already provisioned) aws_instance is not something that terraform notices as a \u2018change\u2019, 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.<\/p>\n\n\n\n<p>Method 1 \u2013 Destroy and Recreate<\/p>\n\n\n\n<p>Adding provisioner sections to an existing (already provisioned) aws_instance is not something that terraform notices as a \u2018change\u2019, 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.<\/p>\n\n\n\n<p>Method 2 \u2013 Using null_resource<\/p>\n\n\n\n<p>The solution is to create a resource \u201cnull_resource\u201d \u201cnameYouWant\u201d { } and then run your commands inside that. They will run after the initial resources are created:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/fc3adc5dee3fe30312777ba89c5ad638.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_67275\"><iframe loading=\"lazy\"  id=\"_ytid_74866\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_67275\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>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 \u201cterraform apply\u201d, it would&#8230; <\/p>\n","protected":false},"author":1,"featured_media":7791,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5129],"tags":[1003,5449,5498,5130],"class_list":["post-7535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-terraform","tag-code","tag-devopsschool","tag-provisioners","tag-terraform"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=7535"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7535\/revisions"}],"predecessor-version":[{"id":25339,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7535\/revisions\/25339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7791"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=7535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=7535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=7535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}