LoginSignup
0
0

More than 1 year has passed since last update.

WSL(Ubuntu)でTerraform利用準備と備忘メモ

Posted at

WSL2のUbuntuを使って、Terraformを操作していく。

環境

以下。
image.png

前提

やり方

WSL準備

  • Powershellを開く

  • powershell:コンソールログ記録(新規)
    Teratermログのようなコンソールの記録を取るばあい、Powershellではこうする。
Powershell
Start-Transcript -Path 保存先パス\ログファイル名.log

  • powershell:WSLリスト表示
Powershell
wsl -l -v

  NAME            STATE           VERSION
* Ubuntu-20.04    Stopped         2

Ubuntuが選択されていることを確認して次へ。

  • powershell:WSL実行
Powershell
wsl


Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.15.90.1-microsoft-standard-WSL2 x86_64)
 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu Apr  6 15:17:53 JST 2023

  System load:  0.61               Processes:             9
  Usage of /:   1.2% of 250.92GB   Users logged in:       0
  Memory usage: 3%                 IPv4 address for eth0: 172.22.233.133
  Swap usage:   0%

0 updates can be applied immediately.

The list of available updates is more than a week old.
To check for new updates run: sudo apt update

This message is shown once a day. To disable it please create the
/home/USER/.hushlogin file.

実行するとプロンプトがpowershellの「>」から「$」に変わり、Ubuntuのシェルセッションに遷移する。

AWS CLIインストール

  • powershell(Ubuntu):AWSCLIインストール
Powershell(Ubuntu-20.04)
$ sudo apt install awscli

(定型:次回以降も繰り返し)AWSクレデンシャル投入

  • powershell(Ubuntu):環境変数にIAMユーザのシークレットとリージョンを投入
Powershell(Ubuntu-20.04)
$ export AWS_ACCESS_KEY_ID=AKXXXXXXXXXXXXXXXXXX
$ export AWS_SECRET_ACCESS_KEY=XXXX!!XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
$ export AWS_DEFAULT_REGION=ap-northeast-1

※exitなどでログアウト、OS再起動等をすると、この環境変数は消えます。再ログイン時にこれを改めて実行。

  • powershell(Ubuntu):AWSのクレデンシャルを登録
Powershell(Ubuntu-20.04)
$ aws configure
AWS Access Key ID [None]: ${AWS_ACCESS_KEY_ID}
AWS Secret Access Key [None]: ${AWS_SECRET_ACCESS_KEY}
Default region name [None]: ${AWS_DEFAULT_REGION}
Default output format [None]: text

※exitなどでログアウト、OS再起動のあと、これを改めて実行。

  • powershell(Ubuntu):AWSアカウントのクレデンシャル確認
Powershell(Ubuntu-20.04)
$ aws sts get-caller-identity --query Account --output text

※exitなどでログアウト、OS再起動のあと、これを改めて実行。AWSのアカウントIDが出ればOK.

各種ツールインストール

  • powershell(Ubuntu):requiredパッケージのインストール
Powershell(Ubuntu-20.04)
$ sudo apt-get install build-essential procps curl file git

  • powershell(Ubuntu):Homebrewのインストール
Powershell(Ubuntu-20.04)
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

  • powershell(Ubuntu):Homebrewのパスを追加
Powershell(Ubuntu-20.04)
$ test -d ~/.linuxbrew && PATH="$HOME/.linuxbrew/bin:$HOME/.linuxbrew/sbin:$PATH"
$ test -d /home/linuxbrew/.linuxbrew && $ PATH="/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:$PATH"
$ test -r ~/.bash_profile && echo "export PATH='$(brew --prefix)/bin:$(brew --prefix)/sbin'":'"$PATH"' >>~/.bash_profile
$ echo "export PATH='$(brew --prefix)/bin:$(brew --prefix)/sbin'":'"$PATH"' >>~/.profile

  • powershell(Ubuntu):Homebrewのパスが通っているかの確認でバージョンを確認
Powershell(Ubuntu-20.04)
$ brew --version

Homebrew 3.4.11

Terraform関連のインストール

  • powershell(Ubuntu):tfenvインストール
Powershell(Ubuntu-20.04)
$ brew install tfenv

==> Downloading https://ghcr.io/v2/homebrew/core/bzip2/manifests/1.0.8-1
######################################################################## 100.0%

  • powershell(Ubuntu):tfenvインストール確認
Powershell(Ubuntu-20.04)
$ tfenv --version

tfenv 2.2.3

  • powershell(Ubuntu):terraformバージョン確認
Powershell(Ubuntu-20.04)
$ tfenv list-remote

1.2.0-rc2
1.2.0-rc1
1.2.0-beta1
1.2.0-alpha20220413
1.2.0-alpha
1.1.9
1.1.8
・・・
・・・

  • powershell(Ubuntu):terraformバージョン指定インストール
    この場合、「0.12.5」バージョンを選択してインストールしている。
Powershell(Ubuntu-20.04)
$ tfenv install 0.12.5

Installing Terraform v0.12.5
Downloading release tarball from https://releases.hashicorp.com/terraform/0.12.5/terraform_0.12.5_linux_amd64.zip

  • powershell(Ubuntu):terraformバージョン指定
    この場合、「0.12.5」バージョンを指定
Powershell(Ubuntu-20.04)
$ tfenv list
$ tfenv use 0.12.5
$ tfenv list
$ terraform -v

Terraform v0.12.5

Your version of Terraform is out of date! The latest version
is 1.1.9. You can update by downloading from www.terraform.io/downloads.html

Terraform:リソースの作成

  • powershell(Ubuntu):リソース作成用ディレクトリ作成、カレントディレクトリ移動
Powershell(Ubuntu-20.04)
$ mkdir example
$ cd example
$ pwd

  • powershell(Ubuntu):Terraformメインファイルの作成
Powershell(Ubuntu-20.04)
$ touch main.tf

  • powershell(Ubuntu):Terraformメインファイルの編集
Powershell(Ubuntu-20.04)
$ nano main.tf
main.tf
provider "aws" {
  region  = "ap-northeast-1"
  version = "4.22.0"
}

リージョン名、プロバイダーバージョンは任意変更。

  • powershell(Ubuntu):(初回のみ)Terraform init リソース作成用バイナリダウンロード、初期化
Powershell(Ubuntu-20.04)
$ pwd
$ terraform init

※この場合、exampleディレクトリでinitすること。そのためにpwdでカレントディレクトリをチェックしている。

  • エディタnanoで良い感じにtfファイル、つまりリソースを書いていく。
    ある程度ソースコード(.tfファイル)にリソースを書いたら、それをAWSに適用する。

  • powershell(Ubuntu):Terraform plan リソース実行計画
    Dry-Runとなる。ここでエラーやWarnが出たら、ソースコードを適宜修正する。
    ここでもexampleディレクトリがカレントなのを確認するためpwdしている。
Powershell(Ubuntu-20.04)
$ pwd
$ terraform plan

  • powershell(Ubuntu):Terraform apply リソース作成
    実際にリソースをクラウドへ作成する。失敗が許されない部分。
    いろいろリソースが表示された後、yes noが出るので、yesでEnterする。
Powershell(Ubuntu-20.04)
$ pwd
$ terraform apply

yes

  • powershell(Ubuntu):Terraform tfstate リソース状態ファイル確認
    リソースの状態がここに記載されるため、軽くチェックする。
Powershell(Ubuntu-20.04)
$ pwd
$ cat terraform.tfstate

  • powershell(Ubuntu):Terraform destroy リソース削除
    リソースを削除する。リソースをすべて消すので注意する。
    なお、ソースコードは消されない。あくまでもリソースが消える。
    いろいろリソースが表示された後、yes noが出るので、yesでEnterする。
Powershell(Ubuntu-20.04)
$ pwd
$ terraform destroy

yes

作業終了

  • powershell(Ubuntu):Ubuntuからログアウト
Powershell(Ubuntu-20.04)
$ exit

プロンプトがubuntuの「$」から、powershellの「>」に変わる。

  • powershell:コンソールログ終了
    取ってたログを止める。
Powershell
Stop-Transcript

参考

  • UbuntuにHomebrewをインストール

  • Ubuntu18.04LTSにlinuxbrew(linux版homebrew)を入れる

  • 実践Terraform AWSにおけるシステム設計とベストプラクティス (技術の泉シリーズ(NextPublishing))
    具体的なTerraformのソースコードの書き方はこの本がとってもおすすめ。

0
0
0

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
0
0