2020年5月22日金曜日

Terraform勉強しようと思う

これまでデプロイ系のサービスは避けて通ってきたけど、そろそろ本腰入れて勉強しようと思い立った。
っで、CFnは少しずつ触ってきたのだけど、便利と評判のTerraformに触手を伸ばしてみようと思う。

まずは環境を整える

brewをインストール
% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

そして、dockerをインストール
% brew cask install docker

AWS CLIもインストール
% brew install awscli

AWS CLIの初期設定
% aws configure
Access Key IDとSecret Access Key、デフォルトリージョンをap-northeast-1を設定

secret keyをgitに上げない保険として、git-secretsをインストール
% brew install git-secrets

ここで、git用のpathを確定する必要がある。自分の場合は、
/Users/nobu/git-repo
とした。

gitの初期設定
% cd /Users/nobu/git-repo
% git init
% git config --global user.name "full name"
% git config --global user.email "mail address"
% git config --global color.diff auto
% git config --global color.status auto
% git config --global color.branch auto


っで、git-secretsをインストール
% git secrets --install -f
% git secrets --register-aws

% git secrets --register-aws --global
% git secrets --install ~/.git-templates/git-secrets
% git config --global init.templateDir ~/.git-templates/git-secrets
% git secrets --add-provider -- cat /Users/nobu/git-repo/.git-templates/git-secrets

っで、環境構築完了

TerraformをDockerで起動してみる。

% mkdir /Users/nobu/terraform-handson
% cd /Users/nobu/terraform-handson

ここで、dockerを起動してみる。
% open /Applications/Docker.app

そして、terraformのdocker fileをpull?

% docker run \
    -e AWS_ACCESS_KEY_ID=<AWS ACCESS KEY> \
    -e AWS_SECRET_ACCESS_KEY=<AWS SECRET ACCESS KEY> \
    -v $(pwd):/terraform \
    -w /terraform \
    -it \
    --entrypoint=ash \
    hashicorp/terraform:0.11.13

Terraformの動作確認
# terraform version

/terraform # terraform version
Terraform v0.11.13
Your version of Terraform is out of date! The latest version
is 0.12.25. You can update by downloading from www.terraform.io/downloads.html
バージョン古いようなメッセージが出たけど、そのまま継続。
別のTerminalを立ち上げて
% mkdir vpc-handson
% cd vpc-handson

っで、terraformを起動しているTerminal側で
# ls
ちゃんとvpc-handsonが表示される。
別のTerminalでvpc-handsonディレクトリに移動して、main.tfファイルを書く。

# terraform plan
# terraform show
# terraform apply
# terraform destroy
などで、実際にVPCができることを確認していく。

勉強になった。

参考サイト:
https://y-ohgi.com/introduction-terraform/first/handson/

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。

Apple Watch Ultra 3

 以前の6から乗り換え 6は娘へお下がり、下取りしても3,000円だった ダイビングを再開してから、潜る前後で時計を付け替える煩わしさを感じてたけど、確かに、ダイビングを休眠する前から感じていたことだったのを思い出した ソフトのサブスクも高いらしいけど、まあぁ、ダメだったら以前の...