great-sunset-355
01/03/2022, 12:22 PMPulumi.dev.yaml
contains a variable debug: true
I pass this to ECS Task container definition and get an error
Error decoding JSON: json: cannot unmarshal bool into Go struct field KeyValuePair.Environment.Value of type string. Examine values at 'TaskDefinition.ContainerDefinitions'.
This is the code:
import json
import pulumi
def json_output(element) -> pulumi.Output:
"""Dump python object to JSON string as pulumi.Output."""
return pulumi.Output.from_input(element).apply(json.dumps)
container_definition = {
"name": self._config.container_name,
"image": self._config.image_name,
"portMappings": [{"containerPort": self._config.application_port, "protocol": "tcp"}],
"environment": [
{"name": k, "value": v} for k, v in self._config.env_vars.items()
],
"linuxParameters": {"initProcessEnabled": True},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": self._config.log_group_name,
"awslogs-region": self._config.aws_classic_provider.region,
"awslogs-stream-prefix": self._config.log_stream_prefix or "ecs",
},
},
}
self.task_definition = classic_ecs.TaskDefinition(
f"{self._config.name}-td",
args=classic_ecs.TaskDefinitionArgs(
family=f"{self._config.name}",
cpu=self._config.task_cpu,
memory=self._config.task_memory,
network_mode="awsvpc",
requires_compatibilities=["FARGATE"],
execution_role_arn=self.task_exec_role.arn,
task_role_arn=self.task_role.arn,
container_definitions=json_output([container_definition]),
tags=self._config.tags,
)
)
prehistoric-activity-61023
01/03/2022, 12:33 PMself._config.env_vars
look like?{"name": k, "value": str(v)} for k, v in self._config.env_vars.items()
v
can be easily converted using str(v)
)debug: true
. I assume that environment
in container definition stands for environment variables (then it makes sense that they are parsed later as strings). If so, you have to properly convert this boolean value to string -> that depends on how your application handles it. Just remember if you use str(v)
, you’re gonna get upper-case versions:
>>> str(True)
'True'
>>> str(False)
'False'
If you want to fully control the conversion process, simply write a helper function that suits your needs:
def convert_to_env_string(v: Any) -> str:
if isinstance(v, str):
return v
if isinstance(v, bool):
return "true" if v else "false"
...
raise ValueError("unsupported type")
and use it later when creating map for environment field:
{"name": k, "value": convert_to_env_string(v)} for k, v in self._config.env_vars.items()
great-sunset-355
01/04/2022, 12:06 PMprehistoric-activity-61023
01/04/2022, 12:22 PMos.environ
in python) cause you cannot relay type information using them.great-sunset-355
01/04/2022, 12:26 PMinputs
that required JSON string
now accept python Dictonary
, hence the assumptionsprehistoric-activity-61023
01/04/2022, 1:38 PMcontainer_definitions
are supposed to be JSON strings and they actually could be python dicts
. However, your issue was related to the type of environment
field within that dictionary and I guess this part won’t change 🙂.