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?

セキュリティグループのパラメーターシート作成

0
Posted at

課題

エクセルシートのSGのパラメーターシートを更新する必要がある。
以下のシート構成にする。

① セキュリティグループ一覧
SG名・SG ID・VPCなどの基本情報

② サーバー別通信ルール
サーバー単位でSGを確認

③ インバウンド一覧
全SGの受信ルールを一覧化

④ アウトバウンド一覧
全SGの送信ルールを一覧化

それぞれを更新するための情報を出力するコマンドを書いていく。

① セキュリティグループ一覧を出力

aws ec2 describe-security-groups \
  --query 'sort_by(SecurityGroups,&GroupName)[*].[GroupName,GroupId,VpcId,Description]' \
  --output text | sed 's/\t/,/g' > security-groups.csv

『① セキュリティグループ一覧』のコマンド説明

コマンド 説明
aws ec2 describe-security-groups セキュリティグループの情報を取得する
--query 取得する項目や並び順を指定する
sort_by(SecurityGroups,&GroupName) SG名の昇順に並び替える
GroupName SG名を取得する
GroupId SG IDを取得する
VpcId VPC IDを取得する
Description 説明文を取得する
--output text 取得結果をテキスト形式で出力する
sed 's/\t/,/g' タブ区切りをカンマ区切りに変換する
> security-groups.csv 実行結果をCSVファイルに保存する(既存ファイルは上書き)

② サーバー別通信ルールを出力

aws ec2 describe-instances --output json | \
jq -r '
  .Reservations[].Instances[] |
  . as $ec2 |
  .NetworkInterfaces[].Groups[] |
  [
    ([$ec2.Tags[]? | select(.Key=="Name") | .Value][0] // ""),
    $ec2.InstanceId,
    .GroupName,
    .GroupId
  ] | @csv
' | sort -u > server-security-groups.csv

『② サーバー別通信ルール』のコマンド説明

コマンド 説明
aws ec2 describe-instances EC2インスタンスの情報を取得する
--output json 取得結果をJSON形式で出力する
| 前のコマンドの結果を次のコマンドに渡す
jq -r JSONデータを加工して出力する
.Reservations[].Instances[] EC2インスタンスを1台ずつ取得する
. as $ec2 EC2の情報を一時的に変数へ保存する
.NetworkInterfaces[].Groups[] 各ENIに設定されたセキュリティグループを取得する
.Tags[]? | select(.Key=="Name") Nameタグを検索する
.Value][0] // "" Nameタグの値を取得する。存在しない場合は空欄にする
$ec2.InstanceId インスタンスIDを取得する
.GroupName セキュリティグループ名を取得する
.GroupId セキュリティグループIDを取得する
@csv 取得結果をCSV形式に変換する
sort -u 重複する行を削除し、並び替える
> server-security-groups.csv CSVファイルに保存する(既存ファイルは上書き)

③ インバウンド一覧を出力

aws ec2 describe-security-groups --output json > /tmp/sg-list.json

aws ec2 describe-security-group-rules --output json | \
jq -r --slurpfile sg /tmp/sg-list.json '
  (.SecurityGroupRules[] | select(.IsEgress == false)) |
  . as $rule |
  [
    ([$sg[0].SecurityGroups[] |
      select(.GroupId == $rule.GroupId) |
      .GroupName][0] // ""),
    .GroupId,
    .SecurityGroupRuleId,
    .IpProtocol,
    (.FromPort // ""),
    (.ToPort // ""),
    (.CidrIpv4 // .CidrIpv6 // .ReferencedGroupInfo.GroupId // .PrefixListId // ""),
    (.Description // "")
  ] | @csv
' > inbound-rules.csv

『③ インバウンド一覧』のコマンド説明

コマンド 説明
aws ec2 describe-security-groups セキュリティグループの基本情報を取得する
--output json 取得結果をJSON形式で出力する
> /tmp/sg-list.json SGの基本情報を一時ファイルに保存する
aws ec2 describe-security-group-rules セキュリティグループの通信ルールを取得する
| 前のコマンドの結果を次のコマンドに渡す
jq -r JSONデータを加工して出力する
--slurpfile sg /tmp/sg-list.json SGの基本情報を読み込み、変数として利用する
.SecurityGroupRules[] セキュリティグループのルールを1件ずつ取得する
select(.IsEgress == false) インバウンドルールのみを抽出する
. as $rule ルール情報を一時的に変数へ保存する
select(.GroupId == $rule.GroupId) SG IDが一致するセキュリティグループを検索する
.GroupName セキュリティグループ名を取得する
.GroupId セキュリティグループIDを取得する
.SecurityGroupRuleId セキュリティグループルールIDを取得する
.IpProtocol プロトコルを取得する
.FromPort // "" 開始ポートを取得する。存在しない場合は空欄にする
.ToPort // "" 終了ポートを取得する。存在しない場合は空欄にする
.CidrIpv4 // .CidrIpv6 // .ReferencedGroupInfo.GroupId // .PrefixListId // "" 送信元のIPv4、IPv6、SG ID、プレフィックスリストIDのいずれかを取得する
.Description // "" ルールの説明を取得する。存在しない場合は空欄にする
@csv 取得結果をCSV形式に変換する
> inbound-rules.csv CSVファイルに保存する(既存ファイルは上書き)

④ アウトバウンド一覧を出力

aws ec2 describe-security-groups --output json > /tmp/sg-list.json

aws ec2 describe-security-group-rules --output json | \
jq -r --slurpfile sg /tmp/sg-list.json '
  .SecurityGroupRules[] |
  select(.IsEgress == true) |
  . as $rule |
  [
    ([$sg[0].SecurityGroups[] |
      select(.GroupId == $rule.GroupId) |
      .GroupName][0] // ""),
    .GroupId,
    .SecurityGroupRuleId,
    .IpProtocol,
    (.FromPort // ""),
    (.ToPort // ""),
    (.CidrIpv4 // .CidrIpv6 // .ReferencedGroupInfo.GroupId // .PrefixListId // ""),
    (.Description // "")
  ] | @csv
' > outbound-rules.csv

『④ アウトバウンド一覧』のコマンド説明

コマンド 説明
aws ec2 describe-security-groups セキュリティグループの基本情報を取得する
--output json 取得結果をJSON形式で出力する
> /tmp/sg-list.json SGの基本情報を一時ファイルに保存する
aws ec2 describe-security-group-rules セキュリティグループの通信ルールを取得する
| 前のコマンドの結果を次のコマンドに渡す
jq -r JSONデータを加工して出力する
--slurpfile sg /tmp/sg-list.json SGの基本情報を読み込み、変数として利用する
.SecurityGroupRules[] セキュリティグループのルールを1件ずつ取得する
select(.IsEgress == true) アウトバウンドルールのみを抽出する
. as $rule ルール情報を一時的に変数へ保存する
select(.GroupId == $rule.GroupId) SG IDが一致するセキュリティグループを検索する
.GroupName セキュリティグループ名を取得する
.GroupId セキュリティグループIDを取得する
.SecurityGroupRuleId セキュリティグループルールIDを取得する
.IpProtocol プロトコルを取得する
.FromPort // "" 開始ポートを取得する。存在しない場合は空欄にする
.ToPort // "" 終了ポートを取得する。存在しない場合は空欄にする
.CidrIpv4 // .CidrIpv6 // .ReferencedGroupInfo.GroupId // .PrefixListId // "" 送信先のIPv4、IPv6、SG ID、プレフィックスリストIDのいずれかを取得する
.Description // "" ルールの説明を取得する。存在しない場合は空欄にする
@csv 取得結果をCSV形式に変換する
> outbound-rules.csv CSVファイルに保存する(既存ファイルは上書き)
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?