如何将输入转换器用于 ECS Fargate 启动类型和 Terraform CloudWatch 事件触发器
Posted
技术标签:
【中文标题】如何将输入转换器用于 ECS Fargate 启动类型和 Terraform CloudWatch 事件触发器【英文标题】:How to use input transformer for ECS Fargate launch type with Terraform CloudWatch event trigger 【发布时间】:2020-10-31 20:28:51 【问题描述】:我正在使用 terraform 创建一个 CloudWatch 事件触发器,该触发器具有事件源为 S3 的 ECS Fargate 启动类型。当我使用input_transformer
字段将bucket 和key 传入ECS 任务时,我的事件规则导致调用失败。
这是aws_cloudwatch_event_rule
:
resource "aws_cloudwatch_event_rule" "event_rule"
name = "dev-gnss-source-put-rule-tf"
description = "Capture S3 events on uploads bucket"
event_pattern = <<PATTERN
"source": [
"aws.s3"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail":
"eventSource": [
"s3.amazonaws.com"
],
"eventName": [
"PutObject"
],
"requestParameters":
"bucketName": [
"example-bucket-name"
]
PATTERN
这是aws_cloudwatch_event_target
:
resource "aws_cloudwatch_event_target" "event_target"
target_id = "dev-gnss-upload-event-target-tf"
arn = "example-cluster-arn"
rule = aws_cloudwatch_event_rule.event_rule.name
role_arn = aws_iam_role.uploads_events.arn
ecs_target
launch_type = "FARGATE"
task_count = 1 # Launch one container / event
task_definition_arn = "example-task-definition-arn"
network_configuration
subnets = ["example-subnet"]
security_groups = []
input_transformer
input_paths =
s3_bucket = "$.detail.requestParameters.bucketName"
s3_key = "$.detail.requestParameters.key"
input_template = <<TEMPLATE
"containerOverrides": [
"name": "myproject-task",
"environment": [
"name": "S3_BUCKET", "value": <s3_bucket> ,
"name": "S3_KEY", "value": <s3_key>
]
]
TEMPLATE
如果我删除 input_transformer
部分,它会正常工作,但我需要传入 s3 存储桶和密钥来处理特定文件。
我这样做的理由是消除对中介 Lambda 的需求,并以这篇 Medium 帖子为指导:https://medium.com/@bowbaq/trigger-an-ecs-job-when-an-s3-upload-completes-3559c44c37d1
感谢任何建议。
【问题讨论】:
【参考方案1】:兜了几个小时,我找到了答案!
所以第一步是检查调用失败的原因是什么。您可以通过导航到 Cloud Trail > 事件历史 > 搜索 Event name
并在搜索框中键入 RunTask
来检查 CloudTrail 日志来执行此操作。您应该会看到来自事件源 ecs.amazonaws.com
的一系列事件。查找与您所经历的失败调用相关的一个。
当您点击进入活动时,您可以在Event record
部分下看到errorMessage
。就我而言,它是以下内容:
"errorCode": "InvalidParameterException",
"errorMessage": "Override for container named myproject-task is not a container in the TaskDefinition.",
这对您来说可能会有所不同。对我来说,这是因为我的 containerOverride 名称不正确。该字段引用:The name of the container that receives the override. This parameter is required if any override is specified.
ref:https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerOverride.html
更正此字段解决了我的问题。
【讨论】:
以上是关于如何将输入转换器用于 ECS Fargate 启动类型和 Terraform CloudWatch 事件触发器的主要内容,如果未能解决你的问题,请参考以下文章
如何从运行 Fargate ECS 任务中查看 Python 打印语句?