gpt4 book ai didi

amazon-web-services - 通过 cloudformation 为 fargate launchtype 任务为 cloudwatch 事件规则创建 'Target'

转载 作者:行者123 更新时间:2023-12-04 02:11:18 25 4
gpt4 key购买 nike

我正在尝试在我的 CloudFormation 模板中创建一个计划任务(CloudWatch Events 规则),它具有以下 EcsParameters:

EcsParameters:
LaunchType: FARGATE
NetworkConfiguration:
AwsVpcConfiguration:
AssignPublicIp: !Ref PublicIpAssignment
SecurityGroups:
- !Ref EcsSecurityGroups
Subnets:
- !Ref SubnetName
TaskCount: 1
TaskDefinitionArn: !Ref TaskDefinitionOne

我的 ECS CLuster 在 Fargate 而不是 EC2 上启动,并且我没有运行服务(用例不需要长时间运行的进程,直接从事件规则调度任务。)

每当我运行此模板(使用 LaunchTypeNetworkConfiguration )时,堆栈创建都会失败,并出现以下错误:

Encountered unsupported property NetworkConfiguration



作为替代方案,我还尝试从 AWS CLI 启动计划任务,但似乎网络配置和启动类型选项在那里也不可用:

Parameter validation failed: Unknown parameter in Targets[0].EcsParameters: "LaunchType", must be one of: TaskDefinitionArn, TaskCount



根据 this page在 AWS 文档本身上,我应该能够指定 LaunchTypeNetworkConfiguration在我的 EcsParameters Targets 中的部分在 PropertiesAWS::Events::Rule资源。

有什么我可以尝试的方法吗?

最佳答案

CloudFormation 尚未 catch 将 Fargate 任务作为 CloudWatch Events 规则的直接目标运行所需的参数。同时,您可以通过让规则以运行 Fargate 任务的 Lambda 函数为目标来实现相同的结果。

为此,事件规则将需要 lambda:InvokeFunction Lambda 函数的权限,并且 Lambda 函数将需要 ecs:RunTaskiam:PassRole适当资源的权限(除了 AWSLambdaBasicExecutionRole 中的常用日志权限)。

编辑 :这是一个示例 CF 模板,显示了我在说什么。 (它是从我们使用的东西拼凑和简化的,所以没有经过测试,但希望能说明这个过程。)

Parameters:
#ClusterName
#Subnets
#SecurityGroups
#CronExpression
#TaskDefinitionArn
#TaskRoleArn
#ExecutionRoleArn

Resources:
FargateLauncherRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-FargateLauncher-${AWS::Region}
AssumeRolePolicyDocument:
Statement:
-
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Path: /

FargateLauncherPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: !Sub ${AWS::StackName}-FargateLauncher-${AWS::Region}
PolicyDocument:
Version: 2012-10-17
Statement:
-
Sid: RunTaskAccess
Effect: Allow
Action:
- ecs:RunTask
Resource: '*'
-
Sid: PassRoleAccess
Effect: Allow
Action:
- iam:PassRole
Resource:
# whatever you have defined in your TaskDefinition, if any
- !Ref TaskRoleArn
- !Ref ExecutionRoleArn
Roles:
- !Ref FargateLauncherRole

FargateLauncher:
Type: AWS::Lambda::Function
DependsOn: FargateLauncherPolicy
Properties:
Environment:
Variables:
CLUSTER_NAME: !Ref ClusterName
SUBNETS: !Ref Subnets
SECURITY_GROUPS: !Ref SecurityGroups
Handler: index.handler
Role: !GetAtt FargateLauncherRole.Arn
Runtime: python3.6
Code:
ZipFile: |
from os import getenv
from boto3 import client
ecs = client('ecs')

def handler(event, context):
ecs.run_task(
cluster=getenv('CLUSTER_NAME'),
launchType='FARGATE',
taskDefinition=event.get('taskDefinition'),
count=1,
platformVersion='LATEST',
networkConfiguration={'awsvpcConfiguration': {
'subnets': getenv('SUBNETS').split(','),
'securityGroups': getenv('SECURITY_GROUPS').split(','),
'assignPublicIp': 'DISABLED'
}})

Schedule:
Type: AWS::Events::Rule
Properties:
ScheduleExpression: !Sub "cron(${CronExpression})"
State: ENABLED
Targets:
-
Id: fargate-launcher
Arn: !GetAtt FargateLauncher.Arn
Input: !Sub |
{
"taskDefinition": "${TaskDefinitionArn}"
}

InvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref FargateLauncher
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt Schedule.Arn

我在我的集​​群堆栈中定义了 Lambda 函数,在那里我已经有了 ClusterName , Subnets , 和 SecurityGroups参数,并且可以将它们直接传递给 Lambda 环境。然后可以在一个或多个单独的堆栈中定义调度和调用权限,并传入 TaskDefinition通过 Lambda 函数的输入为每个任务。这样,您可以为每个集群使用一个 Lambda,但可以根据需要使用尽可能多的不同任务。您还可以将自定义命令字符串和/或其他容器覆盖添加到可以通过 overrides 传递的 Lambda 输入。 run_task 的参数.

编辑 #2 :
这是一个可以放入 CF 模板的 Fargate TaskDefinition 示例:
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: !Ref Family
Cpu: !Ref Cpu
Memory: !Ref Memory
NetworkMode: awsvpc
ExecutionRoleArn: !Ref ExecutionRoleArn
TaskRoleArn: !Ref TaskRoleArn
RequiresCompatibilities:
- FARGATE
ContainerDefinitions:
- Name: !Ref ContainerName
Essential: true
Image: !Ref Image
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref LogGroup
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: !Ref LogPrefix

关于amazon-web-services - 通过 cloudformation 为 fargate launchtype 任务为 cloudwatch 事件规则创建 'Target',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52208700/

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