Redis(6379/TCP)を許可するセキュリティグループを作成してみます。
前提条件
VPCへの権限
- EC2に対してフル権限があること。
AWS CLI
以下のバージョンで動作確認済
- AWS CLI 1.10.44
コマンド
aws --version
結果(例)
aws-cli/1.10.44 Python/2.7.11 Darwin/15.5.0 botocore/1.4.34
バージョンが古い場合は最新版に更新しましょう。
コマンド
sudo -H pip install -U awscli
参考: AWS CLIのデグレード
コマンド(1.10.20に戻す例)
AWS_CLI_VERSION=1.10.20
sudo -H pip install awscli==${AWS_CLI_VERSION}
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. リージョンの決定
変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'
0.2. 変数の確認
変数の確認
aws configure list
結果(例)
Name Value Type Location
---- ----- ---- --------
profile ec2Full-prjZ-mbp13 env AWS_DEFAULT_PROFILE
access_key ****************LOAQ shared-credentials-file
secret_key ****************I1O1 shared-credentials-file
region ap-northeast-1 env AWS_DEFAULT_REGION
- 事前作業
===========
1.1. VPC Security Groupの名称と説明の決定
変数の設定
VPC_SG_NAME='ec2-redis-global-inbound'
VPC_SG_DESC='EC2 Redis 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-redis-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 Redis Global inbound",
"IpPermissions": [],
"GroupName": "EC2 Redis 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
- アクセス制御の設定
=====================
VPC Security Groupの設定追加 (Redis)
変数の設定
VPC_SG_PROTOCOL='tcp'
VPC_SG_PORT='6379'
VPC_SG_CIDR='0.0.0.0/0'
変数の確認
cat << ETX
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-ids ${VPC_SG_ID}
結果(例)
{
"SecurityGroups": [
{
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"UserIdGroupPairs": [],
"PrefixListIds": []
}
],
"Description": "EC2 Redis Global inbound",
"IpPermissions": [
{
"PrefixListIds": [],
"FromPort": 6379,
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": 6379,
"IpProtocol": "tcp",
"UserIdGroupPairs": []
}
],
"GroupName": "ec2-redis-global-inbound",
"VpcId": "vpc-xxxxxxxx",
"OwnerId": "XXXXXXXXXXXX",
"GroupId": "sg-xxxxxxxx"
}
]
}