1

I have slightly changed An introduction to Terraform code. My goal is to deploy cluster of web servers My code,main.tf

provider "aws" { region = "eu-central-1"}

resource "aws_launch_configuration" "example" {
    ami = "ami-df8406b0"
    image_id      = "${data.aws_ami.ubuntu.id}"
    instance_type = "t2.micro"
    vpc_security_group_ids = ["${aws_security_group.instance.id}"]

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

lifecycle {
    create_before_destroy = true
}   
}

variable "server_port" {
  description = "The port the server will use for HTTP requests"
  default = 8080
}


resource "aws_security_group" "instance" {
 name = "terraform-example-instance"
 ingress {
   from_port = "${var.server_port}"
   to_port = "${var.server_port}"
   protocol = "tcp"
   cidr_blocks = ["0.0.0.0/0"]
        }

lifecycle {
    create_before_destroy = true
} 

}

data "aws_ami" "ubuntu" {
  most_recent = true

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

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

  owners = ["099720109477"] # Canonical
}


resource "aws_autoscaling_group" "example" {
    launch_configuration = "${aws_launch_configuration.example.id}"
    min_size = 2
    max_size = 10

    tag {
        key = "Name"
        value = "terraform-asg-example"
        propagate_at_launch = true
    }
}

When I go for terraform plan

2 error(s) occurred:

* aws_launch_configuration.example: : invalid or unknown key: ami
* aws_launch_configuration.example: : invalid or unknown key: vpc_security_group_ids

I am a little bit confused with data,it serves for what? Where is my mistake?

3

The error is that you have specified two arguments to a aws_launch_configuration resource which aren't valid; namely ami and vpc_security_group_ids, see the documentation for what's valid.

I suspect you've changed from using a single aws_instance resource to an aws_launch_configuration resource, however they don't use the same arguments.

  • I have taken a look at documentatiom,still I do not understand how to specify my ami?Yes,that is true that I have changed from single instance. – MikiBelavista Jan 3 '18 at 16:15
  • The howto you linked to shows you how to do this. See the "Deploy a cluster of web servers" section and its first code section. You need to remove those two invalid arguments from the resource before Terraform will even try and create your infrastructure. – bodgit Jan 3 '18 at 16:29
  • 1
    @MikiBelavista aws_instance takes ami, where aws_launch_configuration takes image_id. They are different, in large part because this functionality was added very early in the Terraform project, and done before they had much consistency between resource-types. – sysadmin1138 Jan 3 '18 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.