[Terraform] 请参阅不同 tfstate 中描述的资源信息

我叫寺冈,是一名基础设施工程师。

实用
,有一个
这次,我想介绍一下它。

terraform_remote_state 是什么?

您想要引用有关不同 tfstate 文件中描述的资源的信息
此数据源非常有用

Terraform
记录使用 terraform apply 执行部署时
有一个名为 tfstate 的文件,用于

使用 Terraform 构建基础设施时,
如果您有多个环境(例如跳转主机、生产环境和开发环境),
以便 `terraform apply` 不会影响其他环境
则必须为每个环境分离 tfstate,

当您拆分 tfstate 时,
跳转环境中创建的资源信息
您可能需要在生产环境中创建资源时引用
这时,`terraform_remote_state` 就派上用场了。

■何时使用

以下是使用 AWS 的示例。

假设通过跳转服务器建立与服务器的 SSH 连接,
我们该如何使用 Terraform 创建安全组和规则?
首先,我认为我们需要为跳转服务器创建一个 VPC 以及相应的安全组和规则。

堡垒.tf

Terraform { 后端 "s3" { 存储桶 = "terraform-tfstate" 键 = "terraform-b​​log/bastion.tfstate" 区域 = "ap-northeast-1" } } 资源 "aws_vpc" "堡垒机" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true tags { 名称 = "vpc-bastion" } } 资源 "aws_security_group" "堡垒机" { 名称 = "bastion-sg" description = "堡垒机" vpc_id = "${aws_vpc.bastion.id}" tags { 名称 = "bastion-sg" } } 资源 "aws_security_group_rule" "prod_sg_rule" { security_group_id = "${aws_security_group.bastion.id}" type = "ingress" from_port = "22" to_port = "22" protocol = "tcp" cidr_blocks = "XXX.XXX.XXX.XXX" }

在后端配置中,tfstate 存储在 S3 中,名称为 bastion.tfstate,
但这没问题,因为它只需要添加一个 VPC 和安全组,以及一条允许特定 IP 地址的规则。
那么,跳转主机“接收端”的设置又该如何呢?

production.tf

Terraform { 后端 "s3" { 存储桶 = "terraform-tfstate" 键 = "terraform-b​​log/prod.tfstate" 区域 = "ap-northeast-1" } } 资源 "aws_vpc" "prod" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true tags { 名称 = "vpc-prod" } } 资源 "aws_security_group" "prod" { 名称 = "prod-sg" 描述 = "用于生产服务器" vpc_id = "${aws_vpc.prod.id}" tags { 名称 = "prod-sg" } } 资源 "aws_security_group_rule" "prod_sg_rule" { security_group_id = "${aws_security_group.prod.id}" type = "ingress" from_port = "22" to_port = "22" protocol = " "tcp" source_security_group_id = "${我应该指定什么???}" }

如果要允许从跳转服务器进行 SSH 连接,
则需要允许从跳转服务器的安全组进行访问,因此
需要使用 `source_security_group_id` 指定跳转服务器安全组的 ID。

但是,由于后端描述将跳转服务器和 tfstate 分开,
因此无法在此状态下引用安全组 ID。

■使用方法

既然我们已经确定这是不可能的,我想让它变得可访问。
首先,我们需要将输出设置添加到 bastion.tf 文件中。

堡垒.tf

Terraform { 后端 "s3" { 存储桶 = "terraform-tfstate" 键 = "terraform-b​​log/bastion.tfstate" 区域 = "ap-northeast-1" } } 资源 "aws_vpc" "堡垒机" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true tags { 名称 = "vpc-bastion" } } 资源 "aws_security_group" "堡垒机" { 名称 = "bastion-sg" description = "堡垒机" vpc_id = "${aws_vpc.bastion.id}" tags { 名称 = "bastion-sg" } } 资源 "aws_security_group_rule" "prod_sg_rule" { security_group_id = "${aws_security_group.bastion.id}" type = "ingress" from_port = "22" to_port = "22" protocol = "tcp" cidr_blocks = "XXX.XXX.XXX.XXX" } ## 添加输出 "security_group_id" { value = "${aws_security_group.bastion.id}" }

当引用不同 tfstate 中的值时,
需要预先输出要引用的值。
在本例中,我们要引用堡垒机安全组的 ID,因此
输出 aws_security_group.bastion.id。

在这种状态下,您可以使用 terraform_remote_state 引用 production.tf 中的值。

production.tf

Terraform { 后端 "s3" { 存储桶 = "terraform-tfstate" 键 = "terraform-b​​log/prod.tfstate" 区域 = "ap-northeast-1" } } 资源 "aws_vpc" "prod" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true tags { 名称 = "vpc-prod" } } 资源 "aws_security_group" "prod" { 名称 = "prod-sg" 描述 = "用于生产服务器" vpc_id = "${aws_vpc.prod.id}" tags { 名称 = "prod-sg" } } 资源 "aws_security_group_rule" "prod_sg_rule" { security_group_id = "${aws_security_group.prod.id}" type = "ingress" from_port = "22" to_port = "22" protocol = " "tcp" source_security_group_id = "${data.terraform_remote_state.bastion.security_group_id}" } ## 添加数据 "terraform_remote_state" "bastion" { backend = "s3" config { bucket = "terraform-tfstate" key = "terraform-b​​log/bastion.tfstate" region = "ap-northeast-1" } }

我们使用 `terraform_remote_state` 数据源
来读取存储在 S3 中的跳转主机的 `tfstate`。
该值可以通过 `output` 中指定的名称来引用,因此
要为 `source_security_group_id` 指定的值
将是 `data.terraform_remote_state.bastion.security_group_id`。

■概要

`terraform_remote_state` 的使用频率很高,所以值得记住。
这意味着你可以轻松地拆分 `tfstate` 文件!

如果您觉得这篇文章对您有帮助,请点个“赞”!
1
加载中...
1票,平均分:1.00/11
11,443
X Facebook Hatena书签 口袋

这篇文章的作者

关于作者

寺冈由纪

我于 2016 年加入 Beyond,目前
担任基础设施工程师和 MSP(托管服务提供商)已有六年。我负责处理突发事件的故障排除,
并使用 AWS 等公有云设计和构建基础设施。最近,我一直在
Docker 和 Kubernetes 等容器基础设施。此外,
使用 HashiCorp 的 Terraform 和 Packer 等工具来构建和自动化
我还担任技术推广者的角色,在外部学习小组和研讨会上进行演讲。

・GitHub
https://github.com/nezumisannn

• 演讲邀约
:https://github.com/nezumisannn/my-profile

• 演示材料(SpeakerDeck)
https://speakerdeck.com/nezumisannn

・认证:
AWS认证解决方案架构师 - 助理级、
Google Cloud专业云架构师