AWS CLI(EC2)

セキュリティグループの作成
aws ec2 create-security-group \
--description Mysg01 \
--group-name Mysg01 \
--vpc-id vpc-04ffe9cc84548ab04


セキュリティグループの一覧
aws ec2 describe-security-groups
aws ec2 describe-security-groups | jq -r '.SecurityGroups.GroupId'
aws ec2 describe-security-groups | jq -r '.SecurityGroups
| [ .GroupId, .GroupName ]'

 

セキュリティグループの削除
aws ec2 delete-security-group \
--group-id sg-12345678901234567


セキュリティグループへのアウトバウンドルール追加
aws ec2 authorize-security-group-egress \
--group-id sg-12345678901234567 \
--protocol tcp \
--port 1024-65535 \
--cidr 0.0.0.0/0

 

セキュリティグループへのインバウンドルール追加
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678901234567 \
--protocol tcp \
--port 1024-65535 \
--cidr 0.0.0.0/0

セキュリティグループからのアウトバウンドルール削除
aws ec2 revoke-security-group-egress \
--group-id sg-12345678901234567 \
--protocol tcp \
--port 1024-65535 \
--cidr 0.0.0.0/0


セキュリティグループからのインバウンドルール削除
aws ec2 revoke-security-group-ingress \
--group-id sg-12345678901234567 \
--protocol tcp \
--port 1024-65535 \
--cidr 0.0.0.0/0


キーペアの作成
aws ec2 create-key-pair \
--key-name MyKeyPair


キーペアの一覧
aws ec2 describe-key-pairs
aws ec2 describe-key-pairs | jq -r '.KeyPairs | [ .KeyPairId, .KeyName ]'
aws ec2 describe-key-pairs | jq -r -c '.KeyPairs
| [ .KeyPairId, .KeyName ]'


キーペアの削除
aws ec2 delete-key-pair \
--key-name MyKeyPair

※key-pair-id 単独指定はエラーとなる


ネットワークインターフェースの作成
aws ec2 create-network-interface \
--description "my network interface" \
--groups sg-12345678901234567 \
--private-ip-address 172.16.3.100 \
--subnet-id subnet-12345678901234567

ネットワークインターフェースの一覧
aws ec2 describe-network-interfaces
aws ec2 describe-network-interfaces | jq -rc '.NetworkInterfaces | [ .NetworkInterfaceId, .PrivateIpAddress ]'

aws ec2 describe-network-interfaces | jq -r '.NetworkInterfaces | [ .Attachment, .Description ]'


ネットワークインターフェースの削除
aws ec2 delete-network-interface \
--network-interface-id eni-12345678901234567

ネットワークインターフェースのアタッチ
aws ec2 attach-network-interface \
--device-index 1 \
--instance-id i-12345678901234567 \
--network-interface-id eni-12345678901234567


ネットワークインターフェースのデタッチ
aws ec2 detach-network-interface \
--attachment-id eni-attach-12345678901234567

 


ボリュームの作成

aws ec2 create-volume \
--availability-zone ap-northeast-1a \
--no-encrypted \
--size 10 \
--volume-type gp2


ボリュームの一覧

aws ec2 describe-volumes
aws ec2 describe-volumes | jq -r '.Volumes.VolumeId'


ボリュームの削除

aws ec2 delete-volume \
--volume-id vol-12345678901234567


ボリュームのアタッチ

aws ec2 attach-volume \
--device /dev/sdf \
--instance-id i-12345678901234567 \
--volume-id vol-12345678901234567


ボリュームのデタッチ

aws ec2 detach-volume \
--volume-id vol-12345678901234567


EBSスナップショットの作成

aws ec2 create-snapshot \
--description "snapshot001" \
--volume-id vol-12345678901234567

EBSスナップショットの一覧

aws ec2 describe-snapshots \
--owner-ids self

aws ec2 describe-snapshots \
--owner-ids self | jq -r '.Snapshots.SnapshotId'


EBSスナップショットの削除

aws ec2 delete-snapshot \
--snapshot-id snap-12345678901234567

EBSスナップショットのコピー

aws ec2 copy-snapshot \
--description "snapshot002" \
--destination-region ap-northeast-1 \
--source-region ap-northeast-1 \
--source-snapshot-id snap-12345678901234567


AMIの作成
aws ec2 create-image \
--description "ami001" \
--instance-id i-12345678901234567 \
--name "ami001"

AMIの一覧
aws ec2 describe-images \
--owners self

aws ec2 describe-images \
--owners self | jq -r '.Images.ImageId'


AMIの削除
aws ec2 deregister-image \
--image-id ami-12345678901234567


AMIのコピー
aws ec2 copy-image \
--description "ami002" \
--name "ami002" \
--source-image-id ami-12345678901234567 \
--source-region ap-northeast-1

 

プレイスメントグループの作成
aws ec2 create-placement-group \
--group-name pg01 \
--strategy cluster

aws ec2 create-placement-group \
--group-name pg02 \
--strategy spread

プレイスメントグループの一覧
aws ec2 describe-placement-groups
aws ec2 describe-placement-groups | jq -r ".PlacementGroups.GroupId"

プレイスメントグループの削除

aws ec2 delete-placement-group \
--group-name pg01

 

EC2インスタンスの作成

aws ec2 run-instances \
--image-id ami-12345678901234567 \
--instance-type t2.nano \
--key-name key001 \
--placement GroupName=pg02 \
--security-group-ids sg-12345678901234567 \
--subnet-id subnet-12345678901234567 \
--private-ip-address 172.16.1.99 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=Instance01},{Key=Key1,Value=Val1}]' \
--associate-public-ip-address

 


EC2インスタンスの一覧
aws ec2 describe-instances
aws ec2 describe-instances | jq -r '.Reservations.Instances | { InstanceId, State }'


EC2インスタンスの削除
aws ec2 terminate-instances \
--instance-ids i-12345678901234567


EC2インスタンスの起動
aws ec2 start-instances \
--instance-ids i-12345678901234567


EC2インスタンスの停止
aws ec2 stop-instances \
--instance-ids i-12345678901234567


EC2インスタンスの再起動

aws ec2 reboot-instances \
--instance-ids i-12345678901234567