Claire Herdeman
11/16/2022, 3:33 PMClaire Herdeman
11/16/2022, 3:35 PMresource "aws_ecs_task_definition" "prefectAgent" {
  family = "prefectAgent_${var.env}"
  requires_compatibilities =  ["FARGATE"]
  network_mode = "awsvpc"
  cpu = "512"
  memory = "1024"
  task_role_arn = aws_iam_role.prefect_agent_ecs_task_role.arn
  execution_role_arn = aws_iam_role.prefect_agent_ecs_execution_role.arn
  container_definitions = <<TASK_DEFINITION
  [
    {
        "name": "prefectAgent_${var.env}",
        "image": "${data.aws_caller_identity.current.account_id}.dkr.ecr.${data.aws_region.current.name}.<http://amazonaws.com/integrations_${var.env}:latest|amazonaws.com/integrations_${var.env}:latest>",
        "entryPoint": ["bash", "-c"],
        "stopTimeout": 120,
        "environment": [
                {"name": "PREFECT_LOGGING_LEVEL", "value": "INFO"}
        ],
        "command": ["prefect agent start -q ${var.env}"],
        "logConfiguration": {
            "logDriver": "awslogs",
            "options": {
                "awslogs-group": "${aws_cloudwatch_log_group.prefect_agent.name}",
                "awslogs-region": "us-east-2",
                "awslogs-stream-prefix": "prefect_agent"
            }
        },
        "secrets": [
            {"Name": "PREFECT_API_KEY", "ValueFrom": "${data.aws_secretsmanager_secret_version.prefect_api.arn}:PREFECT_API_KEY::"},
            {"Name": "PREFECT_API_URL", "ValueFrom": "${data.aws_secretsmanager_secret_version.prefect_api.arn}:PREFECT_API_URL::"}
        ]
    }
  ]
  TASK_DEFINITION
}
# Agent Service
resource "aws_ecs_service" "prefectAgentService" {
    name = "prefectAgentService_${var.env}"
    cluster = data.aws_ecs_cluster.internet_cluster.id
    task_definition = aws_ecs_task_definition.prefectAgent.arn
    desired_count = 1
    launch_type = "FARGATE"
    network_configuration {
        subnets = data.aws_subnets.app_subnets.ids
        security_groups = data.aws_security_groups.app_security_groups.ids
    }
}Claire Herdeman
11/16/2022, 3:50 PMFailed to get infrastructure for flow. I'm not seeing any other detail in the logs produced by the agent or the flow run itself.
I develop on an EC2 instance, when I spin up an agent there and deploy to it, the flow runs and succeeds. I've got a couple potential culprits as to why it fails on the agent as a service, but I haven't been able to nail down the issue yet:
• IAM permissions for one of the roles applied to the agent, perhaps the task role? I added "ec2:DescribeSecurityGroups" since SGs are required for us
• Updating the agent image to one that has the prefect-ecs package loaded (saw that on another thread and that change is made here, did not resolve the issue)
• The agent and the flow tasks current run in different ECS clusters, it didn't seem to resolve the issue when I deployed them in the same one 
Other thoughts/suggestions here?Mason Menges
11/16/2022, 5:08 PMClaire Herdeman
11/16/2022, 5:17 PMMason Menges
11/16/2022, 5:19 PMPREFECT_LOGGING_LEVEL='DEBUG' on the agents environment and see if that turns up anything extra when this runs? it might help provide some more contextClaire Herdeman
11/16/2022, 5:30 PMMason Menges
11/16/2022, 5:39 PMClaire Herdeman
11/16/2022, 5:42 PMresource "aws_iam_role" "prefect_agent_ecs_task_role" {
    name = "prefect_agent_task_role_${var.env}"
    force_detach_policies = true
    assume_role_policy = jsonencode({
        Version = "2012-10-17"
        Statement = [
            {
                Action = "sts:AssumeRole"
                Effect = "Allow"
                Sid    = ""
                Principal = {
                    Service = "<http://ecs-tasks.amazonaws.com|ecs-tasks.amazonaws.com>"
                }
            },
        ]
    })
    inline_policy {
        name = "PrefectS3Storage"
        policy = jsonencode({
            Version = "2012-10-17"
            Statement = [
                {
                    Action   = [
                        "s3:ListAllMyBuckets"
                    ]
                    Effect   = "Allow"
                    Resource = "arn:aws:s3:::*"
                },{
                    Action   = [
                        "s3:ListBucket",
                        "s3:GetBucketLocation"
                    ]
                    Effect   = "Allow"
                    Resource = "arn:aws:s3:::${aws_s3_bucket.augintel-prefect-flows.bucket}"
                },{
                    Action   = [
                        "s3:ListBucket",
                        "s3:GetBucketLocation"
                    ]
                    Effect   = "Allow"
                    Resource = "arn:aws:s3:::${aws_s3_bucket.augintel-prefect-flows.bucket}"
                },{
                    Action   = [
                        "s3:PutObject",
                        "s3:PutObjectAcl",
                        "s3:GetObject",
                        "s3:GetObjectAcl",
                        "s3:DeleteObject",
                        "kms:Decrypt",
                        "kms:Encrypt",
                        "kms:GenerateDataKey"
                    ]
                    Effect   = "Allow"
                    Resource = [
                        "arn:aws:s3:::${aws_s3_bucket.augintel-prefect-flows.bucket}/*",
                        aws_kms_key.prefect-flow-key.arn
                    ] 
                },{
                    Action   = [
                        "ecs:RegisterTaskDefinition",
                        "ecs:DeregisterTaskDefinition",
                        "ecs:DescribeTasks",
                        "ecs:RunTask"
                    ]
                    Effect   = "Allow"
                    Resource = "*"
                },{
                    Action   = [
                        "logs:GetLogEvents"
                    ]
                    Effect   = "Allow"
                    Resource = "*"
                },{
                    Action   = [
                        "ec2:DescribeSubnets",
                        "ec2:DescribeVpcs",
                        "ec2:DescribeSecurityGroups",
                        "ecs:DescribeClusters"
                    ]
                    Effect   = "Allow"
                    Resource = "*"
                },
                {
                    Action   = [
                        "ssmmessages:CreateControlChannel",
                        "ssmmessages:CreateDataChannel",
                        "ssmmessages:OpenControlChannel",
                        "ssmmessages:OpenDataChannel"
                    ]
                    Effect   = "Allow"
                    Resource = "*"
                }
            ]
        })
    }
}Zanie
Claire Herdeman
11/16/2022, 7:15 PMZanie
Claire Herdeman
11/16/2022, 7:29 PMClaire Herdeman
11/16/2022, 7:31 PMClaire Herdeman
11/16/2022, 7:33 PMClaire Herdeman
11/16/2022, 7:45 PMClaire Herdeman
11/16/2022, 9:34 PMZanie
Claire Herdeman
11/16/2022, 10:18 PMZanie
Zanie
Zanie
Claire Herdeman
11/16/2022, 11:07 PMClaire Herdeman
11/16/2022, 11:08 PMZanie