简体   繁体   中英

AWS Cloudformation : ECS Task definition ARN

I have created cfn template to trigger cloudwatch event periodically to run an ecs task with Fargate launch type. The CW rule looks like below:

sfECSScheduleEventRule:
Type: AWS::Events::Rule
Description: ''
Properties:
  State: ENABLED
  ScheduleExpression: rate(1 minute)
  Name: !Sub sf-eventrule-ecs-${WPRegion}-${WPEnv}
  Targets:
  - Id: !Sub sf-ecs-target-task-${WPRegion}-${WPEnv}
    Arn: !GetAtt
      - ECSCluster
      - Arn
    RoleArn: !GetAtt
      - ECSTaskExecutionRole
      - Arn
    EcsParameters:
      #arn:aws:ecs:ap-northeast-1:448965722616:task-definition/fargate-task-jp-stg:latest
      TaskDefinitionArn: !Ref sfECSTaskDefinition          
      TaskCount: !Ref ECSTaskDesiredCount
      LaunchType: FARGATE
      PlatformVersion: 'LATEST'
      NetworkConfiguration:
        AwsVpcConfiguration:
          AssignPublicIp: DISABLED
          Subnets:
          - !Ref sfPrivateSubnet1

When I create the stack, I find the rule's target is like below: 在此处输入图片说明

Which fails to trigger the task. But when I change task revision from 4 to Latest manually , then it works. Task executes periodically.

在此处输入图片说明

How I can change the rule So that it points to the latest?

You are using:

TaskDefinitionArn: !Ref sfECSTaskDefinition 

According to the docs this returns arn with the revision number. Also according to TaskDefinitionArn :

If no task revision is supplied, it defaults to the most recent revision at the time of resource creation.

Thus I think if you manually create the arn, you should be able to get the most recent revision.

!Sub "arn:aws:ecs:${AWS::Region}:${AWS::AccountId}:task-definition/fargate-task-jp-stg"

Don't know if your task family name fargate-task-jp-stg is parameterized or not in the template. If yes, then you can also reference it in the Sub intrinsic function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM