Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,960
Community Members
 
Community Events
176
Community Groups

Can we use the repo variables inside the terraform code by referring them as a environment variables

Edited

I am trying to refer the repo variables inside my terraform code as shown below 

export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID

Is this the actual way to declare or is there any other way to declare the variables in the code?

1 answer

2 votes
Patrik S Atlassian Team Dec 09, 2021

Hello @salluri

Welcome to Atlassian Community!

Bitbucket Pipelines allows you add, edit, or remove variables at the account, repository, and deployment environment levels. These are called user-variables, and along with the default variables, they are configured as environment variables in the build container of each of your pipeline's steps.

You can reference those variables in your bitbucket-pipelines.yml file or in any script you invoke using the following syntax :

$MY_BITBUCKET_VARIABLE_NAME

 However, each programming/configuration language has it's own way of fetching the value from an environment variable. For this particular case, where you are using Terraform, looking at their documentation about using environment variables, I've found the following :

  • Each input variable accepted by a module must be declared using a variable block:
    variable "aws_code_access_key" {
      type = string
    }

     

  • Terraform searches the environment of its own process for environment variables named TF_VAR_ followed by the name of a declared variable. For the example variable above, on your build script you would have to set a environment variable named TF_VAR_aws_code_access_key using the value of the pipeline variable $MY_BITBUCKET_VARIABLE_NAME, like this:
    $ export TF_VAR_aws_code_access_key=$MY_BITBUCKET_VARIABLE_NAME
    $ terraform plan

     

  • Within the module that declared a variable, its value can be accessed from within expressions as var.<NAME>, where <NAME> matches the label given in the declaration block:
    resource "aws_instance" "example" {
      instance_type = "t2.micro"
      access_key           = var.aws_code_access_key
    }

The information above was extracted from the official terraform documentation. For more details on how terraform references environment variables you can check the following link:

Hope that helps to address your questions! Let us know if you need further assistance.

Thanks you!

Best regards,

Patrik S

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS

Atlassian Community Events