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?

More than 1 year has passed since last update.

指定した複数リージョンのセキュリティグループの一覧をワンライナーで作る

Posted at

はじめに

セキュリティグループルールの一覧は管理コンソールからのCSV出力が可能ですが、複数リージョンの出力を一度に行いたかったのでAWS CLIで一覧を作成することにしました。
作成した一覧はタブ区切りになっているのでスプレッドシートなどにコピーしてご活用ください。

コマンド

  • 取得したいリージョンが複数あればスペース区切りでリージョン名を追加してください
echo -e "OwnerId\
\tRegionName\
\tVpcId\
\tGroupId\
\tGroupName\
\tDescription\
\tSecurityGroupRuleId\
\tGroupId\
\tIsEgress\
\tIpProtocol\
\tFromPort\
\tToPort\
\tCidrIpv4\
\tCidrIpv6\
\tDescription\
\tReferencedGroupInfo.GroupId\
\tReferencedGroupInfo.PeeringStatus\
\tReferencedGroupInfo.VpcId\
\tReferencedGroupInfo.VpcPeeringConnectionId\
"\ > /tmp/sg-rules.tsv; \
echo "ap-northeast-1 us-east-1" | tr ' ' '\n' | \
while read region_name
do
    aws ec2 describe-security-groups \
        --region $region_name \
        --query 'SecurityGroups[*].[OwnerId, GroupId, GroupName, VpcId, Description]' \
        --output text | LANG=C sort -u | \
    while read owner_id sgid group_name vpcid description
    do
        aws ec2 describe-security-group-rules \
            --filter Name="group-id",Values=$sgid \
            --region $region_name \
            --output text \
            --query "SecurityGroupRules[*].[\
                SecurityGroupRuleId\
                , GroupId\
                , IsEgress\
                , IpProtocol\
                , FromPort\
                , ToPort\
                , CidrIpv4\
                , CidrIpv6\
                , Description\
                , ReferencedGroupInfo.GroupId\
                , ReferencedGroupInfo.PeeringStatus\
                , ReferencedGroupInfo.VpcId\
                , ReferencedGroupInfo.VpcPeeringConnectionId\
            ]" | sed "s/^/${owner_id}\t${region_name}\t${vpcid}\t${sgid}\t${group_name}\t\"${description}\"\t/"
    done
done >> /tmp/sg-rules.tsv;
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?