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?

EC2インスタンスのカスタマイズ

Posted at

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

GROUP_ID=$(aws ec2 create-security-group \
    --group-name mysg \
    --description "My security group" \
    --output text \
    --query 'GroupId' \
    --region ap-northeast-1)

この--group-name ~は自分で名前を設定できる。

2.インバウンドルールを設定する
インバウンドルールを設定するためにポートを開く必要がある。
今回はHTTP通信とHTTPS通信を可能にするポートを開く手順を紹介する。

aws ec2 authorize-security-group-ingress \
  --region ap-northeast-1 \
  --group-id $GROUP_ID \
  --protocol tcp \
  --port 80 \
  --cidr 0.0.0.0/0

ポート80番の閲覧を許可

aws ec2 authorize-security-group-ingress \
--region ap-northeast-1 \
--group-id $GROUP_ID \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0

ポート443番の閲覧を許可

aws ec2 authorize-security-group-ingress \
--region ap-northeast-1 \
--group-id $GROUP_ID \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0

ポート22番の接続を許可

3.セキュリティグループをEC2インスタンスに紐付け

aws ec2 modify-instance-attribute \
  --instance-id $INSTANCE_ID \
  --groups $GROUP_ID
aws ec2 describe-instances \
--instance-ids $INSTANCE_ID \
--region ap-northeast-1 | \
jq -r '.Reservations[].Instances[].SecurityGroups[]'

このコードでEC2インスタンスにセキュリティグループが正しく紐付けられたか確認

これにて簡単なEC2の初期設定が完了
AWSからEC2を検索してEC2インスタンスIDを確認する。

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?