Home > Net >  sed command fails when running via terraform local exec in linux
sed command fails when running via terraform local exec in linux

Time:08-09

I am trying to run the below sed command using terraform local_exec to replace the account id in a deployment yaml file.I am running it in ubuntu ec2 machine

  provisioner "local-exec" {
    interpreter = ["bash", "-c"]
    command = <<EOT
      sed -i 's/\(image: \)[^.]*\(\.dkr\.\)/\1${data.aws_caller_identity.current.account_id}\2/' deployment/java-apps/deployment.yaml
    EOT
  }

But it fails with the following error: No such file or directory Output: sed: can't read deployment/java-apps/deployment.yaml

When I run it manually the command it can find the file and it replaces the value. But when its executed via terraform it fails. It even works when I execute the same terraform script using git bash in my windows machine. But in the ubuntu linux machine I receive the error.

What do I need to change to make it run via terraform?

CodePudding user response:

You should check newlines of all your terraform files - they should be Unix newlines (\n), not Windows (\r\n).

This could be a possible cause of your problem, since you mentioned Windows machine and your command happens to contain a newline in a heredoc-string in your yaml.

With Windows's newline, you'll see something like this when you run your command via terraform in Linux:

(local-exec): Executing: ["bash" "-c" "      sed -i 's/test/text/' deployment/java-apps/deployment.yaml\r\n"]

Note the deployment/java-apps/deployment.yaml\r\n - that \r will be treated as part of a filename, giving you error No such file or directory.

  • Related