You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS CDK Python部署ScheduledFargateTask时无法找到Role属性

解决AWS CDK Python中ScheduledFargateTask指定事件角色的问题

你遇到的问题是patterns.ScheduledFargateTask构造函数本身没有直接暴露role参数来分配事件规则的执行角色,这个角色需要通过event_rule_target_options参数来传递。

修改后的代码示例:

# Create schedule task
scheduledtask = patterns.ScheduledFargateTask(
    self, "Fargate_Service",
    cluster=cluster,  # Required: Provide the cluster
    scheduled_fargate_task_definition_options=scheduled_task,
    schedule=autoscaling.Schedule.expression("cron(30 11,12 * * ? *)"),  # Required: Define the schedule
    desired_task_count=1,
    security_groups=[ecs_sg],
    event_rule_target_options=ecs_patterns.EventRuleTargetOptions(
        role=eventrole  # 在这里指定事件执行角色
    )
)

补充说明

  • ScheduledFargateTask会自动创建CloudWatch Event Rule,触发ECS任务的角色需要通过event_rule_target_options传递给规则的目标配置。
  • 如果不手动指定这个角色,CDK会自动创建一个具备必要权限的默认角色,仅当你需要自定义权限策略时,才需要传入自定义角色。

内容的提问来源于stack exchange,提问作者user965291

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 01:02:33