I need to create several new EC2, RDS, etc.using Terraform, in an existing AWS VPC. and the existing subnet, security group, iam, etc. they are not created by Terraform. it is created manually.
I heard the right way is to use terraform import (it is correct?). To test how terraform import works, I first tested how to import an existing EC2 in stead of an existing VPC, Because I do not want to accidentally change anything In an exist VPC.
before running
terraform import aws_instance.example i-XXXXXXXXXX
It looks like I need to create a very detailed EC2 resource in my ec2.tf file, such as:
resource "aws_instance" "example" {
iam_instance_profile = XXXXXXXXXX
instance_type = XXXXXXX
ami = XXXXXXX
tags {
Name = XXXXX
Department = XXXX
....
}
}
if I just write:
resource "aws_instance" "example" {
}
it showed I missed ami and instance type,
if I write:
resource "aws_instance" "example" {
instance_type = XXXXXXX
ami = XXXXXXX
}
then running "terraform apply" will change tags of my existing EC2 to nothing, change iam profile to nothing.
I have not tried how to import existing vpc, subnet, security group yet. I am afraid if I try, I have to put a lot of information of the existing vpc, subnet, security group, etc. my system is complex.
is it expected that I need to indicate so many details in my terraform code? isn't there a way so that I just simply indicate the id of existing stuff like vpc's id, and my new stuff will be created based on the existing id? sth. like:
data "aws_subnet" "public" {
id = XXXXXXX
}
resource "aws_instance" "example" {
instance_type = "t2.micro"
ami = "${var.master_ami}"
......
subnet_id = "${aws_subnet.public.id}"
}