Creating Terraform Scripts from existing resources in Azure Government

Lately I’ve been doing a lot of work with TerraForm lately, and one of the questions I’ve gotten a lot is the ability to create terraform scripts based on existing resources.

So the use case is the following: You are working on projects, or part of an organization that has a lot of resources in Azure, and you want to start using terraform for a variety of reasons:

  • Being able to iterating in your infrastructure
  • Consistency of environment management
  • Code History of changes

The good new is there is a tool for that. The tool can be found here on github along with a list of pre-requisites.  I’ve used this tool in Azure Commercial and have been really happy with the results. I wanted to use this with Azure Commercial.

NOTE => The Pre-reqs are listed on the az2tf tool, but one they didn’t list I needed to install was jq, using “apt-get install jq”.

Next we need to configure our environment for running terraform.  For me, I ran this using the environment I had configured for Terraform.  In the Git repo, there is a PC Setup document that walks you through how to configure your environment with VS code and Terraform.  I then was able to clone the git repo, and execute the az2tf tool using a Ubuntu subsystem on my Windows 10 machine. 

Now, the tool, az2f, was built to work with azure commercial, and there is one change that has to be made for it to leverage azure government 

Once you have the environment created, and the pre-requisites are all present, you can open a “Terminal” window in vscode, and connect to Azure Government. 

In the ./scripts/resources.sh and ./scripts/resources2.sh files, you will find the following on line 9:

ris=`printf “curl -s  -X GET -H \”Authorization: Bearer %s\” -H \”Content-Type: application/json\” https://management.azure.com/subscriptions/%s/resources?api-version=2017-05-10” $bt $sub`

Please change this line to the following:

ris=`printf “curl -s  -X GET -H \”Authorization: Bearer %s\” -H \”Content-Type: application/json\” https://management.usgovcloudapi.net/subscriptions/%s/resources?api-version=2017-05-10” $bt $sub`

You can then run the “az2tf” tool by running the following command in the terminal:

./az2tf.sh -s {Subscription ID} -g {Resource Group Name}

This will generate the script, and you will see a new folder created in the structure marked “tf.{Subscription ID}” and inside of it will be all configuration steps to setup the environment.

5 thoughts on “Creating Terraform Scripts from existing resources in Azure Government”

  1. hello there and thank you for your information – I have certainly picked up something new from right here.
    I did however expertise several technical issues using this website, as I experienced to reload the site many times previous to I
    could get it to load properly. I had been wondering if your web hosting is OK?
    Not that I am complaining, but sluggish loading instances times
    will very frequently affect your placement in google and could damage your quality score
    if ads and marketing with Adwords. Well I am adding this RSS to my email and can look out for a lot more of your respective intriguing
    content. Make sure you update this again very soon.

    1. Thanks we did have some hosting problems, and issues with a social site marking my blog as spam which caused some issues, but that’s all been fixed.

  2. Hey there great blog! Does running a blog similar to this require a
    large amount of work? I’ve absolutely no knowledge
    of programming but I had been hoping to start my own blog soon. Anyway, if you
    have any recommendations or techniques for new blog owners please share.
    I understand this is off topic nevertheless I simply wanted to ask.
    Thanks!

    1. Thanks, for me it was just a matter of sitting down and starting to write, and taking the things I’m doing normally and figuring out a way to make them repeatable. Like if you are writing to a colleague an email to explain a topic, make it more generic and post a blog entry and send the link. Its all about making it more distributed and consumable by a broader audience. And then just creating a blog and writing. I also did John Somnez’s blog course and that helped.

  3. Attractive section of content. I just stumbled upon your web site and in accession capital
    to assert that I get in fact enjoyed account your blog posts.
    Any way I’ll be subscribing to your feeds and even I achievement you access consistently
    quickly.

Leave a Reply

Your email address will not be published. Required fields are marked *