添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account
  • aws_launch_configuration
  • 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.

    user_data = "#!/bin/bash -v\nID=\\$(curl http://169.254.169.254/latest/meta-data/instance-id)\nhostname \\$ID\nsed -i.bak \\'/127.0.0.1/d\\' /etc/hosts\nsed -i.bak \\"1 a 127.0.0.1 \\$ID.${var.project_name} \\$ID localhost\\" /etc/hosts"

    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:

    user_data = "#!/bin/bash -v\nID=$(curl http://169.254.169.254/latest/meta-data/instance-id)\nhostname $ID\nsed -i.bak '/127.0.0.1/d' /etc/hosts\nsed -i.bak \"1 a 127.0.0.1 $ID.${var.project_name} $ID localhost\" /etc/hosts"

    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:

    user_data = <<EOT
    #!/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
    

    The older issue #4052 that you found and initially commented on was actually discussing a slightly-different problem: including literal backslashes in strings. Since your script doesn't contain any literal backslashes the advice from there does not apply.

    I hope this helps! Please let me know if I misunderstood what you were asking.

    hI @apparentlymart

    Iam also getting an error similar to this
    I am initializing a variable like this

    value = "{"android": {"newest": "0.1.11", "oldest":"0.1.9"}, "ios": {"newest": "0.1.11", "oldest":"0.1.9"}}"

    and getting error as

    expected: IDENT | STRING | ASSIGN | LBRACE got: FLOAT

    is there any changes I need to make.

    Thanks you.

    I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

    If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.