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:

A post was split to a new topic: Error: Missing newline after argument with quotes

2 posts were merged into an existing topic: Error: Missing newline after argument with quotes

resouce “null_resource” “cloning” {
depends_on=[aws_s3_bucket]
provisioner =“local-exec” {
command = “git clone https://github.com/umaavuri/cloud-project.git
}
}

resource “aws_s3_bucket_object” “web-object1”{
bucket = aws_s3_bucket.mybucket.bucket
key = “hybrid.jpg”
source = “myimage/hybrid.jpg”
acl = “public-read”
depends_on= [aws_s3_bucket.mybucket,null_resource.cloning]
}

C:\Users\admin\Desktop\New folder\terraform_1.0.9_windows_amd64>terraform init
There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.

│ Error: Missing newline after argument

│ On ec3.tf line 123: An argument definition must end with a newline.