{"id":1251,"date":"2020-08-25T12:51:10","date_gmt":"2020-08-25T19:51:10","guid":{"rendered":"http:\/\/blog.nillsf.com\/?p=1251"},"modified":"2020-08-25T12:51:13","modified_gmt":"2020-08-25T19:51:13","slug":"how-to-use-terraform-output-in-a-github-action","status":"publish","type":"post","link":"https:\/\/nillsf.com\/index.php\/2020\/08\/25\/how-to-use-terraform-output-in-a-github-action\/","title":{"rendered":"How to use &#8216;terraform output&#8217; in a GitHub action"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Recently, I was building a GitHub Actions pipeline, and needed to access output from Terraform. There&#8217;s a <a href=\"https:\/\/www.terraform.io\/docs\/commands\/output.html\">very convenient feature in Terraform<\/a> that allows you to get output from your applied .tf files. You can call those values from the shell directly, and assign them to variables in your shell.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What you need to do to define an output, and then call it from your shell is the following=:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>output \"rgname\" {\n  value = azurerm_resource_group.aks-sd-rg.name\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Which you can then call from a shell like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform output rgname<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And if you want to assign this to a local variable, you can do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rgname=$(terraform output rgname)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, when using this in a GitHub Action, this fails consistently. This can very easily be fixed in the setup of the Terraform action in your GitHub action:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    - name: Setup Terraform\n      uses: hashicorp\/setup-terraform@v1\n      with:\n        terraform_wrapper: false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And with that you should be able to refer to the output correctly. Let me show you a working example of this on GitHub.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Minimal PoC on GitHub<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I built a very minimal PoC of this on <a href=\"https:\/\/github.com\/NillsF\/terraform-output\">GitHub<\/a>. A very small TF file is used to generate output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>locals {\n    test = \"this is the output\"\n}\n\noutput test{\n    value = local.test\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And I have two actions. One without the <code>terraform_wrapper<\/code>, one with the <code>terraform_wrapper<\/code>. In each action, I execute the following small shell script at the end:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>output=$(terraform output test)\necho $output<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without the wrapper, this is the result of the Action:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"230\" src=\"\/wp-content\/uploads\/2020\/08\/image-36-1024x230.png\" alt=\"\" class=\"wp-image-1252\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/08\/image-36-1024x230.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/08\/image-36-300x68.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/08\/image-36-768x173.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/08\/image-36.png 1373w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Output of the echo $output command<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With the wrapper in place, this is the result of the Action:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"892\" height=\"238\" src=\"\/wp-content\/uploads\/2020\/08\/image-37.png\" alt=\"\" class=\"wp-image-1253\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/08\/image-37.png 892w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/08\/image-37-300x80.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/08\/image-37-768x205.png 768w\" sizes=\"auto, (max-width: 892px) 100vw, 892px\" \/><figcaption>Output of the echo $output with the wrapper=false in place.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can check out the full actions files I used for this here:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/NillsF\/terraform-output\/blob\/master\/.github\/workflows\/no-wrapper.yml\">Without wrapper=false<\/a><\/li><li><a href=\"https:\/\/github.com\/NillsF\/terraform-output\/blob\/master\/.github\/workflows\/with-wrapper.yml\">With wrapper=false<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you need to call terraform output in a GitHub action, make sure to include <code>terraform_wrapper: false<\/code> in your GitHub actions file. This took me a couple hours to figure out, so I hope to save you those hours. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I was building a GitHub Actions pipeline, and needed to access output from Terraform. There&#8217;s a very convenient feature in Terraform that allows you to get output from your applied .tf files. You can call those values from the shell directly, and assign them to variables in your shell. What you need to do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1253,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,5],"tags":[53,147,15],"class_list":["post-1251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-open-source","tag-github","tag-github-actions","tag-terraform"],"jetpack_featured_media_url":"https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/08\/image-37.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/1251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/comments?post=1251"}],"version-history":[{"count":1,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/1251\/revisions"}],"predecessor-version":[{"id":1254,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/1251\/revisions\/1254"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/media\/1253"}],"wp:attachment":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/media?parent=1251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/categories?post=1251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/tags?post=1251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}