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

Bitbucket pipeline condition

Nikhil
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 31, 2023

Hi All,

I'm creating ElasticBeanStalk using bitbucket pipeline and terraform.

I have 3 environment, and each environment has 3 regions under the same account. 

I have also incorporated the IAM role and permission within the same pipeline. When trying to run pipeline for different region, it failed in IAM role & permission creation with an error resource already exists.

I want to use the pipeline variable to check if the IAM role/permission needs to create? If the value is True then it should run the script. 

 

I studied the bitbucket if condition, but it won't execute the terraform script, 

 

if $CREATE_ROLE; then
echo true
- cd createIamAcess
- terraform init -backend-config="bucket=$s3_Backend_Bucket_Name" -backend-config="key=$s3_Backend_StateFile_Location" -reconfigure
- terraform plan
- terraform apply -input=false -auto-approve
fi

 

Can anyone please advise, how to use the terraform script within the if condition? or any other alternate option. 

 

Regards,

Nikhil Shah

1 answer

0 votes
Erez Maadani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 1, 2023

Hey @Nikhil 

You can try using bash's implicit if:

[[ ! -z "$CREATE_ROLE" ]] && terraform init -backend-config="bucket=$s3_Backend_Bucket_Name" -backend-config="key=$s3_Backend_StateFile_Location" -reconfigure

Hope that helps

Nikhil
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 21, 2023

Hi,

Thank you for the suggesstion, it really helps. I have modified the script as follows: 

- cd createIamAcess
- if [[ "$CREATE_ROLE" == true ]]; then terraform init -backend-config="bucket=$s3_Backend_Bucket_Name" -backend-config="key=terraform.tfstate" -reconfigure && terraform apply -input=false -auto-approve; fi
# - |
here, CREATE_ROLE is pipeline ariable needs to set prior executing the pipeline 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events