gpt4 book ai didi

google-cloud-platform - Terraformer 合并多个 tfstate 文件

转载 作者:行者123 更新时间:2023-12-05 02:42:31 27 4
gpt4 key购买 nike

我正在使用 terraformer(不是 terraform,检查这个 https://github.com/GoogleCloudPlatform/terraformer)导入一些托管在谷歌云上的基础设施,请看下面我的命令。

即:terraformer import google --resources=networks,subnetworks,firewall,routes,forwardingRules,vpnTunnels --regions=europe-blabla1 --projects=my_project

它工作正常,命令的输出是一个单独的文件夹,其中包含我导入的每个资源的 tfstate 和 tf 文件。

2021/05/18 11:03:20 google Connecting....
2021/05/18 11:03:20 google save firewall
2021/05/18 11:03:20 google save tfstate for firewall
2021/05/18 11:03:20 google save routes
2021/05/18 11:03:20 google save tfstate for routes
2021/05/18 11:03:20 google save forwardingRules
2021/05/18 11:03:20 google save tfstate for forwardingRules
2021/05/18 11:03:20 google save vpnTunnels
2021/05/18 11:03:20 google save tfstate for vpnTunnels
2021/05/18 11:03:20 google save networks
2021/05/18 11:03:20 google save tfstate for networks
2021/05/18 11:03:20 google save subnetworks
2021/05/18 11:03:20 google save tfstate for subnetworks

现在我想要完成的是将这些多个 tfstate 文件合并为一个文件,以便拥有堆栈,比方说:一个用于网络,一个用于 iam、实例等。

我想知道是否有人能够做到这一点?或者是否有更好的方法来实现这一点?

最佳答案

我去过那里并使用 Terraformer 然后合并状态文件,这是我编写的用于执行状态文件迁移(合并)的脚本:

#!/usr/bin/env bash

state_pull() {
echo ">> state pull"
cd "$1" || return 1
terraform init
terraform state pull >terraform.tfstate
cd ..
return 0
}

state_mv() {
echo ">> state mv"
cd "$2" || return 1
terraform init
for i in $(terraform state list); do
terraform state mv -state-out="../$1/terraform.tfstate" "$i" "$i"
done
cd ..
return 0
}

state_push() {
echo ">> state push"
cd "$1" || return 1
terraform state push terraform.tfstate
cd ..
return 0
}

cleanup() {
echo ">> cleanup"
cd "$1" || return 1
rm terraform.tfstate
rm terraform.tfstate.*
cd ..
return 0
}

# Print help text and exit.
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: tfmv.sh [options]"
echo
echo "Examples:"
echo
echo " - ./tfmv.sh stack1-migrate-to stack2-migrate-from"
echo " - ./tfmv.sh app data"
exit 1
fi

echo "> START"
echo ">> TF = $(terraform version)..."
state_pull "$1"
state_mv "$1" "$2"
state_push "$1"
# cleanup "$1"
echo "> FINISHED"

如果这看起来很花哨,您可以使用 https://github.com/minamijoyo/tfmigrate使用 Go 可以做得更好。

关于google-cloud-platform - Terraformer 合并多个 tfstate 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67585515/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com