Hi there! Could you guys please help me to underst...
# python
b
Hi there! Could you guys please help me to understand why apply from the Output does not work as expected inside a loop?
Copy code
def container_definition(image_name, service, env_variables):
    print("service:",service)
    print("env_vars:", env_variables)
    return json.dumps([{
        'name': 'server',
        'image': image_name,
        'networkMode': 'awsvpc',
        'readonlyRootFilesystem': False,
        'essential': True,
        'environment': env_variables,
        ..... Non relevant code continues
    }])

for service in config.common_config['services']:
    env_variables.append({'name': 'SERVICES', 'value': service})
    if service != 'frontend':
        env_variables.append({'name': 'TEMPORAL_CLI_ADDRESS',
            'value': f'{config.common_config["frontend"]["service_name"]}.{config.common_config["dns_namespace"]}'})
    task_definitions[service] = aws.ecs.TaskDefinition(config.common_config[service]['service_name'],
        family=config.common_config[service]['service_name'],
        cpu=config.common_config[service]['cpu'],
        memory=config.common_config[service]['memory'],
        network_mode='awsvpc',
        requires_compatibilities=['FARGATE'],
        execution_role_arn=ecs_task_role.arn,
        task_role_arn=ecs_task_role.arn,
        container_definitions=image.image_name.apply(
            lambda image_name: container_definition(image_name, service, env_variables)),
        tags={
            "Service": "temporal",
        },
        opts=ResourceOptions(depends_on=[log_group]))
When it is executed for the container_definitions in preview I am seeing env_variables with duplicates and
service
is set to the latest in the list of
config.common_config['services']
. I do believe it is somehow related to the nature of "Promised" Output, but not an expert in Python enough to catch and fix it. TIA! Everything worked properly in the loop until I needed to add image name for ECR 😄
✅ 1
Don't mind me having those print functions. Left from
red-eyed
debugging
n
in my experience, it seems that Outputs inside a loop are cached by pulumi returning only one of the promise. We hit the same issue and we solved it using inner functions, that will work, something like:
Copy code
def main_func():
    .... code here ....
    def _resolve_promise(args):
        ... some awesome code here ...
    now pass _resolve_promise to an output