LoginSignup
6
11

More than 3 years have passed since last update.

【#DevOps】Google社が開発した Terraformer というツールを使って既存のAWS環境の「Infrastructure as Code(IaC)」化を行う #Terraform #Terraformer #AWS #jawsug

Last updated at Posted at 2019-07-22

Google社が既存環境からTerraformのtfファイルを生成するTerrafomerという名前のツールを開発/公開しています↓

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

なんと、このTerrafomerは、Google社が開発していますが、GCPだけではなくAWSなどの他クラウドにも対応しています。

今回、このTerraformerを使用して既存のAWS環境を『Infrastructure as Code(IaC)』化してみたので、その際の内容を自分用のメモとしてまとめました。

実行環境の環境情報

OS/ソフトウェア バージョン 入手元
Windows 10 pro バージョン1803(April 2018 Update) Microsoft Corporation
aws-cli 1.16.200 https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-windows.html
Go 1.12.7 https://golang.org/dl/
terraform v0.12.6-dev https://github.com/hashicorp/terraform
terraformer version v0.7.6 https://github.com/GoogleCloudPlatform/terraformer

1.事前準備

□手順 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をインストール手順については↓に記載していますので参考にしてください。

【#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オプションを付与することでプロファイルの作成も可能です

デフォルトのプロファイルには読み取り権限のみをもったアカウントを登録しておき変更可能な権限はDevOpsDevelopersなどのプロファイルに紐付けておけば誤った変更などを避けられるので安全です

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

2. 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ファイルを作成します

$env
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: generatedというフォルダ下に各種tfファイルが生成されます

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

3. 生成した tfファイルを HCL2 にアップグレードします

現時点のTerraformerの最新バージョンv0.7.6が生成するtfファイルは、Terraform 0.12以降採用されたHCL2に対応していないため、手動でアップグレードを行う必要があります

3-1. 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ファイルを生成する手順でした。

6
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
11