0

I am trying to provision an EC2 instance using terraform, passing a cloudinit configuration file as userdata to the instance.

As part of the cloudinit config I am setting environment variables and some of the variables are JSON strings.

The user data that is generated by terraform and passed to the instance looks correct, surrounded by single quotes, but when the environment variables are written to the file on the server the quotes are stripped which is causing issues

Here is an example snippet of the user data passed to the EC2 instance

#cloud-config

environment:
  var: '{"key": "value"}'

Then on the server the environment file (/etc/environment) looks like this

var={"key": "value"}

But I need to to still be surrounded by single quotes when in /etc/environment

var='{"key": "value"}'

Any ideas on how I do this? I tried escaping the quotes using \' but that is not valid YAML and so the user data fails with an error, any thoughts on this would be appreciated.

1
  • Can you exploit the fact that YAML is a superset of JSON here? Commented Apr 6, 2023 at 21:26

1 Answer 1

3

It seems you may want a block for your YAML value. You can do this using >.

#cloud-config

environment: 
  var: >
    '{"key": "value"}'
2
  • Thanks AbeCoull think you are right will give this a test. Looked more into it and block content can be defined using both | and > depending on whether new lines or spaces are wanted with the block of text. Think this is exactly what I need.
    – Andrew Kew
    Commented Apr 7, 2023 at 7:41
  • 1
    tested this and it was exactly what I needed, thanks for your help
    – Andrew Kew
    Commented Apr 11, 2023 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.