brikis98 / terraform-up-and-running-code

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

Book code chapter 2 fails on apply #118

Open CliffRoberson opened 5 days ago

CliffRoberson commented 5 days ago

I copied and pasted the book code into three files and ran the following commands and the apply failed. Error at the end was about the auto scaling launch configuration. Googling it looks like this is deprecated. What should I use instead?

PS C:\Users\cliff\Desktop\TerraformUpAndRunning\Chp2> terraform init Initializing the backend... Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.

If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. PS C:\Users\cliff\Desktop\TerraformUpAndRunning\Chp2> terraform apply data.aws_vpc.default: Reading... data.aws_vpc.default: Read complete after 1s [id=vpc-0118d7f9580308e88] data.aws_subnets.default: Reading... data.aws_subnets.default: Read complete after 0s [id=us-east-2]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

aws_autoscaling_group.example will be created

Plan: 8 to add, 0 to change, 0 to destroy.

Changes to Outputs:

Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.

Enter a value: yes

aws_lb_target_group.asg: Creating... aws_security_group.instance: Creating... aws_security_group.alb: Creating... aws_lb_target_group.asg: Creation complete after 1s [id=arn:aws:elasticloadbalancing:us-east-2:043309363086:targetgroup/terraform-asg-example/c7b53e0db5b9c21a] aws_security_group.instance: Creation complete after 2s [id=sg-06de222277c569059] aws_launch_configuration.example: Creating... aws_security_group.alb: Creation complete after 3s [id=sg-043099eacdec032e5] aws_lb.example: Creating... aws_lb.example: Still creating... [10s elapsed] aws_lb.example: Still creating... [20s elapsed] aws_lb.example: Still creating... [30s elapsed] aws_lb.example: Still creating... [40s elapsed] aws_lb.example: Still creating... [50s elapsed] aws_lb.example: Still creating... [1m0s elapsed] aws_lb.example: Still creating... [1m10s elapsed] aws_lb.example: Still creating... [1m20s elapsed] aws_lb.example: Still creating... [1m30s elapsed] aws_lb.example: Still creating... [1m40s elapsed] aws_lb.example: Still creating... [1m50s elapsed] aws_lb.example: Still creating... [2m0s elapsed] aws_lb.example: Still creating... [2m10s elapsed] aws_lb.example: Still creating... [2m20s elapsed] aws_lb.example: Still creating... [2m30s elapsed] aws_lb.example: Still creating... [2m40s elapsed] aws_lb.example: Still creating... [2m50s elapsed] aws_lb.example: Creation complete after 2m52s [id=arn:aws:elasticloadbalancing:us-east-2:043309363086:loadbalancer/app/terraform-asg-example/d1a655f2186fdd51] aws_lb_listener.http: Creating... aws_lb_listener.http: Creation complete after 0s [id=arn:aws:elasticloadbalancing:us-east-2:043309363086:listener/app/terraform-asg-example/d1a655f2186fdd51/7b44a3efd8898dbc] aws_lb_listener_rule.asg: Creating... aws_lb_listener_rule.asg: Creation complete after 0s [id=arn:aws:elasticloadbalancing:us-east-2:043309363086:listener-rule/app/terraform-asg-example/d1a655f2186fdd51/7b44a3efd8898dbc/632188ec1b6c47bb] ╷ │ Error: creating Auto Scaling Launch Configuration (terraform-20241113002735313500000001): UnsupportedOperation: The Launch Configuration creation operation is not available in your account. Use launch templates to create configura tion templates for your Auto Scaling groups. │ status code: 400, request id: 5ad8d31c-9281-49b1-857e-f979a388e989 │ │ with aws_launch_configuration.example, │ on main.tf line 16, in resource "aws_launch_configuration" "example": │ 16: resource "aws_launch_configuration" "example" { │ ╵

brikis98 commented 4 days ago

Looks like as of October 1, 2024, AWS no longer supports using launch configurations for new accounts: https://docs.aws.amazon.com/autoscaling/ec2/userguide/launch-configurations.html.

That means you need to replace the aws_launch_configuration resource with the aws_launch_template resource. It'll look something like this:

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

  user_data = base64encode(
    <<-EOF
    #!/bin/bash
    echo "Hello, World" > index.html
    nohup busybox httpd -f -p ${var.server_port} &
    EOF
  )
}

And to use it with the auto scaling group:

resource "aws_autoscaling_group" "example" {
  launch_template {
    id      = aws_launch_template.example.id
    version = "$Latest"
  }

  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               = "terraform-asg-example"
    propagate_at_launch = true
  }
}