0
votes

I am trying to create a instance on AWS with Terraform with existing resources

Below is my varibale.tf file

variable "aws_vpc" {
  description = "VPC ID"
  default = "vpc-1234567b"
}

variable "subnet_prv1" {
  description = "Subnet ID"
  default = "subnet-1234567"
}

Below is my ec2.tf file

provider "aws" {
  access_key = "SDSFFDGRTYUYIJKH"
  secret_key = "sfdfKSFE3546/34sfsfSFDFSF89dv"
  region     = "ap-south-1"
}

resource "aws_instance" "terra-jd" {
  ami           = "ami-1234567"
  instance_type = "t2.micro"
  vpc_id        = "${var.aws_vpc}"
  subnet_id     = "${var.subnet_prv1}"
  key_name      = "AWS-IntIGRATION-APAC"
  security_groups = "MYORG-INT-Ser-Cluster-SG"
  tags {
  Name = "terra-jd-nirvana"
  }
}

When I try to run terraform apply it gives me error

Error: aws_instance.terra-jd: : invalid or unknown key: vpc_id

VPC, SUBNET, Security groups , key name they all are already existing, when try to spin instance with existing resources it gives error , how to correct it?

1

1 Answers

1
votes

The aws_instance resource doesn't take a vpc_id parameter at all so the error is pretty self explanatory.

Setting the subnet_id infers the VPC ID because a subnet can only live in a single VPC.