gpt4 book ai didi

aws-cloudformation - 如何通过 s3 对象创建事件上的 CloudWatch 事件触发器将存储桶/键名称传递给 Fargate 作业?

转载 作者:行者123 更新时间:2023-12-03 07:19:46 25 4
gpt4 key购买 nike

我创建了一个fargate任务,并尝试通过cloudformation通过s3对象创建事件(参见下面的示例)触发它。因为它无法直接触发它,所以我创建了一个cloudwatchevent。我正在尝试将存储桶和对象名称传递给我的fargate任务代码。做了一些研究,我遇到了 -> InputTransformer,但我不确定如何传递我的存储桶和键名称的值以及如何在我的 python 代码中读取它。任何帮助将不胜感激。

AWSTemplateFormatVersion: 2010-09-09
Description: An example CloudFormation template for Fargate.
Parameters:
VPC:
Type: AWS::EC2::VPC::Id
SubnetA:
Type: AWS::EC2::Subnet::Id
SubnetB:
Type: AWS::EC2::Subnet::Id
Image:
Type: String
Default: 123456789012.dkr.ecr.region.amazonaws.com/image:tag
Resources:
mybucket:
Properties:
BucketName: 'mytestbucket-us'

cloudwatchEvent:
Type: AWS::Events::Rule
Properties:
EventPattern:
source:
- aws.s3
detail:
eventSource:
- s3.amazonaws.com
eventName:
- PutObject
- CompleteMultipartUpload
requestParameters:
bucketName:
- !Ref mybucket
Targets:
- Id: my-fargate-task
Arn: myclusterArn
RoleArn: myinvocationrolearn
Input:
'Fn::Sub':
- >-
{"containerOverrides": [{"name":"somecontainer"]}
EcsParameters:
TaskDefinition:
LaunchType: 'FARGATE'
...
NetworkConfiguration:
...

Cluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Join ['', [!Ref ServiceName, Cluster]]

TaskDefinition:
Type: AWS::ECS::TaskDefinition
DependsOn: LogGroup
Properties:
Family: !Join ['', [!Ref ServiceName, TaskDefinition]]
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
Cpu: 256
Memory: 2GB
ExecutionRoleArn: !Ref ExecutionRole
TaskRoleArn: !Ref TaskRole
ContainerDefinitions:
- Name: !Ref ServiceName
Image: !Ref Image

# A role needed by ECS
ExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Join ['', [!Ref ServiceName, ExecutionRole]]
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
# A role for the containers
TaskRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Join ['', [!Ref ServiceName, TaskRole]]
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: 'sts:AssumeRole'

最佳答案

您将使用CloudWatch Event Input Transformer从事件中提取所需的数据,并将该数据作为目标 ContainerOverrides 中的环境变量传递给 ECS 任务。我不使用 CloudFormation,但是here's an example using Terraform .

关于aws-cloudformation - 如何通过 s3 对象创建事件上的 CloudWatch 事件触发器将存储桶/键名称传递给 Fargate 作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73328577/

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