Search code examples
terraformamazon-ecsterraform-provider-awsterraform0.12+

aws_launch_configuration: "couldn't find resource" on terraform apply


I'm new to Terraform. I've tried everything I know to try. Google has not been helpful in this case.

I'm building a complex cloud infrastructure using Terraform. This includes an auto-scaled ECS service. In order to version-control, document and simplify the modification of this infrastructure, I chose to use Terraform.

I'm at 2 things to create from very, very many more things to create, and I've overcome many problems and learned a lot.

However, there's one problem I can't overcome:

resource "aws_launch_configuration" "ecs" {
  depends_on = [aws_security_group.ecs, aws_iam_instance_profile.ecs, aws_key_pair.production]
  name_prefix                 = "${var.ecs_cluster_name}-cluster-"
  image_id                    = lookup(var.amis, "us-east-2")
  instance_type               = "t2.micro"
  security_groups             = [aws_security_group.ecs.id]
  iam_instance_profile        = aws_iam_instance_profile.ecs.name
  key_name                    = aws_key_pair.production.key_name
  associate_public_ip_address = true
  user_data                   = "#!/bin/bash\necho ECS_CLUSTER='${var.ecs_cluster_name}-cluster' > /etc/ecs/ecs.config"
  provider = aws.us-east-2

  lifecycle {
    create_before_destroy = true
  }
}

Planning and applying this results in:

│ Error: couldn't find resource
│
│   with aws_launch_configuration.ecs,
│   on 08_ecs.tf line 6, in resource "aws_launch_configuration" "ecs":
│    6: resource "aws_launch_configuration" "ecs" {
│

What does this mean? What does it mean that the resource isn't found?

It's not the first time I've encountered this error message, but I've previously been able to solve it somehow. In this particular case, I'm at a loss, because nothing I've tried works.

Things I've tried:

  • Specifying depends_on
  • Consulted the Terraform docs on aws_launch_configuration
  • Googling the error message (no results related to this exact situation)

Seems like I'm the first to have this problem to me.

Version:

PS C:\Users\admin\PycharmProjects\my-project\terraform> terraform version
Terraform v1.2.1
on windows_amd64
+ provider registry.terraform.io/hashicorp/aws v4.20.1
+ provider registry.terraform.io/hashicorp/template v2.2.0

I honestly feel like Terraform doesn't give very useful feedback on errors. Something similar to a traceback would be nice, but all I'm getting is "Couldn't find resource," highlighting the "{" part of "resource "aws_launch_configuration" "ecs" {

Any ideas?


Solution

  • I was experimenting the same problem. I could solve it changing the AMI. In my case that solve everything. The error it does not specify that, but was enough to apply succesfully the resource.

    By setting the TF_LOG environment variable to "DEBUG", you can see that AWS returns a Bad Request when trying to describe the specified AMI. Thanks to @qaziqarta for this tip.