如何将 terraform import 与传入的变量一起使用?
How to use terraform import with passed-in variables?
我正在学习 Terraform,并想使用该工具设置 AWS 基础设施。
我们有 3 个 AWS 环境,沙盒、暂存和生产,并且有现有的基础设施来支持这些环境。例如,我们为每个环境设置了 3 个独立的 VPC。
我想根据我尝试设置的环境使用 terraform import
导入这些资源的状态。所以我基本上想这样做,虽然我知道这在语法上不正确,但你明白了。
$ terraform import aws_vpc.my_vpc -var 'environment=sandbox'
因此我的模块设置如下
vpc/main.tf
-----------
provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "my_vpc" {
cidr_block = ""
}
vpc/variables.tf
----------------
variable "environment" {
type map = map(string)
default {
sandbox = "vpc-1234"
staging = "vpc-2345"
production = "vpc-3456"
}
}
所以这意味着我基本上想做
$ terraform import aws_vpc.my_vpc vpc-1234
我怎样才能做到这一点?
仅供参考
The terraform import command is used to import existing
infrastructure.
The command currently can only import one resource at a time. This
means you can't yet point Terraform import to an entire collection of
resources such as an AWS VPC and import all of it. This workflow will
be improved in a future version of Terraform import usage.
您传递的这些将被视为后续模块的输入变量,而不是从环境变量文件中获取值,而是为变量设置值。
Input variables serve as parameters for a Terraform module, allowing
aspects of the module to be customized without altering the module's
own source code, and allowing modules to be shared between different
configurations. configuration variables
导入语法:
terraform import aws_instance.example i-abcd1234
This command locates the AWS instance with ID i-abcd1234 and attaches
its existing settings, as described by the EC2 API, to the name
aws_instance.example in the Terraform state.
所以使用变量处理导入的方法可以是系统环境变量,
由于ec2可以导入,所以本例以使用变量导入实例为主。
instance.tf
provider "aws" {
region = "us-west-2"
profile = "test"
}
resource "aws_instance" "ec2" {
ami = "ami-0f2176987ee50226e"
instance_type = "t2.micro"
associate_public_ip_address = false
subnet_id = "subnet-example"
vpc_security_group_ids = ["sg-example"]
key_name = "mytest-ec2key"
tags = {
Name = "Test EC2 Instance"
}
}
要将变量传递给上述脚本,您可以使用系统环境变量。
export stage_instance=i-abcexample && terraform import aws_instance.ec2 $stage_instance
或者你也可以试试
export stage_instance=abc && export prod_instance=abc
然后尝试导入
terraform import aws_instance.ec2 $stage_instance
我遇到了同样的问题,发现顺序很重要。
此命令有效:
$ terraform import -var 'environment=sandbox' aws_vpc.my_vpc vpc-1234
我正在学习 Terraform,并想使用该工具设置 AWS 基础设施。
我们有 3 个 AWS 环境,沙盒、暂存和生产,并且有现有的基础设施来支持这些环境。例如,我们为每个环境设置了 3 个独立的 VPC。
我想根据我尝试设置的环境使用 terraform import
导入这些资源的状态。所以我基本上想这样做,虽然我知道这在语法上不正确,但你明白了。
$ terraform import aws_vpc.my_vpc -var 'environment=sandbox'
因此我的模块设置如下
vpc/main.tf
-----------
provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "my_vpc" {
cidr_block = ""
}
vpc/variables.tf
----------------
variable "environment" {
type map = map(string)
default {
sandbox = "vpc-1234"
staging = "vpc-2345"
production = "vpc-3456"
}
}
所以这意味着我基本上想做
$ terraform import aws_vpc.my_vpc vpc-1234
我怎样才能做到这一点?
仅供参考
The terraform import command is used to import existing infrastructure.
The command currently can only import one resource at a time. This means you can't yet point Terraform import to an entire collection of resources such as an AWS VPC and import all of it. This workflow will be improved in a future version of Terraform import usage.
您传递的这些将被视为后续模块的输入变量,而不是从环境变量文件中获取值,而是为变量设置值。
Input variables serve as parameters for a Terraform module, allowing aspects of the module to be customized without altering the module's own source code, and allowing modules to be shared between different configurations. configuration variables
导入语法:
terraform import aws_instance.example i-abcd1234
This command locates the AWS instance with ID i-abcd1234 and attaches its existing settings, as described by the EC2 API, to the name aws_instance.example in the Terraform state.
所以使用变量处理导入的方法可以是系统环境变量, 由于ec2可以导入,所以本例以使用变量导入实例为主。
instance.tf
provider "aws" {
region = "us-west-2"
profile = "test"
}
resource "aws_instance" "ec2" {
ami = "ami-0f2176987ee50226e"
instance_type = "t2.micro"
associate_public_ip_address = false
subnet_id = "subnet-example"
vpc_security_group_ids = ["sg-example"]
key_name = "mytest-ec2key"
tags = {
Name = "Test EC2 Instance"
}
}
要将变量传递给上述脚本,您可以使用系统环境变量。
export stage_instance=i-abcexample && terraform import aws_instance.ec2 $stage_instance
或者你也可以试试
export stage_instance=abc && export prod_instance=abc
然后尝试导入
terraform import aws_instance.ec2 $stage_instance
我遇到了同样的问题,发现顺序很重要。 此命令有效:
$ terraform import -var 'environment=sandbox' aws_vpc.my_vpc vpc-1234