まずWebコンソールでゴールの確認
- VPCの作成
- Subnetの作成
- InternetGatewayの作成
- VPCに上記3のInternetGatewayをアタッチ
- SubnetのRouteTableに上記3のInternetGatewayに紐付いたルールを追加
- 上記1のVPC内で、上記2のSubnet配下に、EC2インスタンスをつくる
- (適宜、必要があればSecurityGroupのInboundをいじったりする)
- sshできる。
ここまでやった。できた。
% ssh -i ~/Desktop/otiai10.tokyo.pem ec2-user@54.249.68.183 __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ [ec2-user@ip-10-0-0-11 ~]$ exit logout Connection to 54.249.68.182 closed.
これをゴールとして、以下、これをaws cliだけでやっていく。
まずはお掃除する。
% aws ec2 describe-vpcs \ --filters Name=tag:Name,Values=otiai10-test-web \ | jq .Vpcs[].VpcId % aws ec2 delete-vpc --vpc-id vpc-374f0d50 An error occurred (DependencyViolation) when calling the DeleteVpc operation: The vpc 'vpc-374f0d50' has dependencies and cannot be deleted. % aws ec2 delete-vpc help NAME delete-vpc - DESCRIPTION Deletes the specified VPC. You must detach or delete all gateways and resources that are associated with the VPC before you can delete it. For example, you must terminate all instances running in the VPC, delete all security groups associated with the VPC (except the default one), delete all route tables associated with the VPC (except the default one), and so on.
まあそうですよね。消すべき要素としては、
という感じだろうか。
# EC2インスタンスIDの取得 % aws ec2 describe-instances \ --filters Name=tag:Name,Values='otiai10 Test' \ | jq .Reservations[].Instances[].InstanceId # セキュリティグループの取得 % aws ec2 describe-instances \ --filters Name=tag:Name,Values='otiai10 Test' \ | jq .Reservations[].Instances[].SecurityGroups # EC2インスタンスの削除 % aws ec2 terminate-instances \ --instance-ids i-0acd5d33bb4196314 # セキュリティグループの削除 % aws ec2 delete-security-group \ --group-id sg-7fb2ec07
# Subnet IDの取得 % aws ec2 describe-subnets \ --filters Name=tag:Name,Values=otiai10-test-web-example \ | jq .Subnets[].SubnetId # Subnetの削除 % aws ec2 delete-subnet \ --subnet-id subnet-39020970
# InternetGatewayのIDの取得 % aws ec2 describe-internet-gateways \ --filters Name=tag:Name,Values=otiai10-test-web-ig \ | jq .InternetGateways[].InternetGatewayId # InternetGatewayの削除 % aws ec2 delete-internet-gateway \ --internet-gateway-id igw-6de6b009 An error occurred (DependencyViolation) when calling the DeleteInternetGateway operation: The internetGateway 'igw-6de6b009' has dependencies and cannot be deleted.
おっと。なんだ。detachか?
# VPC IDの取得 % aws ec2 describe-vpcs \ --filters Name=tag:Name,Values=otiai10-test-web \ | jq .Vpcs[].VpcId # InternetGatewayをVPCからdetach % aws ec2 detach-internet-gateway \ --internet-gateway-id igw-6de6b009 \ --vpc-id vpc-374f0d50 # VPCの削除 % aws ec2 delete-vpc --vpc-id vpc-374f0d50 # InternetGatewayの削除 % aws ec2 delete-internet-gateway \ --internet-gateway-id igw-6de6b009
awscliを使ったVPCの新規作成からEC2へのssh可能状態まで
VPCを作成。
% aws ec2 create-vpc --cidr-block 10.0.0.0/16 | jq .Vpc.VpcId "vpc-b7e4acd0"
% aws ec2 create-tags \ --resources vpc-b7e4acd0 \ --tags Key=Name,Value=otiai10-test % aws ec2 describe-vpcs \ --filter Name=tag:Name,Values=otiai10-test # 取れてることを確認
Subnetの作成。
% aws ec2 create-subnet \ --availability-zone ap-northeast-1a \ --cidr-block 10.0.0.0/18 \ --vpc-id vpc-b7e4acd0 # 一応名前つけとく % aws ec2 create-tags \ --resources subnet-eb6076a2 \ --tags Key=Name,Value=otiai10-sn
InternetGatewayの作成。
% aws ec2 create-internet-gateway # 一応名前つけとく % aws ec2 create-tags \ --resources igw-a9beedcd \ --tags Key=Name,Value=otiai10-ig
InternetGatewayをVPCにアタッチ。
% aws ec2 attach-internet-gateway \ --vpc-id vpc-b7e4acd0 \ --internet-gateway-id igw-a9beedcd
MainのRouteTableの確認。
% aws ec2 describe-route-tables \ | jq '.RouteTables[] | select(.VpcId =="vpc-b7e4acd0")'
ここにRouteの追加。
% aws ec2 create-route \ --destination-cidr-block 0.0.0.0/0 \ --route-table-id rtb-57a20331 \ --gateway-id igw-a9beedcd
これで一応要件を満たしたVPC( vpc-b7e4acd0
)ができたと思う。
動作確認
SecurityGroupの作成。
% aws ec2 create-security-group \ --group-name otiai10-ssh \ --description ssh-from-office \ --vpc-id vpc-b7e4acd0 # オフィスからのSSHを開ける % aws ec2 authorize-security-group-ingress \ --group-id sg-ab5550d3 \ --protocol tcp \ --port 22 \ --cidr 106.180.6.34/32
EC2インスタンスの作成
% aws ec2 run-instances \ --instance-type t2.nano \ --image-id ami-e99f4896 \ --key-name otiai10.tokyo \ --security-group-ids sg-ab5550d3 \ --subnet-id subnet-eb6076a2 \ --associate-public-ip-address
% ssh \ -i ~/.ssh/aws/otiai10.tokyo.pem \ ec2-user@52.197.24.227 __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ [ec2-user@ip-10-0-8-208 ~]$ [ec2-user@ip-10-0-8-208 ~]$ uname -a Linux ip-10-0-8-208.ap-northeast-1.compute.internal 4.14.47-64.38.amzn2.x86_64 #1 SMP Mon Jun 18 22:33:07 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [ec2-user@ip-10-0-8-208 ~]$ exit logout Connection to 52.197.24.227 closed. % %
できた。帰りはしんどいのでWebコンソールでお掃除した。
DRYな備忘録として
Amazon Web Services 基礎からのネットワーク&サーバー構築 改訂版
- 作者: 玉川憲,片山暁雄,今井雄太,大澤文孝
- 出版社/メーカー: 日経BP社
- 発売日: 2017/04/13
- メディア: 単行本
- この商品を含むブログを見る
Amazon Web Servicesではじめる新米プログラマのためのクラウド超入門 (CodeZine BOOKS)
- 作者: WINGSプロジェクト阿佐志保,山田祥寛
- 出版社/メーカー: 翔泳社
- 発売日: 2016/06/17
- メディア: 単行本(ソフトカバー)
- この商品を含むブログを見る