gpt4 book ai didi

amazon-ecs - 如何从触发的 Fargate 任务访问 CloudWatch 事件数据?

转载 作者:行者123 更新时间:2023-12-04 13:14:59 24 4
gpt4 key购买 nike

我阅读了有关如何 Run an Amazon ECS Task When a File is Uploaded to an Amazon S3 Bucket 的文档.但是,本文档没有解释如何从 Fargate 任务代码本身的触发事件中获取存储桶/键值。怎么办?

最佳答案

我不确定您是否还需要这个答案。但是我做了类似于 Steven1978 提到的事情,但只使用了 CloudFormation。

您正在寻找的配置是 InputTransformer。检查此示例以了解事件规则的 YAML CloudFormation 模板:

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> }] } ] }'

通过这种方法,您将能够获取 s3 存储桶名称 (EVENT_BUCKET) 和 s3 对象键 (EVENT_OBJECT_KEY) 作为容器内的环境变量。

信息确实不是很清楚,但这里有一些我用来最终让它工作的资源:

容器覆盖; https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerOverride.html

输入转换器: https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_InputTransformer.html#API_InputTransformer_Contents

关于amazon-ecs - 如何从触发的 Fargate 任务访问 CloudWatch 事件数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61391901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com