前提条件
EC2への権限
EC2に対してフル権限があること。
AWS CLIのバージョン
以下のバージョンで動作確認済
- AWS CLI 1.11.7
コマンド
aws --version
結果(例)
aws-cli/1.11.2 Python/2.7.11 Darwin/15.6.0 botocore/1.4.60
バージョンが古い場合は最新版に更新しましょう。
コマンド
sudo -H pip install -U awscli
AWSアカウントの属性
AWSアカウントがEC2-Classicに対応していないこと。
コマンド
AWS_SUPPORT_PLATFORMS=$( \
aws ec2 describe-account-attributes \
--query 'AccountAttributes[?AttributeName == `supported-platforms`].AttributeValues[].AttributeValue' \
--output text \
) && echo ${AWS_SUPPORT_PLATFORMS}
結果
VPC
'VPC'の他に'EC2'が表示される場合、別のアカウントを作成もしくは利用してください。
デフォルトVPCの存在
デフォルトVPCが存在すること。
コマンド
VPC_ID=$( \
aws ec2 describe-vpcs \
--filters Name=isDefault,Values=true \
--query 'Vpcs[].VpcId' \
--output text \
) \
&& echo ${VPC_ID}
結果(例)
vpc-xxxxxxxx
- 準備
=======
0.1. リージョンの決定
変数の設定
AWS_DEFAULT_REGION='us-west-2'
0.2. 変数の確認:
プロファイルが想定のものになっていることを確認します。
変数の確認
aws configure list
結果(例)
Name Value Type Location
---- ----- ---- --------
profile ec2full-prjZ-mbp13 env AWS_DEFAULT_PROFILE
access_key ****************XXXX shared-credentials-file
secret_key ****************XXXX shared-credentials-file
region us-west-2 env AWS_DEFAULT_REGION
- 事前作業
===========
1.1. Security Group名の決定
変数の設定
VPC_SG_NAME='ec2-ssh-global-inbound'
1.2. Security Groupの説明の決定
変数の設定
VPC_SG_DESC='EC2 SSH Global inbound'
1.2. VPC Security Groupの不存在確認
コマンド
aws ec2 describe-security-groups \
--group-names ${VPC_SG_NAME}
結果(例)
A client error (InvalidGroup.NotFound) occurred when calling the DescribeSecurityGroups operation: The security group 'ec2-ssh-global-inbound' does not exist in default VPC 'vpc-xxxxxxxx'
- VPC Security Groupの作成
===========================
2.1. VPC Security Groupの作成
変数の確認
cat << ETX
VPC_SG_NAME: ${VPC_SG_NAME}
VPC_SG_DESC: "${VPC_SG_DESC}"
ETX
コマンド
aws ec2 create-security-group \
--group-name ${VPC_SG_NAME} \
--description "${VPC_SG_DESC}"
結果(例)
{
"GroupId": "sg-xxxxxxxx"
}
2.2. VPC Security Groupの確認
コマンド
aws ec2 describe-security-groups \
--group-names ${VPC_SG_NAME}
結果(例)
{
"SecurityGroups": [
{
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"UserIdGroupPairs": [],
"PrefixListIds": []
}
],
"Description": "EC2 SSH Global inbound",
"IpPermissions": [],
"GroupName": "ec2-ssh-global-inbound",
"VpcId": "vpc-xxxxxxxx",
"OwnerId": "XXXXXXXXXXXX",
"GroupId": "sg-xxxxxxxx"
}
]
}
2.3. VPC Security Group IDの取得
コマンド
VPC_SG_ID=$( \
aws ec2 describe-security-groups \
--filter Name=group-name,Values=${VPC_SG_NAME} \
--query 'SecurityGroups[].GroupId' \
--output text \
) \
&& echo ${VPC_SG_ID}
結果(例)
sg-xxxxxxxx
- アクセス制御の設定
=====================
3.1. プロトコルの指定 (TCP)
変数の設定
VPC_SG_PROTOCOL='tcp'
3.2. ポート番号の指定 (SSH)
変数の設定
VPC_SG_PORT='22'
3.3. CIDRの指定
変数の設定
VPC_SG_CIDR='0.0.0.0/0'
3.4. ルールの追加
変数の確認
cat << ETX
VPC_SG_ID: ${VPC_SG_ID}
VPC_SG_PROTOCOL: ${VPC_SG_PROTOCOL}
VPC_SG_PORT: ${VPC_SG_PORT}
VPC_SG_CIDR: ${VPC_SG_CIDR}
ETX
コマンド
aws ec2 authorize-security-group-ingress \
--group-id ${VPC_SG_ID} \
--protocol ${VPC_SG_PROTOCOL} \
--port ${VPC_SG_PORT} \
--cidr ${VPC_SG_CIDR}
結果
(戻り値なし)
- 事後作業
===========
セキュリティグループの内容確認
コマンド
aws ec2 describe-security-groups \
--group-names ${VPC_SG_NAME}
結果(例)
{
"SecurityGroups": [
{
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"UserIdGroupPairs": [],
"PrefixListIds": []
}
],
"Description": "EC2 SSH Global inbound",
"IpPermissions": [
{
"PrefixListIds": [],
"FromPort": 22,
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": 22,
"IpProtocol": "tcp",
"UserIdGroupPairs": []
}
],
"GroupName": "ec2-ssh-global-inbound",
"VpcId": "vpc-xxxxxxxx",
"OwnerId": "XXXXXXXXXXXX",
"GroupId": "sg-xxxxxxxx"
}
]
}