Skip to content

Commit

Permalink
feat: add to tf bool draft
Browse files Browse the repository at this point in the history
  • Loading branch information
WayneGoosen committed Jul 26, 2024
1 parent 42f9179 commit 68a6d21
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
Binary file added src/assets/images/terraform-bool-fail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 66 additions & 6 deletions src/content/blog/add-boolean-vars-terraform-azure-devops.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ tags:
- iac
- azurepipelines
title: >-
Creating Boolean Terraform Variables from Azure Pipeline Parameters
Creating Terraform Variables from Azure Pipeline Parameters or Variables
---

This post showcases how to allow users to set boolean parameters on a pipeline and pass them to Terraform as a boolean variable.
This post showcases how to allow users to set parameters or variables on a pipeline and pass them to Terraform as a variable. It showcases step by step how to achieve this starting with an initial pipeline.

## Terraform source

Expand Down Expand Up @@ -41,6 +41,8 @@ resource "random_id" "server" {
count = var.is_create_enabled ? 1 : 0
byte_length = 2
}

resource "
```

### variables.tf
Expand All @@ -51,16 +53,22 @@ type = boolean
default = false
description = "This flag is to enable/disable creation of resources."
}

variable "resource_group_name" {
type = string
default = false
description = "This is the name for the resource group.."
}
```

# Azure Pipelines Source
# Initial Azure Pipelines Source

```yaml
trigger: none

parameters:
- name: isCreateEnabled
type: boolean
variables:
- name: resourceGroupName
value: 'terraform'

stages:
- stage: DeployTf
Expand Down Expand Up @@ -106,3 +114,55 @@ stages:
environmentServiceNameAzureRM: 'global-connection'
commandOptions: '-auto-approve -input=false main.tfplan'
```
## Boolean Terraform Variables
Below shows the additions to the terraform source.
### variables.tf
```yaml
variable "is_create_enabled" {
type = boolean
default = false
description = "This flag is to enable/disable creation of resources."
}
```

### main.tf

```yaml
parameters:
- name: isCreateEnabled
type: boolean
default: false
```
and
```yaml
- task: TerraformTaskV4@4
displayName: 'Terraform Plan'
inputs:
provider: 'azurerm'
command: 'plan'
environmentServiceNameAzureRM: 'global-connection'
commandOptions: '-out main.tfplan -var is_create_enabled=${{ parameters.isCreateEnabled }}'
```
When triggering the initial pipeline manually, you will be faced with the following error:
![Terraform Bool Fail Error](/src/assets/images/terraform-bool-fail.png)
The error message 'a bool is required; to convert from string, use lowercase "false"' gives us a good indication on where the error comes from. Lets add a simple task to show what the string value of isCreateEnabled is :
```yaml
- task: Bash@3
inputs:
targetType: 'inline'
script: |
echo "Boolean value is: ${{ parameters.isCreateEnabled }}"
```
The output is:
'Boolean value is: False'

0 comments on commit 68a6d21

Please sign in to comment.