0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I checked that, you dont need to give vpc_id instead you give subnet_id and vpc will pick automatically..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.