Mark Richman
Mark Richman

Reputation: 29720

How to access CloudWatch Event data from triggered Fargate task?

I read the docs on how to Run an Amazon ECS Task When a File is Uploaded to an Amazon S3 Bucket. However, this document stops short of explaining how to get the bucket/key values from the triggering event from within the Fargate task code itself. How can that be done?

Upvotes: 3

Views: 1254

Answers (1)

Vinicius Barros
Vinicius Barros

Reputation: 101

I am not sure if you still need the answer for this one. But I did something similar to what Steven1978 mentioned but only using CloudFormation.

The config you're looking for is the InputTransformer. Check this example for a YAML CloudFormation template for an Event Rule:

rEventRuleForFileUpload:
Type: AWS::Events::Rule
Properties:
  Description: "EventRule"
  State: "ENABLED"
  EventPattern:
    source:
      - "aws.s3"
    detail-type:
      - 'AWS API Call via CloudTrail'
    detail:
      eventSource:
        - s3.amazonaws.com
      eventName:
        - "PutObject"
        - "CompleteMultipartUpload"
      requestParameters:
        bucketName: "{YOUR_BUCKET_NAME}"

  Targets:
    - Id: '{YOUR_ECS_CLUSTER_ID}'
      Arn: !Sub "arn:aws:ecs:${AWS::Region}:${AWS::AccountId}:cluster/${NAME_OF_YOUR_CLUSTER_RESOURCE}"
      RoleArn: !GetAtt {YOUR_ROLE}.Arn
      EcsParameters:
        TaskCount: 1
        TaskDefinitionArn: !Ref {YOUR_TASK_DEFINITION}
        LaunchType: FARGATE

        {... WHATEVER CONFIG YOU MIGHT HAVE...}

      InputTransformer:
        InputPathsMap:
          s3_bucket: "$.detail.requestParameters.bucketName"
          s3_key: "$.detail.requestParameters.key"
        InputTemplate: '{ "containerOverrides": [ { "name": "{THE_NAME_OF_YOUR_CONTAINER_DEFINITION}", "environment": [ { "name": "EVENT_BUCKET", "value": <s3_bucket> }, { "name": "EVENT_OBJECT_KEY", "value": <s3_key> }] } ] }'

With this approach, you'll be able to get the s3 bucket name (EVENT_BUCKET) and the s3 object key (EVENT_OBJECT_KEY) as environment variables inside your container.

The info isn't very clear, indeed, but here are some sources I used to finally get it working:

Container Override; https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerOverride.html

InputTransformer: https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_InputTransformer.html#API_InputTransformer_Contents

Upvotes: 4

Related Questions