LoginSignup
0
0

More than 3 years have passed since last update.

kusanagiのAMI ID を AWS CLI で調べて、一撃で起動したい

Last updated at Posted at 2020-11-19

Kusanagiの最新のAMI IDをCLIで確認してCLIからEC2を起動したい( 2020/11 時点 )

※ AWSマーケットプレイスの ProductCodeId の運用については、オフィシャルがアナウンスしている訳ではないので注意が必要だと思います。将来的に変わる可能性もあるかと。

AWSマーケットプレイス内を『KUSANAGI』で検索

aws ec2 describe-images --region ap-northeast-1 --owners 'aws-marketplace' --filters --filters 'Name=name,Values=*KUSANAGI*' | jq .

出力内容をみると...

ProductCodeId は
* 239igp26xilrqtgegz3yqea8h
* f18wc0igqjhsxwoxouogwqb8m
の2つあり...
f18wc0igqjhsxwoxouogwqb8m の方が起動したい KUSANAGI for AWS (WordPress) を含んでいました。

スクリーンショット 2020-11-19 20.52.25.png

「最新のイメージ」かつ「無償のマシンイメージ」のAMI IDを取得したい

aws ec2 describe-images --region ap-northeast-1 --owners 'aws-marketplace' --filters --filters 'Name=product-code,Values=f18wc0igqjhsxwoxouogwqb8m' --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' --output 'text'

CentOSの例ですが、ここを参考にしました
( CentOSの場合は、こんな感じでプロダクトコードを公開してくれてるんですよね )

--query で日付ごとにソートし、
--output 'text' でシンプルなテキストのみを取得しています。

また --filters で使えるオプションはここが参考になりそうです

AWS CLI で一発起動

最後にjqでパースしてIPアドレスを返します( sshでログインする設定を作るので欲しい )

aws ec2 run-instances --profile AWSプロファイル名 \
--count 1 \
--subnet-id サブネットID \
--security-group-ids セキュリティグループID \
--instance-type t2.medium \
--image-id $(aws ec2 describe-images --region ap-northeast-1 --owners 'aws-marketplace' --filters --filters 'Name=product-code,Values=f18wc0igqjhsxwoxouogwqb8m' --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' --output 'text') \
| jq --raw-output '.Instances[0].NetworkInterfaces[0].PrivateIpAddress'
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