【#DevOps】使用Google开发的名为Terraformer的工具,将现有的AWS环境转换为“基础设施即代码(Infrastructure as Code,IaC)”的形式。#Terraform #Terraformer #AWS #jawsug

Google公司正在开发/发布一款名为Terrafomer的工具,用于根据现有环境生成Terraform的tf文件。

■GitHub – GoogleCloudPlatform/terraformer
https://github.com/GoogleCloudPlatform/terraformer

■GitHub – GoogleCloudPlatform/terraformer
https://github.com/GoogleCloudPlatform/terraformer

这个Terrafomer是由Google开发的,并且不仅支持GCP,还支持其他云平台,如AWS等。

由于这次使用了Terraformer来将现有的AWS环境转化为“基础设施即代码(IaC)”,因此我将其过程整理成了自己的备忘录。

执行环境的环境信息

OS/ソフトウェアバージョン入手元Windows 10 proバージョン1803(April 2018 Update)Microsoft Corporationaws-cli1.16.200https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-windows.htmlGo1.12.7https://golang.org/dl/terraformv0.12.6-devhttps://github.com/hashicorp/terraformterraformerversion v0.7.6https://github.com/GoogleCloudPlatform/terraformer

事前准备

□步骤 1-1:安装 aws-cli

为了在AWS环境中运行Terraformer,需要先安装aws-cli。

在这里,我们省略了安装aws-cli的步骤,但对于没有管理员权限的Windows环境安装aws-cli的步骤,请参考下方的说明。

【#awscli】在没有管理员权限的Windows环境中安装“AWS命令行界面(aws-cli)” #AWS #jawsug #环境搭建 #环境配置

步骤1-2:安装Go

为了安装Terraformer,需要安装Go。

这里省略了Go的安装步骤,但是如果您在没有管理员权限的Windows环境下安装Go,可以参考下面的说明。

在不具备管理员权限的Windows环境中安装Go语言和Visual Studio Code(vscode)的方法。 #golang #golangjp #vscode #环境搭建 #环境设置

在中文中,只需要一種選擇,針對以下內容進行改寫:

□步驟 1-3:執行aws configure指令,記錄認證資訊等。

PS C:\> & aws configure
拡張子 .py のファイルの関連付けが見つかりません
AWS Access Key ID [None]: <My AWS Access Key>
AWS Secret Access Key [None]: <AWS Secret Access Key>
Default region name [None]: ap-northeast-1
Default output format [None]: json

通过添加–profile选项,也可以创建配置文件。

在默认配置文件中,注册一个只具有读取权限的帐户,并将可变权限与DevOps、开发人员等配置文件相关联,可以避免错误的更改,因此是安全的。

PS C:\> & aws configure --profile DevOps
拡張子 .py のファイルの関連付けが見つかりません
AWS Access Key ID [None]: <My AWS Access Key>
AWS Secret Access Key [None]: <AWS Secret Access Key>
Default region name [None]: ap-northeast-1
Default output format [None]: json

□步骤 1-4:首先,使用 go get 命令安装 Terraform。

PS C:\> & go get -v -u github.com/hashicorp/terraform
PS C:\> & terraform --version
Terraform v0.12.6-dev

请按照以下顺序进行操作:1-5步骤,使用相同的go get命令来安装Terraformer。

PS C:\> & go get -v -u github.com/GoogleCloudPlatform/terraformer
PS C:\> & terraformer --version
version v0.7.6

通过Terraformer从现有的AWS环境生成tf文件。

步骤 2-1:创建用于生成 tf 文件的工作文件夹(这里使用名为 MyAWSTerraformManifests 的文件夹)。

PS C:\> New-Item -ItemType Directory -Path $env:GOPATH\src\MyAWSTerraformManifests


    ディレクトリ: C:\Users\xxxxxx\gocode\src


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       YYYY/MM/DD     hh:mm                MyAWSTerraformManifests

□步骤 2-2:在任意的文本编辑器中创建init.tf文件。

provider "aws" {
  region  = "ap-northeast-1"
}

请按照以下步骤操作:
□ 第2-3步:执行terraform init命令。

如果显示如下,则表示成功。

PS C:\> cd $env:GOPATH\src\MyAWSTerraformManifests
PS C:\> & terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "aws" (2.21.0)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 2.21"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

□步骤 2-4:执行 terraformer import aws ~

terraformer import aws --regions=ap-northeast-1 --resources=ec2_instance
terraformer import aws --regions=ap-northeast-1 --resources=elb
terraformer import aws --regions=ap-northeast-1 --resources=iam
terraformer import aws --regions=ap-northeast-1 --resources=rds
terraformer import aws --regions=ap-northeast-1 --resources=route53
terraformer import aws --regions=ap-northeast-1 --resources=s3
terraformer import aws --regions=ap-northeast-1 --resources=sg
terraformer import aws --regions=ap-northeast-1 --resources=subnet
terraformer import aws --regions=ap-northeast-1 --resources=vpc
terraformer import aws --regions=ap-northeast-1 --resources=vpn_connection
terraformer import aws --regions=ap-northeast-1 --resources=vpn_gateway

在步骤2至步骤5中,各种tf文件将在名为”generated”的文件夹下生成。

PS C:> & tree /F
フォルダー パスの一覧:  ボリューム Local Disk
ボリューム シリアル番号は XXXX-XXXX です
C:.
  init.tf

├─.terraform
  └─plugins
      └─windows_amd64
              lock.json
              terraform-provider-aws_v2.20.0_x4.exe

└─generated
    └─aws
        ├─ec2_instance
          └─ap-northeast-1
                  instance.tf
                  outputs.tf
                  provider.tf
                  terraform.tfstate
                  variables.tf
        
        ├─elb
          └─ap-northeast-1
                  elb.tf
                  outputs.tf
                  provider.tf
                  terraform.tfstate
                  variables.tf
        
        ├─iam
          └─ap-northeast-1
                  iam_group.tf
                  iam_group_membership.tf
                  iam_policy.tf
                  iam_policy_attachment.tf
                  iam_role_policy.tf
                  iam_user.tf
                  outputs.tf
                  provider.tf
                  terraform.tfstate
        
        ├─rds
          └─ap-northeast-1
                  db_instance.tf
                  db_option_group.tf
                  db_parameter_group.tf
                  db_subnet_group.tf
                  outputs.tf
                  provider.tf
                  terraform.tfstate
                  variables.tf
        
        ├─route53
          └─ap-northeast-1
                  outputs.tf
                  provider.tf
                  route53_record.tf
                  route53_zone.tf
                  terraform.tfstate
        
        ├─s3
          └─ap-northeast-1
                  outputs.tf
                  provider.tf
                  s3_bucket.tf
                  s3_bucket_policy.tf
                  terraform.tfstate
        
        ├─sg
          └─ap-northeast-1
                  outputs.tf
                  provider.tf
                  security_group.tf
                  terraform.tfstate
        
        ├─subnet
          └─ap-northeast-1
                  outputs.tf
                  provider.tf
                  subnet.tf
                  terraform.tfstate
                  variables.tf
        
        ├─vpc
          └─ap-northeast-1
                  outputs.tf
                  provider.tf
                  terraform.tfstate
                  vpc.tf
        
        ├─vpn_connection
          └─ap-northeast-1
                  outputs.tf
                  provider.tf
                  terraform.tfstate
                  variables.tf
                  vpn_connection.tf
        
        └─vpn_gateway
            └─ap-northeast-1
                    outputs.tf
                    provider.tf
                    terraform.tfstate
                    variables.tf
                    vpn_gateway.tf

将生成的tf文件升级到HCL2。

因为Terraformer目前的最新版本v0.7.6生成的tf文件不支持采用自Terraform 0.12以后开始使用的HCL2语法,所以您需要手动进行升级。

运行terraform 0.12upgrade命令

Get-ChildItem -Path ".\generated\aws\" | %{$path = $_.FullName + "\ap-northeast-1\"; echo $path; & terraform 0.12upgrade -yes -force $path}

3-2. 通过运行terraform validate命令进行验证确认。

Get-ChildItem -Path ".\generated\aws\" | %{$path = $_.FullName + "\ap-northeast-1\"; echo $path; & terraform validate $path}

在我的环境中,执行terraform validate命令后输出了一些错误。似乎Terraformer仍然存在一些问题,需要手动进行一定程度的修正。


使用Terraformer从现有的AWS环境中生成Terraform的tf文件的步骤如下。