Ruy
Ruy

Reputation: 31

Error: Invalid template interpolation value: Cannot include the given value in a string template: string required

I am writing a script for creating a kubernetes cluster in oracle cloud. When I run terraform plan, it returns no errors. But when I run terraform apply, it returns me the error:

Error: Invalid template interpolation value: Cannot include the given value in a string template: string required.

I enabled trace and log file generation to try to identify where the problem is, in trace it returns:

2019/11/21 20:31:15 [TRACE] EvalMaybeTainted: null_resource.k8sworker-ad1 [0] encountered an error during creation, so it is now marked as tainted
2019/11/21 20:31:15 [ERROR] : eval: * terraform.EvalApplyPost, err: 1 error occurred:

    Invalid template interpolation value: Cannot include the given value in a template: string required string.

2019/11/21 20:31:15 [TRACE] EvalMaybeTainted: null_resource.k8sworker-ad1 [1] encountered an error during creation, so it is now marked as tainted
2019/11/21 20:31:15 [ERROR] : eval: * terraform.EvalSequence, err: Invalid template interpolation value: Cannot include the given value in a string template: string required.

I believe the problem is in this part of the code:

resource "null_resource" "k8sworker-ad1" {
  count      = var.k8sWorkerAd1Count
  depends_on = [module.instances-k8sworker-ad1]

  triggers = {
    worker_id       = module.instances-k8sworker-ad1.ids[0][count.index]
    build_source_id = null_resource.build_source.id
  }

  provisioner "local-exec" {
    command = "echo 'alias ${var.label_prefix}workerad1-${count.index}=\"ssh -i ${path.root}/generated/instances_id_rsa opc@${element(module.instances-k8sworker-ad1.public_ips, count.index)}\"' >> source.sh"
  }
}

The Terraform Version is:

Terraform v0.12.16
+ provider.null v2.1.2
+ provider.oci v3.52.0
+ provider.random v2.2.1
+ provider.template v2.1.2
+ provider.tls v2.1.1

The trace output is:

https://gist.github.com/RuyCury/bc0dbccc65bbbadf2eb90569fd438286

Please help me solve this problem.

Upvotes: 3

Views: 13891

Answers (1)

jnamdar
jnamdar

Reputation: 11

I've just run into a similar issue and found a fix so I'll share that here.

Basically my equivalent to the element(module.instances-k8sworker-ad1.public_ips, count.index) I was trying to get was not available at the time of interpolation. What I did to fix it was removing the wait_for_guest_net_timeout = 0 parameter I had put in my VM's resource declaration.

If I let this parameter, when I use terraform console and try to evaluate my VM resource after creation, there is no key called default_ip_address in the output (this is the resource key I was trying to get in the element), and that must be why terraform is returning OP's error (the value is null). When I remove the parameter, the key and value are properly filled. My VM resource is of type vsphere_virtual_machine. I'm guessing OP is using a different type of resource, but the issue might be the same.

Upvotes: 0

Related Questions