0

I'm using aws and creating resources using terraform. I made changes in resource aws_lauch_configuration and applied usign terraform.

Tf code is

resource "aws_launch_configuration" "app" {                                     
  name_prefix = var.ecs_launch_configuration_name_prefix                        
  security_groups = [                                                           
    aws_security_group.instance_sg.id,                                          
  ]                                                                             
                                                                                
  key_name                    = aws_key_pair.deployer.key_name                  
  image_id                    = var.ecs_launch_configuration_image_id           
  instance_type               = var.ecs_launch_configuration_instance_type      
  iam_instance_profile        = aws_iam_instance_profile.app.name               
  associate_public_ip_address = false                                           
                                                                                
  user_data = data.template_file.user_data.rendered                             
                                                                                
  lifecycle {                                                                   
    create_before_destroy = true                                                
  }                                                                             
}

Terraform is showing that first it will create that resource then destroy previously as create_before_destroy is set as true. So after creating resource, teraform is being failed to delete older one. So I deleted that manually from aws console then ran terraform script again.

Now it's showing that deposed object will be destroyed.

# aws_launch_configuration.app (deposed object e9636964) will be destroyed
  - resource "aws_launch_configuration" "app" {
      - arn                              = "arn:aws:autoscaling:ap-south-1:989263488711:launchConfiguration:5c662048-79b0-4158-861a-df6f86d0641e:launchConfigurationName/staging-launch-config-20210723120615775100000001" -> null
      - associate_public_ip_address      = false -> null
      - ebs_optimized                    = false -> null
      - enable_monitoring                = true -> null
      - iam_instance_profile             = "staging-ecs-instance-profile" -> null
      - id                               = "staging-launch-config-20210723120615775100000001" -> null
      - image_id                         = "ami-070ea05cb21034fd4" -> null
      - instance_type                    = "t3.micro" -> null
      - key_name                         = "deployer_key" -> null
      - name                             = "staging-launch-config-20210723120615775100000001" -> null
      - name_prefix                      = "staging-launch-config-" -> null
      - security_groups                  = [
          - "sg-0efdc5b4e5e0d5381",
        ] -> null
      - user_data                        = "62fa54f43cbd35da831f418a9b62474d81b16795" -> null
      - vpc_classic_link_security_groups = [] -> null
    }

After terraform apply, it's throwing error as error deleting Autoscaling Launch Configuration (staging-launch-config-20210723120615775100000001): ValidationError: Launch configuration name not found - Launch configuration staging-launch-config-20210723120615775100000001 not found status code: 400, request id: 12e7aa05-f3b1-4d75-a5a5-2093df3e5838

I understand that this error will be thrown as the resource doesn't exist on aws (deleted manually). I tried terraform apply -refresh=true but it didn't work. Please help me out to resolve this error.

ROHIT BANSAL
  • 57
  • 1
  • 1
  • 8
  • What happens if you run `terraform state rm aws_launch_configuration.app["e9636964"]`? Then run `terraform plan` again after that to see if it still plans on deleting that resource. Before even doing that, you might want to inspect the state file to see how terraform is identifying/tracking the deposed resource. – Zachary Delano Sep 02 '21 at 17:46
  • After running the command `terraform state rm aws_launch_configuration.app["e9636964"]`, terraform still tries to delete the resource. Then I created same resource manually on aws then terraform is working. – ROHIT BANSAL Nov 11 '21 at 12:36

0 Answers0