処理の実行
VPC IDを取得します。
コマンド:
EC2_VPC_ID=$( \
aws ec2 describe-vpcs \
--filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME} \
--query 'Vpcs[].VpcId' \
--output text \
) \
&& echo ${EC2_VPC_ID}
結果(例):
セキュリティグループのセキュリティグループIDを取得します。
コマンド:
EC2_SECURITY_GROUP_ID=$( \
aws ec2 describe-security-groups \
--filter Name=vpc-id,Values=${EC2_VPC_ID} \
Name=group-name,Values=${EC2_SECURITY_GROUP_NAME} \
--query "SecurityGroups[].GroupId" \
--output text \
) \
&& echo ${EC2_SECURITY_GROUP_ID}
結果(例):
作業サイトのIPアドレスを取得します。
コマンド:
IP_ADDR_LOCAL=$( curl -s http://checkip.amazonaws.com/ ) \
&& echo ${IP_ADDR_LOCAL}
結果(例):
変数の設定:
EC2_SECURITY_GROUP_RULE_CIDR="${IP_ADDR_LOCAL}/32" \
&& echo ${EC2_SECURITY_GROUP_RULE_CIDR}
結果(例):
タグ文字列を生成します。
変数の設定:
STRING_EC2_EC2_SECURITY_GROUP_RULE_TAG="ResourceType=security-group-rule,Tags=[{Key=Name,Value=${EC2_SECURITY_GROUP_RULE_TAG_NAME}}]" \
&& echo ${STRING_EC2_EC2_SECURITY_GROUP_RULE_TAG}
結果(例):
ResourceType=security-group-rule,Tags=[{Key=Name,Value=http-local}]
セキュリティグループのルールを作成します。
変数の確認:
cat << END
# EC2_SECURITY_GROUP_ID:"sg-xxxxxxxxxxxxxxxxx"
EC2_SECURITY_GROUP_ID="${EC2_SECURITY_GROUP_ID}"
# EC2_SECURITY_GROUP_RULE_PROTOCOL:"tcp"
EC2_SECURITY_GROUP_RULE_PROTOCOL="${EC2_SECURITY_GROUP_RULE_PROTOCOL}"
# EC2_SECURITY_GROUP_RULE_PORT:"80"
EC2_SECURITY_GROUP_RULE_PORT="${EC2_SECURITY_GROUP_RULE_PORT}"
# EC2_SECURITY_GROUP_RULE_CIDR:"xxx.xxx.xxx.xxx/32"
EC2_SECURITY_GROUP_RULE_CIDR="${EC2_SECURITY_GROUP_RULE_CIDR}"
# STRING_EC2_EC2_SECURITY_GROUP_RULE_TAG:"ResourceType=security-group-rule,Tags=[{Key=Name,Value=http-local}]"
STRING_EC2_EC2_SECURITY_GROUP_RULE_TAG="${STRING_EC2_EC2_SECURITY_GROUP_RULE_TAG}"
END
コマンド:
aws ec2 authorize-security-group-ingress \
--group-id ${EC2_SECURITY_GROUP_ID} \
--protocol ${EC2_SECURITY_GROUP_RULE_PROTOCOL} \
--port ${EC2_SECURITY_GROUP_RULE_PORT} \
--cidr ${EC2_SECURITY_GROUP_RULE_CIDR} \
--tag-specifications ${STRING_EC2_EC2_SECURITY_GROUP_RULE_TAG}
結果(例):
{
"Return": true,
"SecurityGroupRules": [
{
"SecurityGroupRuleId": "sgr-xxxxxxxxxxxxxxxxx",
"GroupId": "sg-xxxxxxxxxxxxxxxxx",
"GroupOwnerId": "XXXXXXXXXXXX",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 80,
"ToPort": 80,
"CidrIpv4": "xxx.xxx.xxx.xxx/32",
"Tags": [
{
"Key": "Name",
"Value": "http-local"
}
]
}
]
}
完了確認
「VPC"handson-cli-ec2-sg-vpc"のセキュリティグループ"handson-cli-ec2-sg-app-sg"に、タグ名"http-local"のルールが存在する。」ことを確認します。
コマンド:
aws ec2 describe-security-group-rules \
--max-results 100 \
--filter Name=group-id,Values=${EC2_SECURITY_GROUP_ID} \
Name=tag:Name,Values=${EC2_SECURITY_GROUP_RULE_TAG_NAME} \
--query 'SecurityGroupRules[].Tags[].Value' \
--output text
結果(例):
「VPC"handson-cli-ec2-sg-vpc"のセキュリティグループ"handson-cli-ec2-sg-app-sg"に、"80/tcp"に対する通信をCIDR"xxx.xxx.xxx.xxx/32"に許可するルールが存在する。」ことを確認します。
コマンド:
aws ec2 describe-security-groups \
--filter Name=vpc-id,Values=${EC2_VPC_ID} \
Name=group-name,Values=${EC2_SECURITY_GROUP_NAME} \
Name=ip-permission.protocol,Values=${EC2_SECURITY_GROUP_RULE_PROTOCOL} \
Name=ip-permission.to-port,Values=${EC2_SECURITY_GROUP_RULE_PORT} \
Name=ip-permission.cidr,Values=${EC2_SECURITY_GROUP_RULE_CIDR} \
--query "SecurityGroups[].IpPermissions[?IpProtocol == \`${EC2_SECURITY_GROUP_RULE_PROTOCOL}\` \
&& ToPort == \`${EC2_SECURITY_GROUP_RULE_PORT}\` \
&& IpRanges[?CidrIp == \`${EC2_SECURITY_GROUP_RULE_CIDR}\`]].IpRanges[][].CidrIp" \
--output text
結果(例):