Monday, 8 April 2019

Run PowerShell from Terraform

Introduction

As great as Terraform is, you will find times when you need to fall back to running other scripts or using other tools. One such tool is PowerShell. We'll do a quick demo on how to run PowerShell scripts from Terraform configurations.

We've got three options and we'll run through each one:
  1. Run PowerShell from Terraform the first time only
  2. Run PowerShell from Terraform every time
  3. Run PowerShell from Terraform on a trigger
To run PowerShell, we'll be using the null_resource in Terraform. You can find out more about it here. Using the null_resource, we'll be calling the local_exec provisioner which specifies that the PowerShell script will be run on the machine running the Terraform configuration. More info on that is here.

All the code for this blog post can be found on GitHub here.

Run PowerShell from Terraform the first time only

In this example, we want to run a PowerShell script only the first time the Terraform configuration runs.

We have a pretty simple PowerShell script which I'll put in a sub folder called helpers.


Our configuration is pretty simple for this one. Basically, we're calling the null_resource and the local_exec provisioner. We then pass in the path to our PowerShell script and the parameters. Note that you need to dot source your PowerShell script by putting a . before it and you also need to surround it in '' in case the path includes a space. 


We can then run terraform init and terraform apply and we see the output below and we can see Terraform ran the PowerShell script (no, you don't need to run terraform plan).




If we then run terraform apply again, we find that it doesn't run the script:


Run PowerShell from Terraform every time

Note that now we're running the same script every time Terraform runs. Terraform is idempotent which means it only makes changes on the first run and each next run doesn't make a change unless it needs to. Make sure your PowerShell scripts are idempotent if you're using this approach.

The PowerShell script is the same as before and it's in a folder called helpers as before. It's not idempotent but as it's just writing out information to the console it's not causing a problem.

As for the Terraform configuration, we're now adding a trigger which causes Terraform to run each time the trigger value changes. To do this, we're assigning a new UUID each time:


Terraform now runs the PowerShell script every time it runs.


Run PowerShell from Terraform on a defined trigger

Rather than using a UUID as the trigger value, we can configure Terraform to run the PowerShell script only when a particular value changes. In this example, we'll only run the PowerShell script if the value of TriggerValue changes. This can be any output or variable in Terraform e.g. you could specify that only set IP restrictions if the list of IP restrictions changes. 

Again, our PowerShell script is the same as before in the helpers folder.

To demonstrate this, we can add a variable TriggerValue and specify this in a variables.tf file. Terraform will expect us to pass this variable on the command line or via a TFVARS file.


Our Terraform configuration now needs to look like this. See that the trigger option is set to our TriggerValue variable.


When we run our configuration the first time using terraform apply, we need to specify the value for the variable (terraform apply -var TriggerValue=100) and we see that the script runs:



If we run it again, we can see that Terraform has nothing to do and the script doesn't run:


If we change the value of TriggerValue and run it again, Terraform now runs our PowerShell script:




Conclusion

In this article, we went through how you can run PowerShell scripts from Terraform either running every time, on a trigger or on the first time only.

85 comments:

  1. I read your blog it is very good and you are doing a great job for helping others. I really appreciate what content you are giving to your viewers. Content is good and powerful people should come And read this article.Read Now 

    ReplyDelete
  2. Thanks for sharing the posts so informative and understandable

    Sanjary Academy provide pressure vessel design,quality management system course, piping design course, qa/qc course and document controller course.
    Welding Inspector Course
    Safety officer course
    Quality Management Course
    Quality Management Course in India

    ReplyDelete
  3. Great blog posts informative I liked it

    Piping Design Engineering course is a job oriented diploma course. Sanjary academy provide best piping design course in Hyderabad, India with job assistance.
    best Piping Design Course
    piping design course with placement
    pipe stress analysis course in hyderabad
    piping engineering course in hyderabad
    piping stress analysis course
    best Piping Design institute
    best institute of Piping Design Course in India

    ReplyDelete
  4. I have read your article!! it is very instructive and valuable to me.
    Docker Online Training
    Kubernetes Online Training

    ReplyDelete
  5. kudos, good one and very useful. Saved me a lot of running them via separate tasks outside of TF.

    ReplyDelete
  6. Wow! this is Amazing! Do you know your hidden name meaning ? Click here to find your hidden name meaning

    ReplyDelete
    Replies
    1. Hi, this is such a great article.
      Think you just about covered everything on blog commenting,
      I’ll bookmark your site and come back to read your
      other articles!
      Printer
      Bitcoin
      Android
      Pertanian
      Robotics

      Delete
  7. So, you can understand, it is an advanced level of TV viewing experience. If you are looking for a kind of streaming device that can offer you to watch a large number of TV Programs or movies for the sake of entertainment. Enjoy this amazing streaming device that opens a world of entertainment for you!

    Roku Activation Link
    Setup Roku Stick On TV
    Activate hulu On Roku Device
    connect roku to wireless network
    roku tv activation link

    ReplyDelete
  8. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.
    Matsya University BCOM 1st, 2nd & Final Year TimeTable 2020

    ReplyDelete

  9. So, you can understand, it is an advanced level of TV viewing experience. If you are looking for a kind of streaming device that can offer you to watch a large number of TV Programs or movies for the sake of entertainment. Enjoy this amazing streaming device that opens a world of entertainment for you!

    roku help number
    roku phone number for help
    roku help phone number
    Roku com customer service
    Roku Activation Link
    Setup Roku Stick On TV
    Activate hulu On Roku Device
    how do i connect my roku to my wireless
    roku tv activation link

    ReplyDelete
  10. Nice Article, Thanks for sharing it.
    Want to Cancel Silver Singles Account? In case the cancellation request raised by you through the website is not processed, please contact the customer care to complete the cancellation.


    Silver Singles customer service Phone number
    SilverSingles Com Login Problem Silver Singles Customer
    support phone number for silver dating
    silvers singles customer service Care
    SilverSingles LogIn Problem
    Silver Singles Forgotten Password
    Silver Singles Helpline

    ReplyDelete
  11. Ensure that whichever maid services in Delhi you are hiring are reliable, particularly if you are leaving kids behind using them. To be able to ensure exactly the same, you can inquire further about their employee experiences, work record alongside some recommendations.

    Housekeeping Services in Delhi
    Babysitting Services in Delhi

    ReplyDelete
  12. Want to Increase website Organic traffic? Get affordable SEO Services in India @Iogoos Solution, We offer best SEO Services at your budget. So book Now!

    Website Designing Services
    Shopify Development Services
    Digital Marketing Services in India

    ReplyDelete
  13. SEO Company in India ensures and gives guarantee to drive traffic and put to them puts forth the best possible impression. Core SEO Services is the best & affordable SEO Company in India to attain those goals. best SEO Services in India

    ReplyDelete

  14. You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! DevOps Training in Chennai | DevOps Training in anna nagar | DevOps Training in omr | DevOps Training in porur | DevOps Training in tambaram | DevOps Training in velachery

    ReplyDelete
  15. Thank you for the post, it is exactly what I am looking for.
    God bless you and have a great day

    ReplyDelete
  16. This is useful but I think you have a typo:

    triggers {
    trigger = "${uuid()}"
    }

    Will throw an error (at least in tf12 anyway) .
    It should read:

    triggers = {
    trigger = "${uuid()}"
    }

    As per the documentation.

    ReplyDelete
  17. Need a Debt Loan To Pay Off Bills?
    Take control of your debt today
    Available Now Business Expansion Loan Offer?
    Do you need a loan to pay off Bills?
    Do you need a loan?
    Do you need Personal Loan?
    Business Expansion Loan?
    Business Start-up, Education,
    Debt Consolidation Loan
    Hard Money Loans
    Loan for any thing ?
    We offer loan at low interest rate of 3%
    Loan with no credit check,
    Email us: financialserviceoffer876@gmail.com
    Call or add us on what's app +918929509036

    ReplyDelete
  18. Hi
    I visited your blog you have shared amazing information, i really like the information provided by you, You have done a great work. I hope you will share some more information regarding full movies online. I appreciate your work.
    Thanks
    OpenStack Training in Bangalore

    ReplyDelete
  19. Great content & Thanks for sharing ith oflox. Do you now What Is Parked Domain

    ReplyDelete
  20. Hi
    I visited your blog you have shared amazing information, i really like the information provided by you, You have done a great work. I hope you will share some more information regarding full movies online. I appreciate your work.
    Thanks
    Online Angularjs Training

    ReplyDelete
  21. Thank you for your post, myself very happy to read it because it can give me more insight, thanks.. Visit my blog..thankyou
    Sbobet369
    Joker388 Slot, Slot88, Daftar Judi Online, Joker888

    ReplyDelete
  22. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website


    Joker388
    Sbobet99
    Sbobet888
    Joker388
    Slot88 Bola

    ReplyDelete
  23. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.

    Daftar S128 Net
    Sbobet1388
    Sbowin
    http://139.59.252.210/
    Sbobet365

    Thank you for this article.
    Have a nice day!! ^_^

    ReplyDelete
  24. Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info
    Sbobet365
    Slot777
    Bola888
    Judi Slot Online
    Sbobet168

    ReplyDelete
  25. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.

    Sbobet777
    Slot777
    Sbobetcc
    Joker123
    Bet88 Slot

    ReplyDelete
  26. Thank you for your post, I look for such article along time. myself very happy to read it because it can give me more insight, thanks.. Visit my blog..thankyou
    Bet88 Slot, Joker388, Sbowin Com, Sbobet365, Live22 Slot

    ReplyDelete
  27. My name is Samuel, I was in love with my wife and we were married for eight years with a son his is Liam,I loved my wife so much she had access to all my bank account and even my cash app which my accountant agreed to and said it was a great idea, then it took my wife and my accountant two month to get hold of all my properties,all accounts but I had a cash app which they knew nothing about,I was thrown out of my own house was sleeping in a hotel for weeks she also took possession of my son could only see him once a week then I found out she was in love with my accountant all these while so I went online and I came across a private investigator who help me get all my properties and my accounts back even my company back how he did these I don’t know but I gave all the information he asked for and followed all his instructions and now I’m happy my life’s better now.
    Thanks to premiumhackservices@gmail.com
    I just said I should share my own story here
    Thank you

    ReplyDelete
  28. I feel that is one of the most vital info for me. And
    i am satisfied reading your article. However should remark on some general
    things, The site taste is ideal, the articles is really nice : D.
    Just right activity, cheers

    Buat Akun Sbobet88
    Slot Joker123
    Sbobet88 Agen Judi Online Terpercaya
    Daftar Akun Sbobet88 Mobile
    Login Sbobet88 Mobile Indonesia

    ReplyDelete
  29. I simply couldn’t go away your web site before suggesting that I extremely loved the usual
    info an individual provide to your guests? Is gonna be
    again regularly to check out new posts

    Cara Login Sbobet777 Indonesia
    Casino88
    Judi Dingdong Online
    Joker88 Slot
    Sbobet128

    ReplyDelete
  30. Just wish to say your article is as surprising.
    The clarity in your post is just excellent and i can assume you’re an expert on this subject.
    Fine with your permission allow me to grab your feed to keep updated with
    forthcoming post. Thanks a million and please carry on the enjoyable
    work.

    Judi Dadu Online
    Sbobet168
    Sbobet303
    Sbobet188
    Sbobet365

    ReplyDelete
  31. It’s really a great and helpful piece of information.
    I’m satisfied that you simply shared this useful info
    with us. Please keep us informed like this. Thank you for
    sharing.

    Sbobet99
    Sbobet338
    Slot88
    Login Sbobet777 Slot
    Login Bet88 Slot

    ReplyDelete
  32. Someone essentially lend a hand to make critically
    posts I would state. That is the very first time I frequented your website page and up to now?
    I surprised with the research you made to create this particular submit
    incredible. Wonderful task!

    Daftar Sbowin Wap Mobile
    Daftar Sbobet99 Slot
    Daftar Sbobetcc Slot
    Daftar Bola888 Slot
    Daftar Sbobet Slot

    ReplyDelete
  33. Oh my goodness! Impressive article dude! Thank you, However I am going through
    difficulties with your RSS. I don’t know the reason why I am
    unable to join it. Is there anybody else having the same RSS problems?
    Anyone that knows the solution will you kindly respond?
    Thanks!!

    Daftar S128 Net
    Daftar Sbobet88 Slot
    Daftar Judi Slot Online
    Dewa Hoki
    Dewahoki

    ReplyDelete
  34. Hey there! I know this is kind of off topic but I was wondering
    which blog platform are you using for this site?
    I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at alternatives
    for another platform. I would be fantastic if you could point me in the direction of a good platform.

    Slot777
    Poker77
    Daftar Sky777 Slot
    Mega888 Slot
    Mega77 Slot

    ReplyDelete
  35. Howdy! I could have sworn I’ve visited this blog before but after browsing through many of the articles I realized it’s new to me. Anyways, I’m certainly delighted I discovered it and I’ll be book-marking it and checking back frequently!

    Login Wap Winsbotop Com
    Sbobet Mobile Login
    Login Sbotop Euro
    Daftar Joker388 Slot Mobile
    Digmaan Live

    ReplyDelete
  36. Nice post. I was checking constantly this blog and I am impressed!
    Extremely useful information specifically the last part �� I care for such information a lot.
    I was seeking this certain info for a very long time.
    Thank you and good luck.

    Live22 Slot
    Dewahoki Slot 777
    Game Slot 777
    Joker Slot 388
    Permainan Menembak Ikan

    ReplyDelete
  37. Hi, i tһink that і saw you visited my website thus і
    came to “return the favor”.I am attempting to find things to impгove my website!I ѕuppoѕe its ok to use a few
    of your iԁeas!!

    Agen 1388 Bet
    Situs Joker1999
    Daftar Fa Fa Slot
    Download Apklive22
    Royal Slot 777

    ReplyDelete
  38. Thank you for your post, myself very happy to read it because it can give me more insight, thanks.. Visit my blog..thankyou

    Slot88
    Megaslot888
    Slot188

    ReplyDelete
  39. I got this web blog from my buddy who told me concerning this web blog and at the moment this time I am browsing this web page and reading very informative articles here.

    Joker388
    Slot99
    Judi Slot88
    Slot777

    ReplyDelete
  40. "Oh my goodness! Impressive article dude! Thank you, However I am going through difficulties with your RSS. I don’t know the reason why I am unable to join it. Is there anybody else having the same RSS problems?
    Anyone that knows the solution will you kindly respond? Thanks!!"

    Sbobet88 slot
    Naga777 Slot
    Joker88 Slot
    Joker gaming

    ReplyDelete
  41. Nice post. I was checking constantly this blog and I am impressed! Extremely useful information specifically the last part �� I care for such information a lot. I was seeking this certain info for a very long time. Thank you and good luck.

    Hoki Slot
    Daftar Sbobet88
    Asiabet88

    ReplyDelete
  42. Thank you for your post, I look for such article along time. myself very happy to read it because it can give me more insight, thanks.. Visit my blog..thankyou

    Joker slot
    Slot99
    Spbo Macau
    Joker777

    ReplyDelete
  43. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.

    Wap Spbo
    Bola338
    Slot Qq
    Qqslot88

    ReplyDelete
  44. It’s really a great and helpful piece of information. I’m satisfied that you simply shared this useful info with us. Please keep us informed like this. Thank you for sharing.

    Idn77
    Sultan Slot

    ReplyDelete
  45. This is a very nice article. Thanks for sharing

    Visit our site https://vidhema.com/technologies-mobile-android.html

    ReplyDelete
  46. Thanks for sharing this excellent post, I am going to share it as an external reference link in a post I am writing on Guest Posting. mpo slot

    ReplyDelete
  47. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website mulesoft online training
    best mulesoft online training
    top mulesoft online training

    ReplyDelete
  48. Thanks for wordpress posting this, I ave been looking for this info for the whilst! Your blog site is wonderful.

    ReplyDelete
  49. "Oh my goodness! Impressive article dude! Thank you, However I am going through difficulties with your RSS. I don’t know the reason why I am unable to join it. Is there anybody else having the same RSS problems?
    Anyone that knows the solution will you kindly respond? Thanks!!"

    Mega Slot
    Judi Slot Online
    Joker123
    Agen Slot Pulsa

    ReplyDelete
  50. Nice post. I was checking constantly this blog and I am impressed! Extremely useful information specifically the last part �� I care for such information a lot. I was seeking this certain info for a very long time. Thank you and good luck.

    388casino
    Mega77 Slot

    ReplyDelete
  51. Thanks for sharing your thoughts. I really appreciate your efforts and I will be
    waiting for your next post thank you once again.

    Big Slot77
    Fa Fa Slot
    Live22 Slot
    Poker77
    Domino77

    ReplyDelete
  52. I was more than happy to find this site. I need
    to to thank you for ones time just for this fantastic read!!
    I definitely really liked every little bit of it and I have you saved as a favorite to look at new things on your site. Thankyou for sharing.

    Bandar888
    88Sbobet
    Mpo88
    Mpo Slot
    Mpo303

    ReplyDelete
  53. This paragraph is genuinely a nice one it helps new net users, who are wishing
    in favor of blogging. Thankyou for sharing.

    Mpo338
    Megajoker88
    Sbobetasia888
    Mpobola
    Hoki99

    ReplyDelete
  54. Every weekend i used to pay a visit this web page, because i want enjoyment, since
    this this site conations actually fastidious funny
    information too. Thankyou for sharing.

    Mpo66
    Mpo33
    Mpo10
    Mpo18
    Mpo81

    ReplyDelete
  55. Asking questions are genuinely good thing if you are not understanding
    anything completely, except this post gives nice understanding yet. Thankyou for sharing.

    Mpo5000
    Mpo005
    Mpo222 Slot
    Joker88
    Mpo168

    ReplyDelete
  56. Heya i'm for the first time here. I found this board
    and I find It truly useful & it helped me out a lot.
    I hope to give something back and help others like you helped
    Thankyou for sharing.

    Mpo12 Slot
    Mpo77 Slot
    Mpo7 Slot
    Mpo99
    Raja88 Slot

    ReplyDelete
  57. Nice post. I was checking constantly this blog and I am impressed! Extremely useful information specifically the last part �� I care for such information a lot. I was seeking this certain info for a very long time. Thank you and good luck.

    Bigslot88

    ReplyDelete
  58. Good article! We will be linking to this particularly great content on our website.
    Keep up the great writing.
    Thankyou for sharing.

    Sbowin
    Judi Dingdong Online
    Dewa Slot
    Mas88 Slot
    Slot777

    ReplyDelete
  59. Everything is very open with a precise explanation of the challenges.
    It was truly informative. Your website is extremely helpful.
    Thanks for sharing!

    Dewa Hoki Slot
    Sbobet Com
    Bet88 Online
    Bet88
    Habanero Slot

    ReplyDelete
  60. I really like your blog.. very nice colors & theme.
    Did you create this website yourself or did you hire someone to do it for you?
    Plz reply as I'm looking to construct my own blog and would like to
    know where u got this from. thank you

    Bola188
    Bola365
    Joker88 Live
    Habanero Slot
    Konami Slot

    ReplyDelete
  61. I got this web blog from my buddy who told me concerning this web blog and at the moment this time I am browsing this web page and reading very informative articles here. Thankyou for sharing.

    Sbobet4d
    Megajoker88
    Joker88
    Lucky Slot 777
    Mpo3 Slot

    ReplyDelete
  62. Oh my goodness! Impressive article dude! Thank you, However I am going through difficulties with your RSS. I don’t know the reason why I am unable to join it. Is there anybody else having the same RSS problems?
    Anyone that knows the solution will you kindly respond? Thanks!!

    Mpo123 Slot
    Joker88 Slot
    Slot Joker88
    Raja Slot88
    Joker88

    ReplyDelete
  63. I got this web blog from my buddy who told me concerning this web blog and at the moment this time I am browsing this web page and reading very informative articles here.

    Joker Slot
    Slot Deposit Pulsa

    ReplyDelete
  64. Thank you for your post, myself very happy to read it because it can give me more insight, thanks.. Visit my blog..thankyou

    Big Slot777
    Hoki Slot

    ReplyDelete
  65. Nice post. I was checking constantly this blog and I am impressed! Extremely useful information specifically the last part �� I care for such information a lot. I was seeking this certain info for a very long time. Thank you and good luck.

    Sbobet88 Slot
    Bet88

    ReplyDelete
  66. Textrics Topic Modeling Algorithms work very efficiently. It collects textual data accurately. AI-based Text Analysis Tool is ultimate for analyzing text sentiment. Try it today. Signup for free.
    offensive language detection
    Text tagging
    intent analysis
    named entity extraction

    ReplyDelete