3

Current Issue:

I am working with Terraform, and I am deploying AWS ECS with Fargate. I have all the applicable .tf files to do all of this, and they work, but I am also automating the deployment. So, first I would need to apply the ecr.tf file to create the ECR repo, then push the docker image to the repo, and then I get the repository_url attribute from the ECR resource to build the infrastructure for the ECS and Fargate. The issue I am having is that I have a deployment folder with all the .tf files, and I only want to apply my network/ecs/fargate files after I have set up my ecr resource by applying my ecr.tf and provider.tf. None of the things I have tried below are working so if anyone has any help they could provide that would be great.

Files:

root main.tf

variable "aws_region" {
  description = "Which region should the resources be deployed into?"
}

provider "aws" {
  shared_credentials_files = ["~/.aws/credentials"]
  shared_config_files = ["~/.aws/config"]
}

module "ecr" {
  source = "./ecr"
}

module "ecs" {
  source = "./ecs"
  aws_region = var.aws_region
  repository_url = module.ecr.repository_url
}

ecr.tf

resource "aws_ecr_repository" "ecr_repo" {
  name = "ecr-automation"
}

output "repository_url" {
  value = aws_ecr_repository.ecr_repo.repository_url
}

provider.tf

variable "aws_region" {
  description = "Which region should the resources be deployed into?"
}

provider "aws" {
  shared_credentials_files = *****
  shared_config_files = ****
}

Solutions I have tried:

  1. Using terraform apply -target=file. This didn't throw errors but the ECR repo was also not created.

  2. Create a main module with a main.tf file. Separate the ECR files and ECS files into their own separate modules, and include them in the main.tf file. Outputting the repository_url from the ecr.tf file, and accessing that from the main.tf file within the ecs module so that I can pass it to the files that need it. This is shown in my code example above, and is giving me the unsupported argument error. I assume I am doing it wrong, but I don't know how to fix it.

  • 1
    Do you have a variable `repository_url` defined in the ECS module? If not, that's what is missing. But some details are missing. Also, I would usually couple resources that work together in the same module. – Marko E Oct 26 '22 at 21:59
  • I don't because I thought that would prompt me to enter the URL in the command line. How can I assign the variable to the repo_url that is output from the aws_ecr? They were together prior, but I just don't understand how to make the ECR, and push the image, before making the ECR. – anothernewbiecoder Oct 26 '22 at 22:02
  • That actually fixed it! Now I can apply to one individual module. I am so excited! Thank you! – anothernewbiecoder Oct 26 '22 at 22:10

1 Answers1

1

try to use depends_on = [module.ecr] inside ecs module:

module "ecs" {
  source = "./ecs"
  aws_region = var.aws_region
  repository_url = module.ecr.repository_url
  depends_on = [module.ecr]
}

Note: Module support for depends_on was added in Terraform version 0.13, and prior versions can only use it with resources.

https://developer.hashicorp.com/terraform/language/meta-arguments/depends_on

Arunas Bartisius
  • 1,879
  • 22
  • 23