brikis98 / terraform-up-and-running-code

Code samples for the book "Terraform: Up & Running" by Yevgeniy Brikman
http://www.terraformupandrunning.com/
MIT License
2.83k stars 1.9k forks source link

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

Open adeomisor opened 1 year ago

adeomisor commented 1 year ago

I am having some errors with the code from Chapter 3 when we split the code to folders. I had written the code on chapter 3 and it worked well till I got to the "Isolation via File Layout". I was using us-east-1 region. I deleted app resources from the us-east-1, including s3 buckets and other resources

After splitting the code into different folders, for the webserver-cluster, I changed the region to us-east-2. I ran terraform init and it was successful.

Terraform plan also worked well. When I ran terraform apply, it failed at the launch configuration with this error.:

aws_launch_configuration: "couldn't find `resource"

I discovered that it is pulling data from my us-east-1. I changed my .aws credentials to use us-east-2 as my default region but still getting the same issue.

But the global/S3 runs smoothly.

I created new folders and wrote a new code there and I am now getting this error:

**Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically use this backend unless the backend configuration changes. Error refreshing state: state data in S3 does not have the expected content.

This may be caused by unusually long delays in S3 processing a previous state update. Please wait for a minute or two and try again. If this problem persists, and neither S3 nor DynamoDB are experiencing an outage, you may need to manually verify the remote state and update the Digest value stored in the DynamoDB table to the following value:**

``

brikis98 commented 1 year ago

I am having some errors with the code from Chapter 3 when we split the code to folders. I had written the code on chapter 3 and it worked well till I got to the "Isolation via File Layout". I was using us-east-1 region. I deleted app resources from the us-east-1, including s3 buckets and other resources

Did you run terragrunt destroy?

After splitting the code into different folders, for the webserver-cluster, I changed the region to us-east-2. I ran terraform init and it was successful.

Terraform plan also worked well. When I ran terraform apply, it failed at the launch configuration with this error.:

aws_launch_configuration: "couldn't find `resource"

I discovered that it is pulling data from my us-east-1. I changed my .aws credentials to use us-east-2 as my default region but still getting the same issue.

If you didn't run destroy, then you still have state pointing at us-east-1. Switch your code back to us-east-1, run destroy, then finally switch to us-east-2.

But the global/S3 runs smoothly.

I created new folders and wrote a new code there and I am now getting this error:

**Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically use this backend unless the backend configuration changes. Error refreshing state: state data in S3 does not have the expected content.

This may be caused by unusually long delays in S3 processing a previous state update. Please wait for a minute or two and try again. If this problem persists, and neither S3 nor DynamoDB are experiencing an outage, you may need to manually verify the remote state and update the Digest value stored in the DynamoDB table to the following value:**

``

S3 buckets live in a specific region too. Did you switch the region in your code to us-east-2, but not actually re-create the bucket in that region?

kevstewa commented 1 year ago

@brikis98 @adeomisor Launch configurations are no longer supported by AWS. You need to use a Launch Template. I've included an example below:

resource "aws_launch_template" "example" {
  image_id        = "ami-083eed19fc801d7a4"
  instance_type   = "t2.micro"
  vpc_security_group_ids = [aws_security_group.instance.id]

  user_data = base64encode(templatefile("user-data.sh", {
    server_port = var.server_port
    db_address = data.terraform_remote_state.db.outputs.address
    db_port = data.terraform_remote_state.db.outputs.port
  }))

  # Required when using a launch configuration with an auto scaling group.
  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "example" {
  launch_template {
    id = aws_launch_template.example.id
  }

  vpc_zone_identifier  = data.aws_subnets.default.ids

  target_group_arns = [aws_lb_target_group.asg.arn]
  health_check_type = "ELB"

  min_size = 2
  max_size = 10

  tag {
    key                 = "Name"
    value               = "${var.cluster_name}-asg"
    propagate_at_launch = true
  }
}