is there any way to provide a dynamic value to `ex...
# aws
i
is there any way to provide a dynamic value to
execution_role
in
awsx.ecs.EC2ServiceTaskDefinitionArgs
?
specifically,
execution_role
is a
DefaultRoleWithPolicyArgs
which takes either a
RoleWithPolicyArgs
or a
role_arn: str
... i'd like to use a dynamic value inside
RoleWithPolicyArgs
i want to add a policy that grants access to a specific Secrets Manager secret
err i guess specifically on
RoleWithPolicyArgs
i was trying to use
policy_arns
, which doesn't accept an input
w
Can you share the code you are trying to make work?
i
sure - it looks something like this (with a bunch of irrelevant params removed) -
Copy code
ecs_service = awsx.ecs.FargateService(f"service-{app}",
    task_definition_args=awsx.ecs.FargateServiceTaskDefinitionArgs(
        execution_role=awsx.awsx.DefaultRoleWithPolicyArgs(
            args=awsx.awsx.RoleWithPolicyArgs(
                managed_policy_arns=infra_stack.get_output('strata_db_asm_policy_arn').apply(lambda arn: [ 
                    "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
                    arn
                ]),
            ),
        ),
    )
)
it appears using the
managed_policy_arns
param as above works, whereas the
policy_arns
param doesn't
w
Right. So you should use
managed_policy_arns
. That’s an (the?) intent of
managed_policy_arns
vs
policy_arns
- to allow for dynamic values (i.e. Output<T>). Or is that not working?
i
managed_policy_arns
is working, but the doc suggests it's deprecated
let me see if i can find it
i was looking at iam.Role which i assume is the underlying resource for the awsx version: managed_policy_arns
The
managed_policy_arns
argument is deprecated. Use the
aws.iam.RolePolicyAttachment
resource instead. If Pulumi should exclusively manage all managed policy attachments (the current behavior of this argument), use the
aws.iam.RolePolicyAttachmentsExclusive
resource as well.
w
Yeah. And it looks like
awsx
doesn’t take that
managed_policy_arns
and turn it into
RolePolicyAttachments
and instead does use the deprecated
managed_policy_arns
property on the execution role. So, the right answer for now is to not set
managed_policy_arns
and let the taskdefinition get created without the policy arns attached to the execution role. Then you can use
RolePolicyAttachment
or
RolePolicyAttachmentsExclusive
and specify the execution role that was created -
task_definition.execution_role.arn
i
i tried that - but pulumi hangs because the container fails to start since it's missing access to secrets
i guess i can pre-create the role, and then use
role_arn
instead of
args
in
DefaultRoleWithPolicyArgs
when specifying
execution_role
?
is
policy_arns
the replacement for
managed_policy_arns
on
awsx.DefaultRoleWithPolicyArgs
? back to my original question - why doesn't it accept
Input
?
w
the awsx resource is lagging the aws provider resource. So it still has the deprecated policy_arns that doesn’t take input. I’m afk right now, but you may need to use the base aws provider resources for this use-case.
👍 1
i
understood. is there any downside to using
managed_policy_arns
if i don't fiddle with that role/policies anywhere else?
w
Another option is to create the task definition and assign the role(s) and the fargate service separately. Something like:
Copy code
# Simple EC2 Task Definition
task_definition = awsx.ecs.EC2TaskDefinition("my-task",
    containers={
        "nginx": awsx.ecs.TaskDefinitionContainerDefinitionArgs(
            name="nginx",  # Container name is required
            image="nginx:latest",
            memory=512,
            cpu=256,
            port_mappings=[
                awsx.ecs.TaskDefinitionPortMappingArgs(
                    container_port=8080,
                    host_port=8080,  # EC2 tasks can map to specific host ports
                    protocol="tcp",
                )
            ],
            # Optional: Add environment variables
            environment=[
                awsx.ecs.TaskDefinitionKeyValuePairArgs(
                    name="ENV",
                    value="production"
                )
            ],
        )
    },
    cpu="512",
    memory="1024"
)

roleattachment = aws.iam.RolePolicyAttachment("my-role-attachment",
    role=task_definition.execution_role.name,
    policy_arn="arn:aws:iam::aws:policy/AmazonSSMReadOnlyAccess"
)

# Fargate Service using AWSX with existing task definition
fargate_service = awsx.ecs.FargateService("my-fargate-service",
    task_definition=task_definition.task_definition,  # Use the existing task definition
   .....
)
👀 1
🙌 1
As far as using
managed_role_arns
it’s probably safe to do so since the upstream provider can’t get rid of it anytime soon since so many users use it.
👍 1