如何解决Terraform:在Docker容器内将JSON文件作为环境变量值与systemd单位文件一起传递
我正在尝试在terraform的systemd单位文件的环境变量中传递json。我正在使用名为CT的外部提供程序来从YAML配置生成点火。
CT配置:
data "ct_config" "ignition" {
# Reference: https://github.com/poseidon/terraform-provider-ct/
content = data.template_file.bastion_user_data.rendered
strict = true
pretty_print = false
}
错误:
Error: error converting to Ignition: error at line 61,column 17
invalid unit content: unexpected newline encountered while parsing option name
on ../../modules/example/launch-template.tf line 91,in data "ct_config" "ignition":
91: data "ct_config" "ignition" {
单位文件内容:
- name: cw-agent.service
enabled: true
contents: |
[Unit]
Description=Cloudwatch Agent Service
Documentation=https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html
Requires=docker.socket
After=docker.socket
[Service]
TimeoutStartSec=0
Restart=always
RestartSec=10s
Environment=CONFIG=${cw-agent-config}
ExecStartPre=-/usr/bin/docker kill cloudwatch-agent
ExecStartPre=-/usr/bin/docker rm cloudwatch-agent
ExecStartPre=/usr/bin/docker pull amazon/cloudwatch-agent
ExecStart=/usr/bin/docker run --name cloudwatch-agent \
--net host \
--env CW_CONFIG_CONTENT=$CONFIG \
amazon/cloudwatch-agent
ExecStop=/usr/bin/docker stop cloudwatch-agent
[Install]
WantedBy=multi-user.target
渲染:
data "template_file" "cw_agent_config" {
template = file("${path.module}/../cw-agent-config.json")
}
cw-agent-config = indent(10,data.template_file.cw_agent_config.rendered)
文件内容:
{
"agent": {
"metrics_collection_interval": 60,"run_as_user": "cwagent"
},"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "$${aws:AutoScalingGroupName}","ImageId": "$${aws:ImageId}","InstanceId": "$${aws:InstanceId}","InstanceType": "$${aws:InstanceType}"
},"metrics_collected": {
"disk": {
"drop_device" : true,"measurement": [
"used_percent"
],"metrics_collection_interval": 120,"resources": [
"/"
]
},"mem": {
"measurement": [
"mem_used_percent"
],"metrics_collection_interval": 120
}
}
}
}
我需要此json文件作为docker容器内名为CW_CONFIG_CONTENT
的环境变量的值来提供。
解决方法
此问题通过使用Terraform jsonencode函数解决。
cw-agent-config = jsonencode(data.template_file.cw_agent_config.rendered)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。