忘れがちな脳への覚え書きです
正確にはここを読んでください。
Amazon EventBridge input transformation - Amazon EventBridge
サンプルとして、S3バケットのCreate Objectを拾って、バケット名とオブジェクトをECSタスクに環境変数として引き渡す例を記載します。
locals { name = "eventbridge-input-test" ecs_task_definition_arn = "タスク定義ARN" ecs_cluster_arn = "ECS Cluster ARN" security_group_id = "ECSタスクのセキュリティグループ" subnets = ["ECSタスクのサブネット"] } module "source_bucket" { source = "terraform-aws-modules/s3-bucket/aws" version = "3.14.0" bucket = "${local.name}-source" versioning = { enabled = true } } # XXX: s3モジュール単体では通知が対応していないので、ここで別途設定する resource "aws_s3_bucket_notification" "source_bucket" { bucket = module.source_bucket.s3_bucket_id eventbridge = true } module "collect_event_csv" { source = "terraform-aws-modules/eventbridge/aws" version = "1.17.3" create_bus = false role_name = "${local.name}-event" attach_ecs_policy = true ecs_target_arns = [local.ecs_task_definition_arn] rules = { "${local.name}-target-ecs" = { description = "create object event" event_pattern = jsonencode({ "source" : ["aws.s3"], "detail-type" : ["Object Created"] "detail" : { "bucket" : { "name" : ["${module.source_bucket.s3_bucket_id}"] } } }) } } targets = { "${local.name}-target-ecs" = [ { name = "${local.name}-target-ecs" arn = local.ecs_cluster_arn attach_role_arn = true ecs_target = { launch_type = "FARGATE" task_count = 1 task_definition_arn = local.ecs_task_definition_arn network_configuration = { assign_public_ip = true subnets = local.subnets aws_security_groups = [local.security_group_id] } } input_transformer = { input_paths = { # $のあとにeventに存在する目的のパスを指定する。この場合S3のオブジェクト作成Eventからバケット名が取得出来る source_bucket = "$.detail.bucket.name" target_object = "$.detail.object.key" } input_template = <<TEMPLATE { "containerOverrides": [ { "name": "${local.container_name}, # 書き換えるコンテナ名を指定 "environment": [ # input_transformer.input_pathsで指定したsource_bucketの値が実行時に変換される { "name": "SOURCE_BUCKET", "value": <source_bucket> }, { "name": "TARGET_OBJECT", "value": <target_object> } ] } ] } TEMPLATE } } ] } }