I'm trying to pass string parameter(s) to a Terraform map variable, but receiving error "Invalid number literal". It's not quite clear how to access keys and values in Terraform maps when passing a Jenkins parameter via terraform apply -var ...
Jenkinsfile:
pipeline {
agent any
parameters {
string(name: 'IP1', defaultValue: '', description: 'Enter first IP address')
}
stages {
stage('Git Checkout') {
steps {
git branch: 'branch1', credentialsId: '', url: 'http://<redacted>.git'
}
}
stage('Deploy Terraform') {
steps {
script {
dir('Linux') {
sh """
terraform init
terraform plan
terraform apply -var 'vms=${params.IP1}' \
--auto-approve
"""
}
}
}
}
}
}
}
variables.tf
variable "vm_map" {
type = map(object({
name = string
ip = string
}))
default = {
"first" = {
name = "ubuntu-jenkins1"
ip = "172.30.100.160"
}
"second" = {
name = "ubuntu-jenkins2"
ip = "172.30.100.161"
}
"third" = {
name = "ubuntu-jenkins3"
ip = "172.30.100.162"
}
}
}
I figured it out! You can substitute 'vm1' for any value that identifies the first map object.
terraform apply -var vms='''{vm1: {name: "ubuntu",ip: "${params.IP1}"}}''' --auto-approve
If you also wanted to add a name string parameter for the vm name, it would look like
terraform apply -var vms='''{vm1: {name: "${params.VM_NAME1}",ip: "${params.IP1}"}}''' --auto-approve
Here's the terraform apply for multiple vm's
terraform apply -var 'vm_map={"first": {"name": "ubuntu-jenkins1", "ip": "172.30.100.160"}, \
"second": {"name": "ubuntu-jenkins2", "ip": "172.30.100.161"}, \
"third": {"name": "ubuntu-jenkins3", "ip": "172.30.100.162"}}' --auto-approve