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

2nd chapter code AMI does no longer exist #68

Open aanzolaavila opened 3 years ago

aanzolaavila commented 3 years ago

On https://github.com/brikis98/terraform-up-and-running-code/blob/master/code/terraform/02-intro-to-terraform-syntax/webserver-cluster/main.tf, the AMI for the instances does no longer exist and fails, and I don't know what other image would contain busybox for trying out the HTTP requests

I was trying out ami-0c55b159cbfafe1f0, but it does not have busybox installed.

brikis98 commented 3 years ago

I'm pretty sure ami-0c55b159cbfafe1f0 still exists, but AMIs are region-specific, so that's the ID solely for the us-east-2 region. Make sure that's the region you pick!

aanzolaavila commented 3 years ago

I was using us-east-1, but I think I found a better solution for me on https://learn.hashicorp.com/tutorials/terraform/cloud-init, in which the main.tf file in the instances folder contains this config (modified a little bit by me), which will get the latest ubuntu image without having to write a specific AMI, which for me is a better solution that is independent from the region as you pointed out.

resource "aws_launch_configuration" "example" {
  image_id = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"
  security_groups = [aws_security_group.instance.id, aws_security_group.ssh.id]

  user_data = data.template_file.user_data.rendered

  lifecycle { 
    create_before_destroy = true
  }
}

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-*20*-amd64-server-*"]
  }

  filter {
    name = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}
brikis98 commented 3 years ago

Yup, keep reading, we use the aws_ami data source later in the book :)

Mr-Slippery commented 3 years ago

In case you are also stuck on trying the web server on a different region, this is how to find out the right AMI Id for your region. Search the given AMI Id on region us-east-2 and find the AMI Name (which might be worth mentioning in a possible future edition of the book too): image And then switch to the region you deploy to and use the same search interface but search for the AMI Name: ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20190212.1 Hope this helps.

brikis98 commented 3 years ago

See also: https://blog.gruntwork.io/locating-aws-ami-owner-id-and-image-name-for-packer-builds-7616fe46b49a

DasLeo commented 2 years ago

I just used the latest Ubuntu 20.04 and installed Busybox in the script:

resource "aws_instance" "example" {
  ami = "ami-05f7491af5eef733a"
  instance_type = "t2.micro"
  vpc_security_group_ids = [ aws_security_group.instance.id ]
  tags = {
    "Name" = "terraform-example"
  }
  user_data = <<-EOF
              #!/bin/bash
              sudo apt-get update -y
              sudo apt-get install -y busybox
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p 8080 &
              EOF
}
mikerossiter commented 2 years ago

Hi guys. I'm in a similar situation. I have tried these suggestions but the web server doesn't seem to want to start. Any ideas?

Gang-Peng commented 6 months ago

I cannot find that image (ami-0c55b159cbfafe1f0) anymore in us-east-2. The closest one is "ami-03a5def6b0190cef7". image

temutai commented 1 day ago

I cannot find that image (ami-0c55b159cbfafe1f0) anymore in us-east-2. The closest one is "ami-03a5def6b0190cef7". image

Pick any available free tier Ubuntu instance. Also it doesn't have to be us-east-2. I did it in us-east-1