Download - JAWS-UG京王線#1業務で使うAWSCLI
![Page 1: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/1.jpg)
ハンズラボ株式会社 ITエンジニア 今井 智明
JAWS-UG京王線#1
業務で使うAWSCLI
![Page 2: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/2.jpg)
2
自己紹介
名前 今井 智明 所属 ハンズラボ株式会社 2013年4月より東急ハンズの IT部門を子会社化 担当 AWSでのインフラ構築、運用 システム開発、運用、その他諸々
![Page 3: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/3.jpg)
3
本日お話すること
l EC2インスタンスの建て直しをAWSCLIを使ってやってみる l 背景 l 手順
![Page 4: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/4.jpg)
4
インスタンスを建て直す背景
l T2インスタンス利用によるコストダウンのため l 変更後にRIを買う
l EBS-GP2の利用によるパフォーマンスアップのため l EBSのタイプを変更するにはディスクの再作成が必要 →インスタンスの建て直しで対応
![Page 5: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/5.jpg)
5
インスタンスを建て直す手順
l インスタンス停止 l AMI取得 l インスタンス削除 l AMIからインスタンス作成 l 結果報告
![Page 6: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/6.jpg)
6
インスタンスを建て直す計画
l 調査したところ対象は20台! l Windows Server + SQL Server
l これらのインスタンスは本番稼働していて、夜間の一時間程度しか作業時間がない
![Page 7: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/7.jpg)
7
問題
l 手動でやったら終わらない!
l 手動でやったらなんか間違える!
l 夜中にやったら更に間違える!
l 分けると夜間作業が増えてつらい!
![Page 8: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/8.jpg)
8
そこで!
AWS CLI!
![Page 9: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/9.jpg)
9
AWSCLIによる手順
l インスタンス停止 手動※ ※データ保全のためOSからシャットダウンする
l インスタンス情報取得 CLI l インスタンス停止待ち CLI l AMI取得、AMI取得待ち CLI l インスタンス削除 CLI l AMIからインスタンス作成 CLI l 結果報告 CLI
![Page 10: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/10.jpg)
10
インスタンスの情報収集
# インスタンス名を取得する $ aws ec2 describe-tags \ --filters Name=resource-id,Values=${instance_id} \ Name=key,Values=Name > instance_name $ instance_name=$(jq ’.Tags[0].Value’ instance_name) # インスタンスの情報を取得する $ aws ec2 describe-instances \ --instance-ids ${instance_id} \ --query 'Reservations[0].Instances[0]' > describe_instance $ subnet_id=$(jq -r '.SubnetId’ describe_instance) $ private_ip=$(jq -r '.PrivateIpAddress’ describe_instance) $ key_name=$(jq -r '.KeyName’ describe_instance) $ security_group=$(jq -r '.SecurityGroups[0].GroupId’ describe_instance)
![Page 11: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/11.jpg)
11
インスタンス停止待ち
$ aws ec2 wait instance-stopped --instance-ids ${instance_id} # タイムアウトオプションほしい
![Page 12: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/12.jpg)
12
AMI取得
$ cat << FIN > mapping.json [{ "DeviceName": "/dev/xvda", "Ebs": { "VolumeType": "gp2" } }] FIN $ aws ec2 create-image \ --instance-id ${instance_id} \ --name ${ami_name} \ --description ${description} \ --block-device-mappings file://mapping.json \ --no-reboot > create_image
![Page 13: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/13.jpg)
13
AMI取得待ち
$ new_ami_id=$(jq -r '.ImageId’ create_image) # AMIの取得完了を待つ(waitほしい) $ while true; do state=$(aws ec2 describe-images --image-ids ${new_ami_id} | jq -r '.Images[0].State') [ ${state} = "available" ] && break sleep 30 done
![Page 14: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/14.jpg)
14
インスタンス削除、削除待ち
$ aws ec2 terminate-instances --instance-ids ${instance_id} # タイムアウトオプションほしい $ aws ec2 wait instance-terminated --instance-ids ${instance_id}
![Page 15: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/15.jpg)
15
インスタンス作成
$ aws ec2 run-instances \ --image-id ${new_ami_id} \ --instance-type ${instance_type} \ --private-ip-address ${private_ip} \ --security-group-ids ${security_group} \ --subnet-id ${subnet_id} \ --key-name ${key_name} > run_instance
![Page 16: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/16.jpg)
16
インスタンス作成待ち、タグ付け
$ new_instance_id=$(jq -r '.Instances[0].InstanceId’ run_instance) $ aws ec2 wait instance-running \ --instance-ids ${new_instance_id} $ aws ec2 create-tags \ --resources ${new_instance_id} \ --tags Key=Name,Value=${instance_name}
![Page 17: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/17.jpg)
17
結果報告
$ cat << FIN > message インスタンスの建て直しが完了しました。 動作確認をお願いします。 インスタンス名:${instance_name} インスタンスID:${new_instance_id} IPアドレス:${private_ip} FIN $ aws sns publish \ --topic-arn ${sns_topic_arn} \ --subject “インスタンス建て直し完了(${instance_name})" \ --message file://message
![Page 18: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/18.jpg)
18
仕上げ
l 一発スクリプトに仕上げる l このスクリプトを20個分呼び出すスクリプトを用意する
l 20並列でも動くインスタンスを用意する(c3.8xlarge?)
l リハーサルする
![Page 19: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/19.jpg)
19
今日のまとめ
l AWSCLIを使えば作業が自動化できる
l 作業が自動化できれば並列で実行できる
l 作業が自動化できればミスがなくなる
l 作業が自動化できれば寝てられる
![Page 20: JAWS-UG京王線#1業務で使うAWSCLI](https://reader035.vdocuments.pub/reader035/viewer/2022081209/55a8c7001a28abb1108b482a/html5/thumbnails/20.jpg)
ご清聴ありがとうございました
エンジニア中途採用絶賛募集中です
⇒www.hands-lab.com