How To Convert Jenkins String Parameter to Terraform Map Variable (Invalid Number Literal Issue)
Recently, I encountered an issue when trying to pass string parameters from a Jenkinsfile pipeline stage directly as variables within my terraform configuration. The error message indicated “Invalid number literal.” Here’s the context and resolution of this challenge:
Jenkins Setup (Pipeline):
In our Jenkinsfile
, we defined an optional parameter for inputting IP addresses during a deployment phase, which is critical when provisioning infrastructure via Terraform. The code looks like this:
pipeline {
agent any
parameters {
string(name: 'IP1', defaultValue: '', description: 'Enter first IP address')
}
stages {
stage('Git Checkout') {
steps {
git branch: 'branch1', credentialsId: '', url: '<redacted>.git'
}
}
// Deployment Stage Here...
}
}
Terraform Configuration (variables.tf):
Our terraform
configuration defines a map variable to associate names with IP addresses of virtual machines as follows, using the Jenkins parameter:
variable "vm_map" {
type = map(object({
name = string
ip = string
}))
default = {
"first" = {
name = "ubuntu-jenkins1"
ip = "<IP address>" // Placeholder for dynamic IP input from Jenkins parameter.
}
// Additional VMs...
}
}
Resolution: Error and Correction Methodology
Encountering the error, I discovered that Terraform expects a literal value rather than interpolation syntax provided by shell scripts within sh
blocks in pipelines—this is where we hit our snag. To overcome this hurdle, proper variable substitution must be applied when setting up resources directly through Jenkins pipeline execution:
script {
sh '''
terraform init
terraform plan -var "vms=${params.IP1}" # Correct usage of the parameter within Terraform command line interface (CLI).
terraform apply -auto-approve --var="vms={vm1: {name: "${params.VM_NAME}", ip: "${paramss'}'}}"''' # Using double quotes for complex variable substitution in shell scripts, including nested curly braces and single quote encapsulation within the script block
}
Accepted Answer Summary (Correct Substitution Technique):
I resolved this by embedding a correct terraform apply
command into my Jenkins pipeline:
- Enclose variable substitution with double quotes to ensure proper shell interpretation when passing it in as an argument (
${params}
). - Use curly braces around the map key, followed by single-quoted string interpolation for both name and IP address parameters using
${jenkins parameter}
, ensuring that they are correctly interpreted within Jenkinsfile’s script block:terraform apply -var "vms='{vm1: {name: "${params.VM_NAME}", ip: '${params}'}}'" --auto-approve # Note the double quotes around variable substitution and single quote encapsulation for correct shell interpretation in Jenkins pipeline script block syntax.
By adopting this strategy, I successfully passed my string parameters from a Jenkins build to Terraform without errors related to invalid number literals or improperly parsed variables—allowing seamless infrastructure provision through automation with robust error handling and clear code readability in our CI/CD pipeline. This solution should benefit others facing similar issues when integrating their Java-based builds into a world of continuous delivery powered by Terraform scripts for resource management on the fly!