Error: Missing newline after argument

provider "aws" {
  
  profile                 = "sandbox"
  region                  = "us-east-1"

}

data "aws_instances" "test" {
  instance_tags = {
    env = "test"
  }


  instance_state_names = ["running"]
}
 resource "null_resource" "examplerr6" {
provisioner "local-exec" {
count    = "${length(data.aws_instances.test.ids)}"
command = echo "${element(data.aws_instances.test.ids[count.index])}>>abc.txt"
}
}
Error: Missing newline after argument

  on main.tf line 20, in resource "null_resource" "examplerr6":
  20: command = echo "${element(data.aws_instances.test.ids[count.index])}>>abc.txt"

An argument definition must end with a newline.

Hi @sharathmankala,

Assuming your question here is “what is causing this error?”, it looks like the problem is that your command argument in the provisioner is not properly quoted.

Something like this should work:

   command = "echo ${data.aws_instances.test.ids[count.index]} >>abc.txt"

Another problem here (which Terraform isn’t reporting yet, because of the syntax error) is that your count argument should be directly inside the resource block, not inside the provisioner block. So the whole null_resource configuration might look something like this:

resource "null_resource" "examplerr6" {
  count = length(data.aws_instances.test.ids)
  provisioner "local-exec" {
    command = "echo ${data.aws_instances.test.ids[count.index]} >>abc.txt"
  }
}

It looks like your goal here is to gather a list of instance ids in this file abc.txt. If so, I’d recommend using a local_file resource to create that file rather than provisioners. Provisioners are a last resort, so it’s always better to use resource-based techniques where possible:

resource "local_file" "instance_ids" {
  filename = "abc.txt"
  content = <<-EOT
    %{ for id in data.aws_instances.test.ids ~}
    ${id}
    %{ endfor }
  EOT
}

The content argument above is a Terraform template given inline using “heredoc” syntax. That for directive will repeat its contents once for each id in the list, producing a single string containing all of the ids which local_file will then write into the abc.txt file.

2 Likes

For future reference, this error also appears when you use the ternary the wrong way:

locals {
  some_local = var.some_var : true ? false
}

Switching ? and : can help in this specific case :smiley: