Terraformer安装与使用

安装go环境

根据go.mod文件查看go的版本要求

image-20211221144228939

下载go安装包https://golang.google.cn/dl/

1
2
3
wget https://golang.google.cn/dl/go1.17.linux-amd64.tar.gz
sudo tar -zxvf go1.17.linux-amd64.tar.gz -C /opt
/opt/go/bin/go version

修改环境变量vi /etc/profile

1
2
3
4
5
6
7
export GOROOT=/opt/go
export GOPATH=/home/ubuntu/go
export GOPROXY=https://goproxy.cn
export GOARCH=amd64
export GOOS=linux
export GOTOOLS=$GOROOT/pkg/tool
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

使环境变量生效source /etc/profile

编译terraformer

  • Run git clone && cd terraformer/

  • Run git clone && cd terraformer/

  • Run go mod download (下载模块到本地缓存,缓存路径是 $GOPATH/pkg/mod/cache)

  • Run go build -v for all providers OR build with one provider: go run build/main.go {google,aws,azure,kubernetes and etc}

    image-20211221144524483

  • .Run terraform init against a versions.tf file to install the plugins required for your platform. For example, if you need plugins for the google provider, versions.tf should contain:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    terraform {
    required_providers {
    google = {
    source = "hashicorp/google"
    }
    }
    required_version = ">= 0.13"
    }
    # if need aws
    terraform {
    required_providers {
    aws = {
    source = "hashicorp/aws"
    }
    }
    }
  • Or alternatively Copy your Terraform provider’s plugin(s) to

    folder ~/.terraform.d/plugins/{darwin,linux}_amd64/, as appropriate.

    cp terraform-provider-aws_v3.57.0_x5 /root/.terraform.d/plugins/linux_amd64/

terraform 安装

1
2
3
4
5
6
$ wget https://releases.hashicorp.com/terraform/1.0.5/terraform_1.0.5_linux_amd64.zip
$ unzip terraform_1.0.5_linux_amd64.zip
$ sudo cp terraform /usr/local/bin/
$ which terraform
/usr/local/bin/terraform
$ terraform init

默认会下载aws插件到当前隐藏目录 ./terraform/.terraform/providers/registry.terraform.io/hashicorp/aws/3.57.0/linux_amd64/terraformprovider-aws_v3.57.0_x5

国内下载不了插件,可以去github下载release进行go编译;或者翻墙下载 https://registry.terraform.io/providers/hashicorp/aws/latest

资源tf抓取测试

1
2
3
4
5
# ./terraformer-aws import aws --resources s3
2021/09/03 10:25:53 aws importing default region
2021/09/03 10:25:56 aws importing... s3
2021/09/03 10:26:14 aws done importing s3
2021/09/03 10:26:14 Number of resources for service s3: 3

文件会保存在当前的generated目录下

image-20211221144945997

第二种安装方式

除了通过源码进行安装,还有一种比较简单的安装方式是下载对应的terraformer二进制安装包,地址如下:

https://github.com/GoogleCloudPlatform/terraformer/releases

找到对应云厂商的terraformer二进制包即可。这里选择AWS。terraformer-aws-linux-amd64

如果是windows,则选择对应的terraformer-aws-windows-amd64.exe

1
2
3
4
[root@localhost demoterraform]# wget https://github.com/GoogleCloudPlatform/terraformer/releases/download/0.8.19/terraformer-aws-linux-amd64
[root@localhost demoterraform]# mv terraformer-aws-linux-amd64 terraform-aws
[root@localhost demoterraform]# chmod +x terraform-aws
[root@localhost demoterraform]# alias terraform-aws="$(pwd)/terraform-aws"

输入terraform-aws查看命令有无生效.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost demoterraform]# terraform-aws
Usage:
[command]

Available Commands:
help Help about any command
import Import current state to Terraform configuration
plan Plan to import current state to Terraform configuration
version Print the version number of Terraformer

Flags:
-h, --help help for this command
-v, --version version for this command

Use " [command] --help" for more information about a command.