I have a problem: `Pulumi.dev.yaml` contains a var...
# python
g
I have a problem:
Pulumi.dev.yaml
contains a variable
debug: true
I pass this to ECS Task container definition and get an error
Copy code
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:
Copy 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,
            )
        )
p
based on the error message… how does the content of
self._config.env_vars
look like?
I think it might expect values as strings and it fails because at least of the values is boolean
I’d try to convert all values to strings first:
Copy code
{"name": k, "value": str(v)} for k, v in self._config.env_vars.items()
(considering that all values
v
can be easily converted using
str(v)
)
Ok, you wrote that you have variable
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:
Copy code
>>> str(True)
'True'

>>> str(False)
'False'
If you want to fully control the conversion process, simply write a helper function that suits your needs:
Copy code
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:
Copy code
{"name": k, "value": convert_to_env_string(v)} for k, v in self._config.env_vars.items()
g
Yeah it assumes all values to be a string
and I wanted to avoid the conversion function. I'll keep it as is, knowing the limitation and have high hopes for aws native
p
do you think it’s a limitation and aws native will resolve this?
I wouldn’t say so. In most cases, environment variables are treated as strings (same goes for k8s deployments or reading envs using
os.environ
in python) cause you cannot relay type information using them.
g
That's difficult to say, since I did not try this use-case exactly but I noticed that in python
inputs
that required
JSON string
now accept python
Dictonary
, hence the assumptions
p
Ok, this part might change. Right now,
container_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 🙂.