処理の実行
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}
結果(例):
ソースグループセキュリティグループのセキュリティグループIDを取得します。
コマンド:
EC2_SECURITY_GROUP_ID_SOURCE=$( \
aws ec2 describe-security-groups \
--filter Name=vpc-id,Values=${EC2_VPC_ID} \
Name=group-name,Values=${EC2_SECURITY_GROUP_NAME_SOURCE} \
--query "SecurityGroups[].GroupId" \
--output text \
) \
&& echo ${EC2_SECURITY_GROUP_ID_SOURCE}
結果(例):
タグ文字列を生成します。
変数の設定:
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=PostgreSQL-handson-cli-ec2-sg-app-sg}]
セキュリティグループルール作成します。
変数の確認:
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:"5432"
EC2_SECURITY_GROUP_RULE_PORT="${EC2_SECURITY_GROUP_RULE_PORT}"
# EC2_SECURITY_GROUP_ID_SOURCE:"sg-yyyyyyyyyyyyyyyyy"
EC2_SECURITY_GROUP_ID_SOURCE="${EC2_SECURITY_GROUP_ID_SOURCE}"
# STRING_EC2_EC2_SECURITY_GROUP_RULE_TAG:"ResourceType=security-group-rule,Tags=[{Key=Name,Value=PostgreSQL-handson-cli-ec2-sg-app-sg}]"
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} \
--source-group ${EC2_SECURITY_GROUP_ID_SOURCE} \
--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": 5432,
"ToPort": 5432,
"ReferencedGroupInfo": {
"GroupId": "sg-yyyyyyyyyyyyyyyyy"
},
"Tags": [
{
"Key": "Name",
"Value": "PostgreSQL-handson-cli-ec2-sg-app-sg"
}
]
}
]
}
完了確認
「VPC"handson-cli-ec2-sg-vpc"のセキュリティグループ"handson-cli-ec2-sg-db-sg"に、タグ名"PostgreSQL-handson-cli-ec2-sg-app-sg"のルールが存在する。」ことを確認します。
コマンド:
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
結果(例):
PostgreSQL-handson-cli-ec2-sg-app-sg
「VPC"handson-cli-ec2-sg-vpc"のセキュリティグループ"handson-cli-ec2-sg-db-sg"に、"5432/tcp"に対する通信をソースセキュリティグループ"handson-cli-ec2-sg-app-sg"に許可するルールが存在する。」ことを確認します。
コマンド:
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.group-id,Values=${EC2_SECURITY_GROUP_ID_SOURCE} \
--query "SecurityGroups[].IpPermissions[?IpProtocol == \`${EC2_SECURITY_GROUP_RULE_PROTOCOL}\` \
&& ToPort == \`${EC2_SECURITY_GROUP_RULE_PORT}\` \
&& UserIdGroupPairs[?GroupId == \`${EC2_SECURITY_GROUP_ID_SOURCE}\`]]"
結果(例):
[
[
{
"FromPort": 5432,
"IpProtocol": "tcp",
"IpRanges": [],
"Ipv6Ranges": [],
"PrefixListIds": [],
"ToPort": 5432,
"UserIdGroupPairs": [
{
"GroupId": "sg-xxxxxxxx",
"UserId": "XXXXXXXXXXXX"
}
]
}
]
]