2
votes

I am trying to build a VPC and subnet within that VPC. Thirdly I am trying to create an AWS instances within that subnet. Sounds simple, but the subnet_id parameter seems to break the terraform 'apply' (plan works just fine). Am I missing something?

Extract from main.tf

resource "aws_vpc" "poc-vpc" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "dedicated"
  enable_dns_hostnames = "true"
}

resource "aws_subnet" "poc-subnet" {
  vpc_id     = "${aws_vpc.poc-vpc.id}"
  cidr_block = "10.0.1.0/24"
  map_public_ip_on_launch = "true"
  availability_zone = "${var.availability_zone}"
}


resource "aws_instance" "POC-Instance" {
  ami = "${lookup(var.amis, var.region)}"
  instance_type = "${var.instance_type}"
  availability_zone = "${var.availability_zone}"
  associate_public_ip_address = true
  key_name = "Pipeline-POC-Key-Pair"
  vpc_security_group_ids = ["${aws_security_group.poc-sec-group.id}"]
  subnet_id = "${aws_subnet.poc-subnet.id}"
}

If I remove the subnet_id the 'apply' works, but the instance is created in my default VPC. This is not the aim.

Any help would be appreciated. I am a newbie to terraform so please be gentle.

4

4 Answers

3
votes

I worked this out and wanted to post this up to hopefully saves others some time.

The issue is the conflict of subnet_id in the aws_instance provisioner and instance_tennancy in the aws_vpc provisioner. Remove instance tenancy and all is fixed (or set to default)

The error message is meaningless. I've asked whether this can be improved.

2
votes

It's also possible that there is a conflict in your other configuration. I encountered the same Unsupported error because of different reason.

I used AMI ami-0ba5dfee72d5bb9a1 that I found from https://cloud-images.ubuntu.com/locator/ec2/ I just choose anything that is in the same region as my VPC.

Apparently that AMI can only support a* instance type and don't support t* or m* instance type.

So I think double check that:

  • Your AMI is compatible with your instance type.
  • Your AMI is in the same region as your VPC or subnet.
  • There is no other conflicting configuration.
1
votes

The problem is terraform example that you likely copied is WRONG.

The problem is vpc instance tenancy property in most cases.

Change the VPC instance_tenancy = "dedicated" to instance_tenancy = "default".

It should work for any ec2 instance type.

The reason is that dedicated instances are supported only for m5.large or bigger instances so VPC and ec2 instance types are in conflict if you are actually creating smaller instances like t3.small or t2.m. You can micro etc. You can look at the dedicated instances here.

https://aws.amazon.com/ec2/pricing/dedicated-instances/

0
votes

if you want to create your own VPC network and not use the default, then you need to also create a route table and internet gateway so you can have access to the created ec2. You will need to also add the follow config to create a full VPC network with ec2 instances that is accessible with the public IP you assigned

# Internet GW
resource "aws_internet_gateway" "main-gw" {
    vpc_id = "${aws_vpc.poc-vpc.id}"

    tags {
        Name = "poc-vpc"
    }
}

# route tables
resource "aws_route_table" "main-public" {
    vpc_id = "${aws_vpc.poc-vpc.id}"
    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = "${aws_internet_gateway.main-gw.id}"
    }

    tags {
        Name = "main route"
    }
}

# route associations public
resource "aws_route_table_association" "main-public-1-a" {
    subnet_id = "${aws_subnet.poc-subnet.id}"
    route_table_id = "${aws_route_table.main-public.id}"
}