LoginSignup
5
5

More than 5 years have passed since last update.

Macでaws-cliを使って、EC2を立ち上げてみた

Posted at

aws-cliの導入

ec2を立ち上げるのに必要なツールです。
pythonのパッケージ管理ツールであるpipを使ってインストールします。

pip install awscli

python, pipが入ってない場合

# pythonをインストール
brew install python

# 下記設定を~/.bashrcに追記
export PATH=/usr/local/share/python:$PATH

# pipをインストール
easy_install pip

jqの導入

jsonをいい感じに扱えるようになるツールです。
aws-cliでインスタンスを作ったとき、そのインスタンス情報がjsonで返ってきます。インスタンス情報に含まれるinstanceIdを簡単に抜き出すために入れておきます。

brew install jq

日本語でjqの使い方を書いてくれてる記事

jq コマンドを使う日常のご紹介

EC2を立ち上げて名前を付ける

EC2を立ち上げるにはaws ec2 run-instancesコマンドを使います。
名前(Nameのタグ)を付けるにはaws ec2 create-tagsを使います。

  1. aws ec2 run-instancesでインスタンスを作成する。この時、返ってきたinstance情報をtest.jsonに保存する(サブネットの指定、IAMの指定が必要ない人は外してください。)
 aws ec2 run-instances --image-id "AMIのイメージid(ami-***)" --count 1 --security-group-ids "セキュリティグループのid(sg-***)" --instance-type "インスタンスの種類(t2.micro)" --key-name "Key Pairの名前" --subnet-id "サブネットのid(subnet-***)" --iam-instance-profile Name="IAMの名前" > test.json
  1. aws ec2 create-tagsでTestという名前を付ける
  instanceId=`cat test.json | jq -r '.Instances[].InstanceId'`
  aws ec2 create-tags --resources $instanceId --tags '[{"Key": "Name", "Value": "Test"}]'

最後に

aws-cliを使ってEC2のインスタンスを立ち上げてみました。すでに、ansibleやchefでサーバー構成を管理しているのであれば、CloudFormationを使うのもいいかもしれません。

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