6

I am using terraform to provision the resources required.

I have a terraform codepipeline resource and the Production stage reads the imagedefinitions.json file to know which images to deploy:

resource "aws_codepipeline" "pipeline" {
  stage {
    name = "Build"
    action {
      name             = "Build"
      category         = "Build"
      owner            = "AWS"
      provider         = "CodeBuild"
      version          = "1"
      input_artifacts  = ["source"]
      output_artifacts = ["imagedefinitions"]
      configuration {
        ProjectName = "${var.project_prefix}-codebuild"
      }
    }
  }
 stage {
    name = "Production"
    action {
      name            = "Deploy"
      category        = "Deploy"
      owner           = "AWS"
      provider        = "ECS"
      input_artifacts = ["imagedefinitions"]
      version         = "1"
      configuration {
        ClusterName = "${var.ecs_cluster_name}"
        ServiceName = "${var.ecs_service_name}"
        FileName    = "imagedefinitions.json"
      }
    }
  }

The imagedefinitions.json file is built during the build phase, from buildspec.yml:

build:
  commands:
    - echo Build started on 'date'
    - echo Building the Docker image...
    - docker build -t $REPOSITORY_URI:latest .
    - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
post_build:
  commands:
    - echo Build completed on 'date'
    - echo Pushing the Docker images...
    - docker push $REPOSITORY_URI:latest
    - docker push $REPOSITORY_URI:$IMAGE_TAG
    - echo Writing image definitions file...
    - printf '[{"name":"docker-image-name","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json  

I am not sure what the name of the image should be in this line:

  • printf '[{"name":"docker-image-name","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json

The error I get repeats the "name" value from this line:

"Invalid action configuration The AWS ECS container docker-image-name does not exist"

What is the name I should have in here?

Jamie Starke
  • 8,776
  • 3
  • 38
  • 58
edzillion
  • 3,592
  • 5
  • 31
  • 50

2 Answers2

18

It looks like you're using the Continuous Deployment with AWS CodePipeline tutorial. From Prerequisites, you should have a task definition, and a service that uses the task definition.

When the ECS Deploy step in the CodePipeline runs, it looks up the task definition for the service you specify, creates a new task definition where it updates the container with the same name as the one in your imagedefinition.json file.

So from your example, I would expect the Task Definition associated with the ECS Service your pipeline is updating to have a container with the name docker-image-name.

Jamie Starke
  • 8,776
  • 3
  • 38
  • 58
  • thanks! I was reading a tutorial that had taken some of that code, but not explained it. It was much better reading the original. I had a different `name` in the `task_definition.json` than the `name` I was writing to the `imagedefinitions.json` file. – edzillion Oct 08 '18 at 09:47
  • 1
    This was exactly my problem. I had named my container "app" in the TaskDefinition, but when I was creating the imagedefinition,json file from CodeBuild, it was using the container name "api" as I coped that code over from another project. Thanks mate – Dalmiro Granas Jan 30 '21 at 16:37
0

You have created an ERC repo and you have up uploaded an image to it? This might help, I saw this which looks kinda similiar to a part of what you are trying to achieve: https://github.com/aws-samples/amazon-ecs-catsndogs-workshop/blob/master/Lab-6-Artifacts/dogs/buildspec.yml

OpenBSDNinja
  • 1,037
  • 10
  • 18