1

I have defined an "aws_ecs_task_definition" resource as defined below. And "aws_ecs_task_definition" has all the details required for our ECS to perform a blue/green deployment.


resource "aws_ecs_task_definition" "ecs-task-definition" {
  container_definitions    = data.template_file.ecs_task_definition_template.rendered
  family                   = "${var.project_name}-${var.infra_env}-backend"
  cpu                      = var.fargate_cpu
  memory                   = var.fargate_memory
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  execution_role_arn       = aws_iam_role.ecs_tasks_execution_role.arn
  task_role_arn            = var.task_iam_role_arn
}

data "template_file" "ecs_task_definition_template" {
  template = file("${path.module}/task-definitions/task_definition.json")
  vars = {
    task_definition_name  = "${var.project_name}-${var.infra_env}-${var.container_name}"
    ecs_service_name      = "${var.project_name}-${var.infra_env}-service"
    docker_image_url      = data.aws_ecr_repository.emc_backend_repo.repository_url
    memory                = var.fargate_memory
    docker_container_port = "8000"
    region                = var.region
    message               = "Hi, Stack!!"
  }

Problem i am facing is that my codepipleline doesn't recognize this task definition. I had to manually add a task definition file(taskdef.json) to my codecommit repository and reference in the deploy stage of my codepipeline terraform like below.

 stage {
    name = "Deploy"
    action {
      name     = "Deploy"
      category = "Deploy"
      owner    = "AWS"
      version  = "1"
      //Enable ECS Blue/Green deployment
      provider        = "CodeDeployToECS"
      run_order       = 1
      input_artifacts = ["BuildOutput"]
      configuration = {
        ApplicationName     = var.code_deploy_app_name
        DeploymentGroupName = var.code_deploy_deployment_group_name
        TaskDefinitionTemplateArtifact = "BuildOutput"
        TaskDefinitionTemplatePath     = "taskdef.json"
        AppSpecTemplateArtifact = "BuildOutput"
        AppSpecTemplatePath     = "appspec.yml"
        Image1ArtifactName      = "BuildOutput"
        Image1ContainerName     = "IMAGE1_NAME"
      }

Basically i dont want add "taskdef.json" in my codecommit repo but instead refer the task definition i created as a resource. I want to know how can i do that and how can i let my codepipeline deploy to stage to refer the task definition from resource instead of a physical file in codecommit?

Abhi.G
  • 1,801
  • 5
  • 20
  • 35
  • One thing i have found is that you can use the replacement string in your `appspec.json|yaml` during codepipeline in order to run the latest ECS registered task definition. See https://docs.aws.amazon.com/codepipeline/latest/userguide/tutorials-ecs-ecr-codedeploy.html#tutorials-ecs-ecr-codedeploy-taskdefinition – Mr Hash Feb 18 '22 at 13:22
  • did you manage to solve this ? – change198 Mar 20 '22 at 16:26

0 Answers0