• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

aws ec2

date: 2017-04-07 excerpt: aws ec2の使い方

tag: awsec2


aws ec2の使い方

概要

  • aws ec2の使い方についてまとめる
  • プロジェクトによって, AMIやセキュリティグループ, インスタンスの設定が固有であることがあるため、.envファイルを用意して、そこに設定を書く
  • アドホックに作成したインスタンスは、作成後にインスタンスタイプを変更することができる
    • Stop Instances -> Instance Settings -> Change Instance Type

sshキーの登録

  • 公開鍵に名前をつけて登録
$ aws ec2 import-key-pair --key-name "MyKeyPair" --public-key-material fileb://~/.ssh/id_ed25519.pub --region ap-northeast-1

セキュリティグループ

セキュリティグループの作成

  • “MySecurityGroup”という名前で作成
$ aws ec2 create-security-group \
      --group-name "MySecurityGroup" \
      --description "Security Group for my instance" \
      --region ap-northeast-1 \
      --query 'GroupId' --output text

セキュリティグループの設定

  • セキュリティグループにインバウンドルールを追加
  • port 22を開ける
  • $sg_idにはセキュリティグループのIDを指定
$ aws ec2 authorize-security-group-ingress \
      --group-id $sg_id \
      --protocol tcp \
      --port 22 \
      --cidr 0.0.0.0/0 \
      --region ap-northeast-1    

インスタンス

インスタンスの作成

  • インスタンスを作成する
  • $ami_idにはAMIのIDを指定
  • $sg_idにはセキュリティグループのIDを指定
  • $key_nameにはsshキーの名前を指定
$ aws ec2 run-instances \
      --image-id $ami_id \
      --count 1 \
      --instance-type t3.small \
      --key-name "MyKeyPair" \
      --security-group-ids $sg_id \
      --region ap-northeast-1 \
      --query 'Instances[0].InstanceId'

インスタンスの停止

$ aws ec2 stop-instances --instance-ids $instance_id --region ap-northeast-1

インスタンスの削除

$ aws ec2 terminate-instances --instance-ids $instance_id --region ap-northeast-1


awsec2 Share Tweet