Can Terraform use bash environment variables?










2















When defining the aws provider in terraform,



provider "aws" 
access_key = "<AWS_ACCESS_KEY>"
secret_key = "<AWS_SECRET_KEY>"
region = "<AWS_REGION>"



I'd like to be able to just use the, already defined, system variables



AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY


Is there any way to have the tf files read environment variables?
doing something like,



provider "aws" 
access_key = env.AWS_ACCESS_KEY_ID
secret_key = env.AWS_SECRET_KEY_ID
region = env.AWS_REGION










share|improve this question


























    2















    When defining the aws provider in terraform,



    provider "aws" 
    access_key = "<AWS_ACCESS_KEY>"
    secret_key = "<AWS_SECRET_KEY>"
    region = "<AWS_REGION>"



    I'd like to be able to just use the, already defined, system variables



    AWS_ACCESS_KEY_ID
    AWS_SECRET_ACCESS_KEY


    Is there any way to have the tf files read environment variables?
    doing something like,



    provider "aws" 
    access_key = env.AWS_ACCESS_KEY_ID
    secret_key = env.AWS_SECRET_KEY_ID
    region = env.AWS_REGION










    share|improve this question
























      2












      2








      2








      When defining the aws provider in terraform,



      provider "aws" 
      access_key = "<AWS_ACCESS_KEY>"
      secret_key = "<AWS_SECRET_KEY>"
      region = "<AWS_REGION>"



      I'd like to be able to just use the, already defined, system variables



      AWS_ACCESS_KEY_ID
      AWS_SECRET_ACCESS_KEY


      Is there any way to have the tf files read environment variables?
      doing something like,



      provider "aws" 
      access_key = env.AWS_ACCESS_KEY_ID
      secret_key = env.AWS_SECRET_KEY_ID
      region = env.AWS_REGION










      share|improve this question














      When defining the aws provider in terraform,



      provider "aws" 
      access_key = "<AWS_ACCESS_KEY>"
      secret_key = "<AWS_SECRET_KEY>"
      region = "<AWS_REGION>"



      I'd like to be able to just use the, already defined, system variables



      AWS_ACCESS_KEY_ID
      AWS_SECRET_ACCESS_KEY


      Is there any way to have the tf files read environment variables?
      doing something like,



      provider "aws" 
      access_key = env.AWS_ACCESS_KEY_ID
      secret_key = env.AWS_SECRET_KEY_ID
      region = env.AWS_REGION







      terraform






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 1:10









      jmartorijmartori

      5518




      5518






















          2 Answers
          2






          active

          oldest

          votes


















          4














          Yes, can read environment variables in Terraform. There is a very specific way that this has to be done. You will need to make the environment variable a variable in terraform.



          For example I want to pass in a super_secret_variable to terraform. I will need to create a variable for it in my terraform file.



          variable "super_secret_variable" 
          type = "string



          Then based on convention I will have to prefix my environment variable with TF_VAR_ like this:



          TF_VAR_super_secret_variable


          Then terraform will automatically detect it and use it. Terraform processors variables based on a specific order that order is -var option, -var-file option, environment variable, then default values if defined in your tf file.



          Alternative you can pass environment variables in through the CLI to set variables in terraform like so.



          > terraform apply -var super_secret_variable=$super_secret_variable


          This doesn't require that you prefix it so if they are something you can't change that may be your best course of action.



          You can read more here in the docs.






          share|improve this answer






























            2














            You could just use bare provider like so:



            provider "aws"



            And make sure env vars available in your shell session:



            $ export AWS_ACCESS_KEY_ID="your-key-id"
            $ export AWS_SECRET_ACCESS_KEY="your-secret-key"
            $ export AWS_DEFAULT_REGION="your-region"


            Then, check if the above works:



            terraform plan



            If you satisfy the the plan, you might perform terraform apply to make the change.



            For more details: https://www.terraform.io/docs/providers/aws/#environment-variables






            share|improve this answer


















            • 3





              While this is true for the AWS credentials it might be worth pointing out that you can pass environment variables to Terraform by prefixing them with TF_VAR_ as well.

              – ydaetskcoR
              Nov 16 '18 at 9:28











            • @ydaetskcoR Agreed. While I was trying to answer to this question OP asked specifically about aws provider as mentioned in the question description, it would be great mentioning TF_VAR_ for general use cases that terraform implicitly handle variables even if it's not asked in the first place.

              – zdk
              Nov 21 '18 at 5:12










            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330060%2fcan-terraform-use-bash-environment-variables%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            Yes, can read environment variables in Terraform. There is a very specific way that this has to be done. You will need to make the environment variable a variable in terraform.



            For example I want to pass in a super_secret_variable to terraform. I will need to create a variable for it in my terraform file.



            variable "super_secret_variable" 
            type = "string



            Then based on convention I will have to prefix my environment variable with TF_VAR_ like this:



            TF_VAR_super_secret_variable


            Then terraform will automatically detect it and use it. Terraform processors variables based on a specific order that order is -var option, -var-file option, environment variable, then default values if defined in your tf file.



            Alternative you can pass environment variables in through the CLI to set variables in terraform like so.



            > terraform apply -var super_secret_variable=$super_secret_variable


            This doesn't require that you prefix it so if they are something you can't change that may be your best course of action.



            You can read more here in the docs.






            share|improve this answer



























              4














              Yes, can read environment variables in Terraform. There is a very specific way that this has to be done. You will need to make the environment variable a variable in terraform.



              For example I want to pass in a super_secret_variable to terraform. I will need to create a variable for it in my terraform file.



              variable "super_secret_variable" 
              type = "string



              Then based on convention I will have to prefix my environment variable with TF_VAR_ like this:



              TF_VAR_super_secret_variable


              Then terraform will automatically detect it and use it. Terraform processors variables based on a specific order that order is -var option, -var-file option, environment variable, then default values if defined in your tf file.



              Alternative you can pass environment variables in through the CLI to set variables in terraform like so.



              > terraform apply -var super_secret_variable=$super_secret_variable


              This doesn't require that you prefix it so if they are something you can't change that may be your best course of action.



              You can read more here in the docs.






              share|improve this answer

























                4












                4








                4







                Yes, can read environment variables in Terraform. There is a very specific way that this has to be done. You will need to make the environment variable a variable in terraform.



                For example I want to pass in a super_secret_variable to terraform. I will need to create a variable for it in my terraform file.



                variable "super_secret_variable" 
                type = "string



                Then based on convention I will have to prefix my environment variable with TF_VAR_ like this:



                TF_VAR_super_secret_variable


                Then terraform will automatically detect it and use it. Terraform processors variables based on a specific order that order is -var option, -var-file option, environment variable, then default values if defined in your tf file.



                Alternative you can pass environment variables in through the CLI to set variables in terraform like so.



                > terraform apply -var super_secret_variable=$super_secret_variable


                This doesn't require that you prefix it so if they are something you can't change that may be your best course of action.



                You can read more here in the docs.






                share|improve this answer













                Yes, can read environment variables in Terraform. There is a very specific way that this has to be done. You will need to make the environment variable a variable in terraform.



                For example I want to pass in a super_secret_variable to terraform. I will need to create a variable for it in my terraform file.



                variable "super_secret_variable" 
                type = "string



                Then based on convention I will have to prefix my environment variable with TF_VAR_ like this:



                TF_VAR_super_secret_variable


                Then terraform will automatically detect it and use it. Terraform processors variables based on a specific order that order is -var option, -var-file option, environment variable, then default values if defined in your tf file.



                Alternative you can pass environment variables in through the CLI to set variables in terraform like so.



                > terraform apply -var super_secret_variable=$super_secret_variable


                This doesn't require that you prefix it so if they are something you can't change that may be your best course of action.



                You can read more here in the docs.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 18:04









                JamieJamie

                1,133820




                1,133820























                    2














                    You could just use bare provider like so:



                    provider "aws"



                    And make sure env vars available in your shell session:



                    $ export AWS_ACCESS_KEY_ID="your-key-id"
                    $ export AWS_SECRET_ACCESS_KEY="your-secret-key"
                    $ export AWS_DEFAULT_REGION="your-region"


                    Then, check if the above works:



                    terraform plan



                    If you satisfy the the plan, you might perform terraform apply to make the change.



                    For more details: https://www.terraform.io/docs/providers/aws/#environment-variables






                    share|improve this answer


















                    • 3





                      While this is true for the AWS credentials it might be worth pointing out that you can pass environment variables to Terraform by prefixing them with TF_VAR_ as well.

                      – ydaetskcoR
                      Nov 16 '18 at 9:28











                    • @ydaetskcoR Agreed. While I was trying to answer to this question OP asked specifically about aws provider as mentioned in the question description, it would be great mentioning TF_VAR_ for general use cases that terraform implicitly handle variables even if it's not asked in the first place.

                      – zdk
                      Nov 21 '18 at 5:12















                    2














                    You could just use bare provider like so:



                    provider "aws"



                    And make sure env vars available in your shell session:



                    $ export AWS_ACCESS_KEY_ID="your-key-id"
                    $ export AWS_SECRET_ACCESS_KEY="your-secret-key"
                    $ export AWS_DEFAULT_REGION="your-region"


                    Then, check if the above works:



                    terraform plan



                    If you satisfy the the plan, you might perform terraform apply to make the change.



                    For more details: https://www.terraform.io/docs/providers/aws/#environment-variables






                    share|improve this answer


















                    • 3





                      While this is true for the AWS credentials it might be worth pointing out that you can pass environment variables to Terraform by prefixing them with TF_VAR_ as well.

                      – ydaetskcoR
                      Nov 16 '18 at 9:28











                    • @ydaetskcoR Agreed. While I was trying to answer to this question OP asked specifically about aws provider as mentioned in the question description, it would be great mentioning TF_VAR_ for general use cases that terraform implicitly handle variables even if it's not asked in the first place.

                      – zdk
                      Nov 21 '18 at 5:12













                    2












                    2








                    2







                    You could just use bare provider like so:



                    provider "aws"



                    And make sure env vars available in your shell session:



                    $ export AWS_ACCESS_KEY_ID="your-key-id"
                    $ export AWS_SECRET_ACCESS_KEY="your-secret-key"
                    $ export AWS_DEFAULT_REGION="your-region"


                    Then, check if the above works:



                    terraform plan



                    If you satisfy the the plan, you might perform terraform apply to make the change.



                    For more details: https://www.terraform.io/docs/providers/aws/#environment-variables






                    share|improve this answer













                    You could just use bare provider like so:



                    provider "aws"



                    And make sure env vars available in your shell session:



                    $ export AWS_ACCESS_KEY_ID="your-key-id"
                    $ export AWS_SECRET_ACCESS_KEY="your-secret-key"
                    $ export AWS_DEFAULT_REGION="your-region"


                    Then, check if the above works:



                    terraform plan



                    If you satisfy the the plan, you might perform terraform apply to make the change.



                    For more details: https://www.terraform.io/docs/providers/aws/#environment-variables







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 4:42









                    zdkzdk

                    1,088614




                    1,088614







                    • 3





                      While this is true for the AWS credentials it might be worth pointing out that you can pass environment variables to Terraform by prefixing them with TF_VAR_ as well.

                      – ydaetskcoR
                      Nov 16 '18 at 9:28











                    • @ydaetskcoR Agreed. While I was trying to answer to this question OP asked specifically about aws provider as mentioned in the question description, it would be great mentioning TF_VAR_ for general use cases that terraform implicitly handle variables even if it's not asked in the first place.

                      – zdk
                      Nov 21 '18 at 5:12












                    • 3





                      While this is true for the AWS credentials it might be worth pointing out that you can pass environment variables to Terraform by prefixing them with TF_VAR_ as well.

                      – ydaetskcoR
                      Nov 16 '18 at 9:28











                    • @ydaetskcoR Agreed. While I was trying to answer to this question OP asked specifically about aws provider as mentioned in the question description, it would be great mentioning TF_VAR_ for general use cases that terraform implicitly handle variables even if it's not asked in the first place.

                      – zdk
                      Nov 21 '18 at 5:12







                    3




                    3





                    While this is true for the AWS credentials it might be worth pointing out that you can pass environment variables to Terraform by prefixing them with TF_VAR_ as well.

                    – ydaetskcoR
                    Nov 16 '18 at 9:28





                    While this is true for the AWS credentials it might be worth pointing out that you can pass environment variables to Terraform by prefixing them with TF_VAR_ as well.

                    – ydaetskcoR
                    Nov 16 '18 at 9:28













                    @ydaetskcoR Agreed. While I was trying to answer to this question OP asked specifically about aws provider as mentioned in the question description, it would be great mentioning TF_VAR_ for general use cases that terraform implicitly handle variables even if it's not asked in the first place.

                    – zdk
                    Nov 21 '18 at 5:12





                    @ydaetskcoR Agreed. While I was trying to answer to this question OP asked specifically about aws provider as mentioned in the question description, it would be great mentioning TF_VAR_ for general use cases that terraform implicitly handle variables even if it's not asked in the first place.

                    – zdk
                    Nov 21 '18 at 5:12

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330060%2fcan-terraform-use-bash-environment-variables%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党