简体   繁体   English

如何使用 Terraform 在现有 VPC 中启动 EC

[英]How to launch ECs in an existing VPC using Terraform

I need to create several new EC2, RDS, etc.using Terraform, in an existing AWS VPC.我需要在现有的 AWS VPC 中使用 Terraform 创建几个新的 EC2、RDS 等。 and the existing su.net, security group, iam, etc. they are not created by Terraform. it is created manually.还有现有的su.net、security group、iam等,不是Terraform创建的,是手动创建的。

I heard the right way is to use terraform import (it is correct?).我听说正确的方法是使用 terraform 导入(这是正确的吗?)。 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.为了测试 terraform 导入的工作原理,我首先测试了如何导入现有的 EC2 而不是现有的 VPC,因为我不想不小心更改现有 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:看来我需要在我的 ec2.tf 文件中创建一个非常详细的 EC2 资源,例如:

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,它显示我错过了 ami 和实例类型,

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.然后运行“terraform apply”会将我现有 EC2 的标签更改为无,将 iam 配置文件更改为无。

I have not tried how to import existing vpc, su.net, security group yet.我还没有尝试过如何导入现有的 vpc、su.net、安全组。 I am afraid if I try, I have to put a lot of information of the existing vpc, su.net, security group, etc. my system is complex.我怕试的话,要放很多已有的vpc、su.net、security group等的信息,我的系统比较复杂。

is it expected that I need to indicate so many details in my terraform code?我的 terraform 代码需要说明这么多细节吗? 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?有没有办法让我简单地指出现有东西的 id,比如 vpc 的 id,我的新东西将根据现有的 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}"
}

You can leave the body of the resource blank during the import, but you'll need to go back in and fill in the specific details once it's been imported.您可以在导入过程中将资源正文留空,但您需要在导入后返回并填写具体详细信息。 You can look at the imported resource with the terraform show command, and fill in all of the resource details, so when you try to run terraform plan it should show no changes needed.您可以使用 terraform show 命令查看导入的资源,并填写所有资源详细信息,因此当您尝试运行 terraform plan 时,它应该显示不需要更改。

But, to answer your question, yes you can use your existing resources without having to import them.但是,要回答您的问题,是的,您可以使用现有资源而无需导入它们。 Just create a variables file that holds your existing resource ids that you need for your new resources, and then you can then reference the ones you need.只需创建一个变量文件,其中包含新资源所需的现有资源 ID,然后您就可以引用所需的资源。

So you could have a .vars file with something like:因此,您可以拥有一个 .vars 文件,其中包含以下内容:

variable "ami_id" {
  description = "AMI ID"
  default = "ami-xxxxxxxx"
}

variable "subnet_prv1" {
  description = "Private Subnet 1"
  default = "subnet-xxxxxx"
}

Then in your main.tf to create the resource:然后在你的 main.tf 中创建资源:

resource "aws_instance" "example" {
   instance_type = "t2.micro"
   ami = "${var.ami_id}"
   ......
   subnet_id = "${var.subnet_prv1}"
}

Just one way to go about it.只是一种方法。 There are others, which you can read up on in the terraform docs for variables还有其他的,你可以在terraform docs 中阅读变量

Just use this with your vpc data只需将其与您的 vpc 数据一起使用

resource "aws_vpc" "main" {
  cidr_block = "XXXXXXXXXX"
}

then run cmd with existing vpc id:然后使用现有的 vpc id 运行 cmd:

$ terraform import aws_vpc.main VPC_ID

For using existing VPC you can create a new su.net and define the ip range for this su.net.要使用现有 VPC,您可以创建一个新的 su.net 并为此 su.net 定义 ip 范围。 For example, for VPC with cdir 10.10.0.0/16 we can do the next:例如,对于 cdir 10.10.0.0/16 的 VPC,我们可以执行以下操作:

resource "aws_subnet" "pre_exist_vpc" {
    vpc_id = "id_of_existing_vpc"
    cidr_block = "10.10.10.0/24"
}

resource "aws_instance" "test_ec2" {
    ami = "ami-033b95fb8079dc481"
    instance_type = "t2.micro"
    subnet_id = aws_subnet.exist_vpc.id
}

And the second option - using an existing su.net of necessary VPC:第二个选项 - 使用必要 VPC 的现有 su.net:

resource "aws_instance" "test_ec2" {
    ami = "ami-033b95fb8079dc481"
    instance_type = "t2.micro"
    subnet_id = "id of existing subnet"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM