Terraform: Example in getting started guide fails because t2.micro requires a VPC

Created on 17 Dec 2015  ·  32Comments  ·  Source: hashicorp/terraform

The example in the getting started guide fails because the t2.micro instance requires a VPC and no VPC is specified in the example. I worked around by reverting to t1.micro and the ami listed in this version of the doc.

 ~/hack/terraform-test    terraform apply
aws_instance.example: Creating...
  ami:                      "" => "ami-d05e75b8"
  availability_zone:        "" => "<computed>"
  ebs_block_device.#:       "" => "<computed>"
  ephemeral_block_device.#: "" => "<computed>"
  instance_type:            "" => "t2.micro"
  key_name:                 "" => "<computed>"
  placement_group:          "" => "<computed>"
  private_dns:              "" => "<computed>"
  private_ip:               "" => "<computed>"
  public_dns:               "" => "<computed>"
  public_ip:                "" => "<computed>"
  root_block_device.#:      "" => "<computed>"
  security_groups.#:        "" => "<computed>"
  source_dest_check:        "" => "1"
  subnet_id:                "" => "<computed>"
  tenancy:                  "" => "<computed>"
  vpc_security_group_ids.#: "" => "<computed>"
Error applying plan:

1 error(s) occurred:

* aws_instance.example: Error launching source instance: VPCResourceNotSpecified: The specified instance type can only be used in a VPC. A subnet ID or network interface ID is required to carry out the request.
    status code: 400, request id:

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.
documentation provideaws

Most helpful comment

It is really sad when the first example in a getting started guide doesn't work.

All 32 comments

@WillAbides do you mean on this page? https://terraform.io/intro/getting-started/build.html

@stack72 Yes, that's the page I meant. Although after going through the rest of the getting started documentation I had to keep substituting the old images for the ones on the current page.

Hey @WillAbides sorry for that, I changed them all recently in https://github.com/hashicorp/terraform/pull/4330 to try and update the docs, but I clearly overlooked this situation (my account has a default VPC). I'll get this cleaned up

interesting that we're hitting this at almost the exact same time. I checked the PR above, but it seemed to be changing things to use ami-d05e75b8 with t2.micro, which doesn't work for me. I switched to m3.large and didn't have any problems. What is the suggested way to launch a t2.micro?

@donwb as the subject of this issue states t2 instances must be launched into a VPC. So, to launch one you need to create or specify a VPC, Subnet, etc which isn't included in the tutorial. I think you're just running into this issue now because the docs recently changed. Maybe the old m1.small instance types will work for you...

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/t2-instances.html

Just hit this today

I ran into this exact same issue while trying to get started with terraform. I corrected the issue by switching to an m3.medium

resource "aws_instance" "example" {
  ami           = "ami-408c7f28"
  instance_type = "m3.medium"
}

The error message

Error applying plan:

1 error(s) occurred:

* aws_instance.example: Error launching source instance: VPCResourceNotSpecified: The specified instance type can only be used in a VPC. A subnet ID or network interface ID is required to carry out the request.
        status code: 400, request id: 38266bc0-f95d-4fc3-a0bf-ebd9a11b0623

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

Attempting to use the above:

Example Resource

resource "aws_instance" "example" {
ami = "ami-408c7f28"
instance_type = "m3.medium"
}
fails for me...
terraform apply
aws_instance.example: Creating...
ami: "" => "ami-408c7f28"
associate_public_ip_address: "" => ""
availability_zone: "" => ""
ebs_block_device.#: "" => ""
ephemeral_block_device.#: "" => ""
instance_state: "" => ""
instance_type: "" => "m3.medium"
key_name: "" => ""
network_interface_id: "" => ""
placement_group: "" => ""
private_dns: "" => ""
private_ip: "" => ""
public_dns: "" => ""
public_ip: "" => ""
root_block_device.#: "" => ""
security_groups.#: "" => ""
source_dest_check: "" => "true"
subnet_id: "" => ""
tenancy: "" => ""
vpc_security_group_ids.#: "" => ""
Error applying plan:

1 error(s) occurred:

  • aws_instance.example: Error launching source instance: InvalidAMIID.NotFound: The image id '[ami-408c7f28]' does not exist
    status code: 400, request id: 22a474f8-47e9-4a9a-8740-8e745c77f2da

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

This is using the latest: Terraform v0.7.10

Images have different _iam_ codes for each region. Per example, Ubuntu 16.04 in São Paulo-BR has code _ami-7379e31f_ and on Virgina-US has code _ami-6edd3078_ for the same image. Then if you try launch a image from São Paulo(_ami-7379e31f_) in Virginia, won't work.

Same issue. The tutorial is not clear.

any resolution to this? I have the same problem.

There's no easy to fix this - it's a moving target of instance types and AMIs - is the existing note not sufficient? I think it's pretty clear but open to ideas to make it clearer.

sorry, am new to this, but which existing note are you referring to? This link gives 404 error
https://github.com/hashicorp/terraform/blob/28d39cd0bc7e9212eeea77f871cf4d4c7df0da73/docs/providers/index.html

Or, because old aws accounts doesn't have a default VPC, just use the subnet_id inside aws_instance resource:

resource "aws_instance" "example" {
    ami           = "ami-2757f631"
    instance_type = "t2.micro"
    subnet_id     = "subnet-123abc12"
}

You must create a VPC with that subnet before.

Oh, and use vpc = true on the next page:

resource "aws_eip" "ip" {
    vpc      = true
    instance = "${aws_instance.example.id}"
}

It is really sad when the first example in a getting started guide doesn't work.

I was able to work around the issue by adding a security group ID and subnet ID. Basically, this:
````
provider "aws" {
region = "us-west-2"
}

resource "aws_instance" "bogus1" {
ami = "ami-04c14c64"
instance_type = "t2.micro"
}
````

becomes this:
````
provider "aws" {
region = "us-west-2"
}

resource "aws_instance" "example" {
ami = "ami-04c14c64"
instance_type = "t2.micro"
vpc_security_group_ids = [""]
subnet_id = ""
}
````

...obviously, replacing my_xxxxxx values with your actual subnet and security group ids

So, for documentation, you may want to add an outset that mentions "If you don't have a default VPC created, you may want to also add these two elements to your resource... blah, blah, blah'

This is still... not really working. I've run into this myself just now. The AMI listed in the example doesn't work and reports back the whole...

==> amazon-ebs: Error launching source instance: VPCResourceNotSpecified: The specified instance type can only be used in a VPC. A subnet ID or network interface ID is required to carry out the request.
==> amazon-ebs:         status code: 400, request id: bdad1b26-c82b-4e13-8c1d-2053dc523c25
==> amazon-ebs: No volumes to clean up, skipping
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' errored: Error launching source instance: VPCResourceNotSpecified: The specified instance type can only be used in a VPC. A subnet ID or network interface ID is required to carry out the request.
        status code: 400, request id: bdad1b26-c82b-4e13-8c1d-2053dc523c25

The below worked for me, but requires creating a VPC and subnet:

provider "aws" {
  access_key = "ACCESS_KEY_HERE"
  secret_key = "SECRET_KEY_HERE"
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami = "ami-2757f631"
  instance_type = "t2.micro"
  subnet_id = "${aws_subnet.us-east-1a-public.id}"
}

resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support = true
}

resource "aws_subnet" "us-east-1a-public" {
  vpc_id = "${aws_vpc.example.id}"
  cidr_block = "10.0.1.0/25"
  availability_zone = "us-east-1a"
}

@sinzin91's comment fixed it for me. Really not great when the very first example fails, eh?

I just hit today, and I believe docs needs to be updated for non VPC users with;

resource "aws_instance" "example" {
  ami           = "ami-408c7f28"
  instance_type = "t1.micro"
}

I also just hit this issue today. I changed instance type to t1.micro and everything worked as usual. Docs still need updating.

MadOtis comment helped med get past this. But someone should really have a look at the getting starting docs.
I get that learning by failing is a thing, but maybe let people get started before we start failing and flailing? ;)

the issue is more than 3 years old and it still has not been fixed :-1:

I'm in region ap-southeast2 and I had to change it to

resource "aws_instance" "example" {
ami = "ami-0c9d48b5db609ad6e"
instance_type = "t2.micro"
}

might be good to get this info right, for first-time users.

that issue just hit me

that issue just hit me

Same, just hit me June 29, 2019. 3.5 years after issue opened. Granted, it forced me to learn about VPC dependencies, but as others have said, this example should be updated in the documentation.

@jamtur01 As @MadOtis and others suggested, a gentle reminder that you may need to create the VPC/subnet/security group before the example works would be nice, perhaps with a link to an example like the one @sinzin91 posted that creates said resources. I think people are still getting stuck on this point... not sure why my default VPC wasn't already up to par, but I had to do manual steps. Thanks for otherwise great docs.

PS. It turns out my AWS is old enough that it includes EC2 classic capabilities, so default VPC is not available without having customer support intervene. So if the docs mentioned older accounts need the vpc creation step, rather than default vpc, that would be helpful. Some folks new to terraform might also be new to this limitation as per this thread.

Hitting this exact problem mid 2019 - why is this issue closed??
(t2 -> t2.micro was the sole fix required here)

I think the best solution is: aws-console -> new support issue -> "Regarding: Account and Billing Support" -> "Service: Account" -> "Category: Convert EC2 Classic to VPC".

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings