I was trying to assign a string to the argument user_data when creating aws_launch_configuration, I did as you suggested to use \ than \ to escape characters, it throws error:
Error loading config: Error parsing .\main.tf: At 217:188: expected: IDENT | STRING | ASSIGN | LBRACE got: NUMBER
This is the string I assigned to user_data as follow, could you please advise? The char escape I use is actually '\', it displays '' only.
Hi Bexie,
I took your string and "mentally parsed" it in the way I would expect the Terraform configuration parser to deal with the escapes. I think the problem is that there are some unescaped quotes in the result.
What you have there starts with the following string:
#!/bin/bash -v
ID=\$(curl http://169.254.169.254/latest/meta-data/instance-id)
hostname \$ID
sed -i.bak \'/127.0.0.1/d\' /etc/hosts
sed -i.bak \
It then contains the following stuff that exists outside of a string, due to the unescaped quote:
1 a 127.0.0.1 \$ID.${var.project_name} \$ID localhost\
I expect that this 1 at the start is the unexpected NUMBER token that the error message is referring to.
Then finally there's another string:
/etc/hosts
I think your intention was to produce the following string:
#!/bin/bash -v
ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
hostname $ID
sed -i.bak '/127.0.0.1/d' /etc/hosts
sed -i.bak "1 a 127.0.0.1 $ID.${var.project_name} $ID localhost" /etc/hosts
In which case, the only things that need to be escaped here are the newlines and the quotes on the second sed command, since the rest of the string doesn't contain any quotes, ${ sequences or backslashes:
You might prefer to write it using the "heredoc" syntax, which allows it to wrap over multiple lines and thus be easier to read/maintain. This also avoids the need to escape the quotes, because heredoc strings are not themselves quoted: