Ahmed Omar
Ahmed Omar

Reputation: 33

Terraform with ECS :Invalid arn syntax

i receive this error when running terraform apply ( i deploy a container using ecs task which connect to rds with terraform )


Error: creating ECS Task Definition (project_task): ClientException: Invalid arn syntax.
│
│   with module.ecs.aws_ecs_task_definition.project_task,
│   on modules/ecs/main.tf line 37, in resource "aws_ecs_task_definition" "project_task":
│   37: resource "aws_ecs_task_definition" "project_task" {
│

as seen from the main.tf i declared the execution rule

data "aws_ecr_repository" "project_ecr_repo" {
  name = "project-ecr-repo"
}

resource "aws_ecs_cluster" "project_cluster" {
  name = "project-cluster"
}

data "aws_iam_policy_document" "ecs_task_execution_role" {
  version = "2012-10-17"
  statement {
    sid = ""
    effect = "Allow"
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["ecs-tasks.amazonaws.com"]
    }
  }
}

# ECS task execution role
resource "aws_iam_role" "ecs_task_execution_role" {
  name               = "ecs_task_execution_role"
  assume_role_policy = "${data.aws_iam_policy_document.ecs_task_execution_role.json}"
}

# ECS task execution role policy attachment
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role" {
  role       = "${aws_iam_role.ecs_task_execution_role.name}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}



resource "aws_ecs_task_definition" "project_task" {
  family = "project_task"
  container_definitions = file("container_def.json")
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  memory                   = 512
  cpu                      = 256
  execution_role_arn       = aws_iam_role.ecs_task_execution_role.arn
}

resource "aws_ecs_service" "project_service" {
  name            = "project-service"
  cluster         = aws_ecs_cluster.project_cluster.id
  task_definition = aws_ecs_task_definition.project_task.arn
  launch_type     = "FARGATE"
  desired_count   = 2

  network_configuration {
    subnets          = var.vpc.public_subnets
    assign_public_ip = true
  }
}

and here is my container definition file

[
        {
            "name": "backend_feed",
            "image": "639483503131.dkr.ecr.us-east-1.amazonaws.com/backend-feed:latest",
            "cpu": 256,
            "memory": 512,
            "portMappings": [
                {
                    "containerPort": 8080,
                    "hostPort": 8080,
                    "protocol": "tcp"
                }
            ],
            "environmentFiles": [
                {
                    "value": "https://myawsbucket-639483503131.s3.amazonaws.com/env_vars.json",
                    "type": "s3"
                }
            ]
        }
]

appreciate your help

Thank you

terrafrom apply -auto-approve

expected to create ecs task with the provided container specs

Upvotes: 1

Views: 569

Answers (1)

Mark B
Mark B

Reputation: 200476

Your environmentFiles value is a web URL, while ECS expects an S3 object ARN. Also, the documentation says the environment file must have a .env extension.

So first you need to rename env_vars.json to env_vars.env, and the file can't be JSON format, it has to be in the format of one VARIABLE=VALUE per line.

Then you need to specify the environmentFiles value property as an ARN:

"value": "arn:aws:s3:::myawsbucket-639483503131/env_vars.env"

Upvotes: 2

Related Questions