Terraform 0.12.13 - 无法旋转 AWS 实例

Terraform 0.12.13 - Not able to spin AWS instance

在 aws_instance 部分创建网络接口时,AWS EC2 实例创建失败。配置遵循 Terraform 网络接口中定义的配置 配置。

删除网络块后,配置将无缝运行。使用网络块记录了以下错误

"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}"
  }

}

问题是由于 Terraform 版本。以下是支持 Terraform V.0.12.16 以在 AWS 上创建 EC2 实例的更新脚本。

// 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