0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

操作しながら覚えるAWS:コマンドラインでSSH接続可能なEC2環境を作る

0
Posted at

この記事は私が初めてAWS上でEC2を作成した際に利用した手順の記録です。
元々の手順は、ChatGPTに作成してもらいました。その手順で動作確認しながら学習を行いました。

0.変数の設定

REGION=ap-northeast-1
VPC_CIDR=10.0.0.0/10
SUBNET_CIDR=10.0.1.0/16
KEY_NAME=my-key
SG_NAME=my-sg

1.VPCの作成

VPC_ID=$(aws ec2 create-vpc --region $REGION --cidr-block $VPC_CIDR --query "Vpc.VpcId" --output text)

2.Subnetの作成

SUBNET_ID=$(aws ec2 create-subnet --region $REGION --vpc-id $VPC_ID --cidr-block $VPC_CIDR --availability-zone ${REGION}a --query "Subnet.SubnetId" --output text)

3.InternetGateway(IGW)の作成

IGW_ID=$(aws ec2 create-internet-gateway --region $REGION --query "InternetGateway.InternetGatewayId" --output text)

4.IGWをVPCにアタッチ

aws ec2 attach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_ID

5.RouteTable(RTB)の作成

RTB_ID=$(aws ec2 create-route-table --region $REGION --vpc-id $VPC_ID --query "RouteTable.RouteTableId" --output text)

6.RTBにルーティングを追加

全てのパケット(0.0.0.0/0)をIGWに転送するルーティングを追加

aws ec2 create-route --route-table-id $RTB_ID --destination-cidr 0.0.0.0/0 --gateway-id $IGW_ID

7.SubnetをRouteTableに関連付け

aws ec2 associate-route-table --subnet-id $SUBNET_ID --route-table-id $RTB_ID

8.KeyPairの作成

aws ec2 create-key-pair --region $REGION --key-name $KEY_NAME --key-type ed25519 --query "MatreialId" --output text > ${KEY_NAME}.pem
chmod 400 ${KEY_NAME}.pem

9.SecurityGroupの作成

SG_ID=$(aws ec2 create-security-group --region $REGION --group-name $SG_NAME --description "SSH access" --vpc-id $VPC_ID --query "GroupId" --output text)

10.SecurityGroupに受信許可ルールの追加

aws ec2 authorize-security-group-ingress --group-id $SG_ID --protocol tcp --port 22 --cidr 0.0.0.0/0

11.AMIのID取得

IMAGE_ID=$(aws ec2 describe-images --owners amazon --region $REGION --filters "Name=name,Values=al2023-ami-minimal-*-6.18-x86_64" --query "Images[0].ImageId" --output text)

12.EC2インスタンスの起動

INSTANCE_ID=$(aws ec2 run-instances --region $REGION --image-id $IMAGE_ID --instance-type t2.micro --key-name $KEY_NAME --security-group-ids $SG_ID --subnet-id $SUBNET_ID --query "Instances[0].InstanceId" --output text)

13.ElasticIPの取得

ALLOC_ID=$(aws ec2 allocate-address --region $REGION --query "AllocationId" --output text)

14.EC2インスタンスへElasticIPを付与

aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $ALLOC_ID

15.EC2インスタンスに割り当てられたPublicIPを取得

PUBLIC_IP=$(aws ec2 describe-instances --region $REGION --instance-ids $INSTANCE_ID --query "Reservations[0].Instances[0].PublicIpAddress" --output text)

16.SSH接続

ssh -i ${KEY_NAME}.pem ec2-user@$PUBLIC_IP

17.お片付け

aws ec2 terminate-instances --instance-ids $INSTANCE_ID
aws ec2 delete-security-group --group-id $SG_ID
 RTBA_ID=$( aws ec2 describe-route-tables --filters "Name=association.subnet-id,Values=$SUBNET_ID" --query "RouteTables[].Associations[].RouteTableAssociationId" --output text)
aws ec2 delete-route-table --route-table-id $RTB_ID
aws ec2 detach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_ID
aws ec2 delete-internet-gateway --internet-gateway-id $IGW_ID
aws ec2 delete-subnet --subnet-id $SUBNET_ID
aws ec2 delete-vpc --vpc-id $VPC_ID

ElasticIPを数日削除し忘れていたため従量課金が発生してしまいました。

aws release-address --allocation-id xxx
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?