简体   繁体   English

如何在 Terraform 中使用 JSON 编码指定解组类型?

[英]How do I specify unmarshall types with JSON encode in Terraform?

I am working on building a fargate cluster and am having difficulties following the documentation for the aws_ecs_task_definition section ( https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition )我正在构建一个 fargate 集群,并且在遵循 aws_ecs_task_definition 部分的文档时遇到了困难 ( https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition )

│ Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition.Cpu of type int64
│ 
│   with aws_ecs_task_definition.app,
│   on ecs.tf line 40, in resource "aws_ecs_task_definition" "app":
│   40:   container_definitions = jsonencode([
│   41:     {
│   42:       "name": "${var.prefix}",
│   43:       "image": "${var.app_image}",
│   44:       "cpu": "256",
│   45:       "memory": "${var.fargate_memory}",
│   46:       "networkMode": "awsvpc",
│   47:       "logConfiguration": {
│   48:           "logDriver": "awslogs",
│   49:           "options": {
│   50:             "awslogs-group": "${aws_cloudwatch_log_group.ecs.name}",
│   51:             "awslogs-region": "${var.region}",
│   52:             "awslogs-stream-prefix": "ecs"
│   53:           }
│   54:       },
│   55:       "environment": [
│   56:         {"name": "ENV_RUNNER_SLEEP_SECS", "value": "${var.app_env_runner_sleep_secs}"}
│   57:       ]
│   58:     }
│   59:   ])

The error is pointing to the value for CPU.错误指向 CPU 的值。 This would normally be another variable but I'm just testing out inputs to try and get it to pass.这通常是另一个变量,但我只是测试输入以尝试使其通过。 Annoyingly enough, if I set the value to a number, I get a different error: cannot unmarshal number into Go struct field KeyValuePair.Environment.Value of type string .令人讨厌的是,如果我将值设置为一个数字,我会得到一个不同的错误: cannot unmarshal number into Go struct field KeyValuePair.Environment.Value of type string

Any ideas?有任何想法吗?

Your analysis of the first error is correct: it is due to cpu having to be an integer / number / int64.您对第一个错误的分析是正确的:这是由于cpu必须是整数/数字/int64。 That means you need to specify it as "cpu": 256 .这意味着您需要将其指定为"cpu": 256

The second error then tells you not to look at ContainerDefinition.Cpu but KeyValuePair.Environment.Value which is the "environment": [ ... ] section.然后第二个错误告诉您不要查看ContainerDefinition.Cpu而是KeyValuePair.Environment.Value这是"environment": [ ... ]部分。 The problem here is that keys and values have to be string s and even though you write "${var.app_env_runner_sleep_secs}" terraform still outputs a number despite the " surrounding it. To fix that you need to put a tostring around the argument: "value": "${tostring(var.app_env_runner_sleep_secs)}" .这里的问题是键和值必须是string s,即使你写了"${var.app_env_runner_sleep_secs}" terraform 仍然输出一个数字,尽管"围绕它。要解决这个问题,你需要在参数周围放置一个tostring"value": "${tostring(var.app_env_runner_sleep_secs)}"

Note that additionally depending on your terraform version it is a lot shorter and cleaner to write eg "awslogs-region": var.region by removing the "${...}" in all places.请注意,另外,根据您的 terraform 版本,通过删除所有位置的"${...}"来编写例如"awslogs-region": var.region更短更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM