Search code examples
amazon-web-servicesamazon-iamterraformamazon-ecs

Terraform: ECS service - InvalidParameterException


I am trying to provision an ECS cluster with terraform, everything seems to work well up until I am creating the ecs service:

resource "aws_ecs_service" "ecs-service" {
  name            = "ecs-service"
  iam_role        = "${aws_iam_role.ecs-service-role.name}"
  cluster         = "${aws_ecs_cluster.ecs-cluster.id}"
  task_definition = "${aws_ecs_task_definition.my_cluster.family}"
  desired_count   = 1

  load_balancer {
    target_group_arn  = "${aws_alb_target_group.ecs-target-group.arn}"
    container_port    = 80
    container_name    = "my_cluster"
  }
}

and the IAM role is:

resource "aws_iam_role" "ecs-service-role" {
  name = "ecs-service-role"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}
EOF
}

resource "aws_iam_role_policy_attachment" "ecs-service-role-attachment" {
    role       = "${aws_iam_role.ecs-service-role.name}"
    policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole"
}

I am getting the following error message:

  • aws_ecs_service.ecs-service: 1 error(s) occurred:

  • aws_ecs_service.ecs-service: InvalidParameterException: Unable to assume role and validate the specified targetGroupArn. Please verify that the ECS service role being passed has the proper permissions.


Solution

  • In assume_role_policy, can you change the "Principal" line to as mentioned below: You are having ec2.amazonaws.com.

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Action": "sts:AssumeRole",
          "Principal": {
            "Service": "ecs.amazonaws.com"
          },
          "Effect": "Allow",
          "Sid": ""
        }
      ]
    }