Terraform
標準モジュール構造
最低 3つ
main.tf
主要なエントリーポイント
outputs.tf
すべての出力値の宣言
variables.tf
すべての入力変数の宣言
type = any
型チェックをスキップ
厳密な片付けをしない場合に利用
あまり利用はしない
References
認定試験
サンプル
AMI ID 確認
Important
destroy 時に誤削除しないように prevent_destroy = true つける
for macOS
Terraform を先に導入している場合には、シンボリックリンクを先に外す
code:shell
$ brew unlink terraform
tfenv の導入
Terraform のバージョンマネージャ
install
code:shell
$ brew install tfenv
$ tfenv --version
tfenv 1.0.1
利用可能な Terraform のバージョン確認
code:shell
$ tfenv list-remote
ローカルインストール
code:shell
$ tfenv install 0.12.5
$ terraform --version
Terraform v0.12.5
バージョンファイルの設定
code:shell
$ echo "0.12.5" > .terraform-version
別の開発者がインストールする場合には、 tfenv install を実行すると指定したバージョンがインストールされる
AWS 環境構築
1. IAM ユーザの作成
IAM ユーザを作る
2. コンフィグレーションファイルの作成
認証情報
variables.tf
code:conf
provider "aws" {}
実行
code:shell
$ export AWS_ACCESS_KEY_ID="anaccesskey"
$ export AWS_SECRET_ACCESS_KEY="asecretkey"
$ export AWS_DEFAULT_REGION="us-west-2"
$ terraform plan
3. 初期化
code:shell
$ terraform init
4. ドライラン
code:shell
$ terraform plan
5. 適用
code:shell
$ terraform apply
## ConoHa サンプル
IAM を管理したときに複合も含めたワンライナー
code:shell
$ set count 1 | terraform output -json | jq -r '.user.value, .aws_iam_user_admin_password.value, .id.value, .encrypted_secret.value' | awk '{print $1}' | while read line; echo $line | if test $count -eq 2 -o $count -eq 4; base64 --decode | keybase pgp decrypt; echo; else; echo $line ; end ;set count (math $count + 1); end
code:shell
$ set count 1 | cat terraform.tfstate | jq -cr '.resources[].instances[].attributes | select(.user == "test01") | .id, .encrypted_secret, .encrypted_password' | grep -v "null" | while read line; echo $line | if test $count -eq 2 -o $count -eq 4; base64 --decode | keybase pgp decrypt; echo; else; echo $line ; end ;set count (math $count + 1); end
`