Avoid ecs task definition revision during deploy stage of codepipeline

0

I have created the ecs resources like cluster, task definition and service etc., through terraform. I do a deployment with the codepipeline, in last stage of pipeline it deploys the newer version of application with ecs action stage and in this stage it's creating the newer version of the task definition even if there is no change. Is there a way to skip the creation of newer version of task definition and just do a force deployment of service

We do tag the image with "latest" tag

There problem is every time it creates a new version and terraform does not track that new version and it show as a change I want to avoid this case

1 Answer
1

Hi Follow These steps to resolve issue

Approach 1: Using a Fixed Task Definition

One way to force a deployment without creating a new task definition is to reuse the same task definition revision. You can achieve this by updating the service to use the same task definition revision and force a new deployment. This can be done through the AWS CLI or SDK, but here’s how to integrate it into a CodePipeline setup:

  • Create a Lambda Function: This function will update the ECS service to force a new deployment using the same task definition revision.
  • Invoke the Lambda Function in CodePipeline: Add an invocation of this Lambda function in your CodePipeline.

Approach 2: Manage Task Definitions with Terraform

Another approach is to manage task definitions explicitly with Terraform and ensure that CodePipeline only updates the service with the new task definition registered by Terraform. Here's how you can do it:

  • Register the Task Definition with Terraform: Use Terraform to register the task definition and manage its versions.
  • Update the Service with CodePipeline: Modify CodePipeline to update the ECS service to use the new task definition version created by Terraform.
profile picture
EXPERT
Sandeep
answered 4 months ago
EXPERT
reviewed 4 months ago
  • Hi Sandeep, Not able to completely understand the second approach. We are using amazon ecs as the provider for deploy stage.

  • Here's a simplified version of how you can manage task definitions in Terraform:

    resource "aws_ecs_task_definition" "my_task" { family = "my_task_family" container_definitions = jsonencode(local.container_definitions) requires_compatibilities = ["FARGATE"] cpu = "256" memory = "512" network_mode = "awsvpc" execution_role_arn = var.execution_role_arn task_role_arn = var.task_role_arn }

    resource "aws_ecs_service" "my_service" { name = "my_service" cluster = aws_ecs_cluster.my_cluster.id task_definition = aws_ecs_task_definition.my_task.arn desired_count = 1 launch_type = "FARGATE" network_configuration { subnets = var.subnets security_groups = var.security_groups } }

    By managing the task definition with Terraform, you ensure that changes are tracked and controlled by Terraform. You can then update your ECS service in CodePipeline to use the latest task definition revision created by Terraform.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions