为什么我的Lambda不允许在ECS资源上运行Task



我刚刚创建了一个AWS ECS集群和任务定义,并运行得很好。我能够连接到服务器。该任务正在Fargate上运行,并按需运行。我现在正试图创建一个Lambda,它将运行RunTask命令来启动服务器。这是我在Terraform中对Lambda的定义。

data "aws_iam_policy_document" "startup_lambda_assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}   
}
}
resource "aws_iam_role" "startup_lambda" {
name = "report_lambda_role"
assume_role_policy = data.aws_iam_policy_document.startup_lambda_assume_role.json
}
resource "aws_cloudwatch_log_group" "startup_lambda" {
name = "/aws/lambda/${aws_lambda_function.startup.function_name}"
retention_in_days = 14
}
data "aws_iam_policy_document" "startup_lambda" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:CreateLogGroup",
]
resources = [aws_cloudwatch_log_group.startup_lambda.arn]
}
statement {
effect = "Allow"
actions = ["logs:PutLogEvents"]
resources = ["${aws_cloudwatch_log_group.startup_lambda.arn}:*"]
}
statement {
effect = "Allow"
actions = [
"ecs:RunTask",
]
resources = [
aws_ecs_task_definition.game.arn
]
}
statement {
effect = "Allow"
actions = [
"iam:PassRole",
]
resources = [
aws_iam_role.ecs_task_execution.arn,
aws_iam_role.game_task.arn
]
}
}
resource "aws_iam_role_policy" "startup_lambda" {
name = "startup_lambda_policy"
policy = data.aws_iam_policy_document.startup_lambda.json
role = aws_iam_role.startup_lambda.id
}
data "archive_file" "startup_lambda" {
type = "zip"
source_file = "${path.module}/startup/lambda_handler.py"
output_path = "${path.module}/startup/lambda_handler.zip"
}
resource "aws_lambda_function" "startup" {
function_name = "startup_lambda"
filename = data.archive_file.startup_lambda.output_path
handler = "lambda_handler.handler"
source_code_hash =  data.archive_file.startup_lambda.output_base64sha256
runtime = "python3.8"
role = aws_iam_role.startup_lambda.arn
environment {
variables = {
CLUSTER_ARN = aws_ecs_cluster.game.arn,
TASK_ARN = aws_ecs_cluster.game.arn,
SUBNET_IDS = "${aws_subnet.subnet_a.id},${aws_subnet.subnet_b.id},${aws_subnet.subnet_c.id}"
}
}
}

这是我位于startup/lambda_handler.py中的Python代码,当我在AWS控制台中检查时,它确实正确地显示为函数的代码。

import os
import boto3
def handler (event, callback):
client = boto3.client("ecs")
response = client.run_task(
cluster = os.getenv("CLUSTER_ARN"),
taskDefinition = os.getenv("TASK_ARN"),
launchType = "FARGATE",
count = 1,
networkConfiguration = {
"awsvpcConfiguration": {
"subnets": os.getenv("SUBNET_IDS", "").split(","),
"assignPublicIp": "ENABLED",
},
},
)

当我在控制台中使用一个空的JSON对象作为参数运行Lambda函数的测试时,我希望看到我的ECS任务启动,但我得到了以下错误。

Response
{
"errorMessage": "An error occurred (AccessDeniedException) when calling the RunTask operation: User: arn:aws:sts::703606424838:assumed-role/report_lambda_role/startup_lambda is not authorized to perform: ecs:RunTask on resource: * because no identity-based policy allows the ecs:RunTask action",
"errorType": "AccessDeniedException",
"stackTrace": [
"  File "/var/task/lambda_handler.py", line 6, in handlern    response = client.run_task(n",
"  File "/var/runtime/botocore/client.py", line 386, in _api_calln    return self._make_api_call(operation_name, kwargs)n",
"  File "/var/runtime/botocore/client.py", line 705, in _make_api_calln    raise error_class(parsed_response, operation_name)n"
]
}

请注意,在我的Lambda附带的IAM政策文件中,我的任务定义中确实允许使用ecs:RunTask的声明。我不知道为什么这不允许Lambda运行任务。

传递给lambda容器的TASK_ARN是错误的。可能应该是aws_ecs_task_definition.game.arn,而不是重复的aws_ecs_cluster.game.arn

相关内容

  • 没有找到相关文章

最新更新