6

I have got an AWS account that already has ECR and IAM ready. I am now creating a new environment using terraform modules. But I could not find a way to import the existing IAM and ECR resources to my modules. When I run the command terraform import aws_ecr_repository.c2m_an c2m_an, I am getting an error as

Error: resource address "aws_ecr_repository.c2m_cs" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "aws_ecr_repository" "c2m_cs" {
  # (resource arguments)
}

My ECR module definition is as follows:

resource "aws_ecr_repository" "c2m_cs" {
  name = var.c2m_cs#"c2m_cs"
}

output "c2m_cs" {
  value = "terraform import aws_ecr_repository.c2m_cs c2m_cs"
}

And in my main.tf file withtin my environment folder, I have module definition as follows:

module "ecr" {
  source = "../modules/ecr"
  c2m_cs = module.ecr.c2m_cs
}
Alla Sasikanth
  • 541
  • 3
  • 7
  • 22

1 Answers1

8

To correct way to import a resource into the module is exemplified in the docs:

terraform import module.foo.aws_instance.bar i-abcd1234

Thus in your case it would be:

terraform import module.aws_ecr_repository.c2m_an c2m_an 
Marcin
  • 215,873
  • 14
  • 235
  • 294