I am trying to fix this piece of code and I would like to create bastion host but I am receiving: Error: Error applying plan:
1 error occurred: * aws_instance.bastion: 1 error occurred: * aws_instance.bastion: Error launching source instance: VPCIdNotSpecified: No default VPC for this user status code: 400, request id: daf3dd12-d7c4-469a-9be5-3707c0490c2a
I am using Terraform 0.11
Code:
resource "aws_vpc" "main" {
cidr_block = "${var.main_vpc_cidr}"
instance_tenancy = "default"
enable_dns_support = true
enable_dns_hostnames = true
tags {
Name = "main"
}
}
resource "aws_subnet" "subnet1" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.1.0/24"
availability_zone = "${var.availability_zone1}"
tags {
Name = "app-subnet-1"
}
}
########## Create bastion host ######
##resource "aws_default_vpc" "default" {}
resource "aws_instance" "bastion" {
ami = "ami-0fdc6293d3e87a86e"
key_name = "${aws_key_pair.bastion_key.key_name}"
instance_type = "t2.micro"
security_groups = ["${aws_security_group.bastion-sg.name}"]
associate_public_ip_address = true
}
resource "aws_security_group" "bastion-sg" {
name = "bastion-security-group"
vpc_id = "${aws_vpc.main.id}"
ingress {
protocol = "tcp"
from_port = 22
to_port = 22
cidr_blocks = ["0.0.0.0/0"]
}
egress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_key_pair" "bastion_key" {
key_name = "your_key_name"
public_key = "ssh-rsa AAA
output "bastion_public_ip" {
value = "${aws_instance.bastion.public_ip}"
}
variables.tf file:
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "aws_region" {
description = "EC2 Region for the VPC"
default = "eu-west-1"
}
variable "availability_zone1" {
description = "Avaialbility Zones"
default = "eu-west-1a"
}
variable "availability_zone2" {
description = "Avaialbility Zones"
default = "eu-west-1b"
}
variable "main_vpc_cidr" {
description = "CIDR of the VPC"
default = "10.0.0.0/16"
}