0
votes

instance Configuration Network Configuration

AWS EC2 instance creation is failing while creating a network interface in the aws_instance section. The configuration is following configuration as defined in Terraform Network Interfaces Configuration.

On removing the network block the configuration works seamlessly. With network block the following error was logged

"Error: Error launching source instance: Unsupported: The requested configuration is currently not supported. Please check the documentation for supported configurations."

variable "aws_region" {}
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "vpc_cidr_block" {}
variable "environment" {}
variable "applicationtype" {}
variable "subnet_cidr_block" {}
variable "amiid" {}
variable "instancetype" {}
variable "bucketname" {}
variable "publickey-fe" {}
variable "publickey-be" {}

provider "aws" {
  profile    = "default"
  region     = "${var.aws_region}"
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
}

data "aws_availability_zones" "availability" {
  state = "available"
}

resource "aws_vpc" "sitespeed_vpc" {
  cidr_block       = "${var.vpc_cidr_block}"
  instance_tenancy = "dedicated"
  tags = {
    env         = "${var.environment}"
    application = "${var.applicationtype}"
    Name        = "site-speed-VPC"
  }
}

resource "aws_subnet" "sitespeed_subnet" {
  vpc_id     = "${aws_vpc.sitespeed_vpc.id}"
  cidr_block = "${var.subnet_cidr_block}"
  availability_zone = "${data.aws_availability_zones.availability.names[0]}"

  tags = {
    env         = "${var.environment}"
    application = "${var.applicationtype}"
    Name        = "site-speed-Subnet"
  }
}

resource "aws_network_interface" "sitespeed_frontend_NIC" {
  subnet_id   = "${aws_subnet.sitespeed_subnet.id}"
  private_ips = ["192.168.10.100"]

  tags = {
    env         = "${var.environment}"
    application = "${var.applicationtype}"
    Name        = "site-speed-frontend-nic"
  }
}

resource "aws_network_interface" "sitespeed_backend_NIC" {
  subnet_id   = "${aws_subnet.sitespeed_subnet.id}"
  private_ips = ["192.168.10.110"]

  tags = {
    env         = "${var.environment}"
    application = "${var.applicationtype}"
    Name        = "site-speed-backend-nic"
  }
}

resource "aws_key_pair" "sitespeed_front_key" {
  key_name   = "site_speed_front_key"
  public_key = "${var.publickey-fe}"
}

resource "aws_key_pair" "sitespeed_back_key" {
  key_name   = "site_speed_back_key"
  public_key = "${var.publickey-be}"
}
resource "aws_instance" "sitespeed_front" {
  ami           = "ami-00942d7cd4f3ca5c0"
  instance_type = "t2.micro"
  key_name      = "site_speed_front_key"
  availability_zone = "${data.aws_availability_zones.availability.names[0]}"

  network_interface {
    network_interface_id = "${aws_network_interface.sitespeed_frontend_NIC.id}"
    device_index = 0
  }

  tags = {
    env         = "${var.environment}"
    application = "${var.applicationtype}"
    Name        = "site-speed-frontend-server"
    public      = "yes"  
  }
}

resource "aws_instance" "sitespeed_backend" {
  ami           = "ami-00942d7cd4f3ca5c0"
  instance_type = "t2.micro"
  key_name      = "site_speed_back_key"

  network_interface {
    network_interface_id = "${aws_network_interface.sitespeed_backend_NIC.id}"
    device_index         = 0
  }
  tags = {
    env         = "${var.environment}"
    application = "${var.applicationtype}"
    Name        = "site-speed-backend-server"
    public      = "No"
  }


} 
resource "aws_s3_bucket" "b" {
  bucket = "${var.bucketname}"
  acl    = "private"

  tags = {
    env         = "${var.environment}"
    application = "${var.applicationtype}"
  }

}
1
It is highly recommended to post the code, not as an image. - Lamanus
Your private ip address, is that really starts with 192? - Lamanus
@Lamanus yes, and it can be assigned to the EC2 instances. - Ankit Mehta
@Lamanus acknowledged, will update the with code / gist link - Ankit Mehta
I didn't have a problem running that combination in 0.12.13. In the code/gist you'll add can you include the aws_subnet.sitespeed_subnet resource? - Carlo Mencarelli

1 Answers

0
votes

The issue was due to the Terraform Version. Following is the updated script that supports Terraform V.0.12.16 to create an EC2 Instance on AWS.

// Variable Definition
variable "aws_region" {}
variable "aws_vpc_cidr_block" {}
variable "aws_subnet_cidr_block" {}
variable "aws_private_ip_fe" {}
variable "aws_Name" {}
variable "aws_Application" {}
variable "aws_ami" {}
variable "aws_instance_type" {}

// Provider Definition
provider "aws" {
  version = "~> 2.40"
  region  = var.aws_region
}

// Adds a VPC
resource "aws_vpc" "aws_ec2_deployment_test-vpc" {
  cidr_block = var.aws_vpc_cidr_block

  tags = {
    Name        = join("-", [var.aws_Name, "vpc"])
    Application = var.aws_Application
  }
}

//Adds a subnet
resource "aws_subnet" "aws_ec2_deployment_test-subnet" {
  vpc_id            = aws_vpc.aws_ec2_deployment_test-vpc.id
  cidr_block        = var.aws_subnet_cidr_block
  availability_zone = join("", [var.aws_region, "a"])

  tags = {
    Name        = join("-", [var.aws_Name, "subnet"])
    Application = var.aws_Application
  }
}

//Adds a Network Interface
resource "aws_network_interface" "aws_ec2_deployment_test-fe" {
    subnet_id = aws_subnet.aws_ec2_deployment_test-subnet.id
    private_ips = [ var.aws_private_ip_fe ]

    tags = {
    Name        = join("-", [var.aws_Name, "network-interface-fe"])
    Application = var.aws_Application
  }

}
//Adds an EC2 Instance 
resource "aws_instance" "aws_ec2_deployment_test-fe"{
    ami = var.aws_ami
    instance_type = var.aws_instance_type

    network_interface {
        network_interface_id = aws_network_interface.aws_ec2_deployment_test-fe.id
        device_index = 0
    }

    tags = {
    Name        = join("-", [var.aws_Name, "fe-ec2"])
    Application = var.aws_Application
  }
}


// Print Output Values
output "aws_ec2_deployment_test-vpc" {
  description = "CIDR Block for the VPC: "
  value       = aws_vpc.aws_ec2_deployment_test-vpc.cidr_block
}

output "aws_ec2_deployment_test-subnet" {
  description = "Subnet Block: "
  value       = aws_subnet.aws_ec2_deployment_test-subnet.cidr_block
}

output "aws_ec2_deployment_test-private-ip" {
  description = "System Private IP: "
  value       = aws_network_interface.aws_ec2_deployment_test-fe.private_ip
}

output "aws_ec2_deployment_test-EC2-Details" {
  description = "EC2 Details: "
  value       = aws_instance.aws_ec2_deployment_test-fe.public_ip
}

Gist link to the solution