メインコンテンツへスキップ

AWSのセキュリティグループのインバウンドルールを、AWS CLIとWindowsバッチで更新する

    自分だけしか使わないhttpsサイト、ポート変更したsshログインについて、自宅のダイアルアップを繋ぎ直した際に、毎回グローバルIPを「確認くん」で調べて、AWSのWeb管理画面で変更する。

    ぶっちゃけ疲れた。

    なので、WindowsのAWS CLIと、バッチで何とかする。

    1. 元々のインバウンドルール

    IPを絞って、かつSSHはポートも変更してる。

    画像1

    2. AWS CLIのインストール

    まぁこの辺は「windows aws cli インストール」とかでググって。

    3. IAMのユーザとロール

    AWSのユーザを作って、ロールはEC2の権限を全部振ってみた。
    もっと絞れるかも?だけど、ログインとかもしないし、あまり気にしない。

    画像2

    4. AWS CLIの設定

    aws configureで、ディフォルトの言語は「json」を指定。
    この後、コマンドの結果をJSONで受け取って、再利用する部分がある。

    C:\Users\xxxxx>aws configure
    AWS Access Key ID [****************PQAY]:
    AWS Secret Access Key [****************WMtW]:
    Default region name [ap-northeast-1]:
    Default output format [json]:

    5. 現行のルールの削除

    Windowsコマンドプロンプトの制約がめんどい。

    変数で改行は削除したいし、echoで半角カンマはそのまま使えないし…
    aws cliでJSON実行するところで半角ダブルクォートはエスケープしないとダメだし。

    で、紆余曲折で以下のような感じに。
    %%Aの部分は、バッチファイルにしない場合はパーセント記号を1つにしないとエラー出るから注意ね。

    set groupId=sg-xxxxxxxxxxxxx
    del res.json
    for /f "usebackq delims=" %%A in (`echo {"GroupId": "%groupId%"^, "IpPermissions":`) do <NUL set /p="%%A")>> res.json
    for /f "usebackq delims=" %%A in (`aws ec2 describe-security-groups --group-id %groupId% --query "SecurityGroups[0].IpPermissions"`) do <NUL set /p="%%A")>> res.json
    echo } >> res.json
    for /f "usebackq delims=" %%A in (res.json) do set json=%%A
    del res.json
    aws ec2 revoke-security-group-ingress --cli-input-json "%json:"=\"%"

    6. 今のグローバルIPを変数にぶち込む

    以下のサイトそのまんまで。

    7. インバウンドルールの再設定

    ここはひねりは無し。
    ルールに対してDescription入れてるくらい?

    set groupId=sg-xxxxxxxxxxxxxx
    set httpPort=80
    set httpsPort=443
    set sshPort=xxx22
    aws ec2 revoke-security-group-ingress --cli-input-json "%json:"=\"%"
    for /f "usebackq" %%A in (`curl https://api.ipify.org`) do set ipAddr=%%A
    aws ec2 authorize-security-group-ingress --group-id %groupId% --ip-permissions IpProtocol=tcp,FromPort=%httpPort%,ToPort=%httpPort%,IpRanges="[{CidrIp=0.0.0.0/0,Description=http}]"
    aws ec2 authorize-security-group-ingress --group-id %groupId% --ip-permissions IpProtocol=tcp,FromPort=%httpsPort%,ToPort=%httpsPort%,IpRanges="[{CidrIp=%ipAddr%/32,Description=https}]"
    aws ec2 authorize-security-group-ingress --group-id %groupId% --ip-permissions IpProtocol=tcp,FromPort=%sshPort%,ToPort=%sshPort%,IpRanges="[{CidrIp=%ipAddr%/32,Description=SSH}]"

    8. ソース全体

    こんな感じ。
    いやー、ショートカットをタスクバーにピン止めしたから、IPの設定が超絶楽になったわ。

    @echo off
    set groupId=sg-xxxxxxxxxxxxxxxxxxxx
    set httpPort=80
    set httpsPort=443
    set sshPort=xx22
    del res.json
    for /f "usebackq delims=" %%A in (`echo {"GroupId": "%groupId%"^, "IpPermissions":`) do <NUL set /p="%%A")>> res.json
    for /f "usebackq delims=" %%A in (`aws ec2 describe-security-groups --group-id %groupId% --query "SecurityGroups[0].IpPermissions"`) do <NUL set /p="%%A")>> res.json
    echo } >> res.json
    for /f "usebackq delims=" %%A in (res.json) do set json=%%A
    del res.json
    aws ec2 revoke-security-group-ingress --cli-input-json "%json:"=\"%"
    for /f "usebackq" %%A in (`curl https://api.ipify.org`) do set ipAddr=%%A
    aws ec2 authorize-security-group-ingress --group-id %groupId% --ip-permissions IpProtocol=tcp,FromPort=%httpPort%,ToPort=%httpPort%,IpRanges="[{CidrIp=0.0.0.0/0,Description=http}]"
    aws ec2 authorize-security-group-ingress --group-id %groupId% --ip-permissions IpProtocol=tcp,FromPort=%httpsPort%,ToPort=%httpsPort%,IpRanges="[{CidrIp=%ipAddr%/32,Description=https}]"
    aws ec2 authorize-security-group-ingress --group-id %groupId% --ip-permissions IpProtocol=tcp,FromPort=%sshPort%,ToPort=%sshPort%,IpRanges="[{CidrIp=%ipAddr%/32,Description=SSH}]"

    9. Windowsのコマンドラインとかバッチファイルでの細かいテクニックのメモ

    あんまり使ってないから色々忘れてた。
    かなりググった。
    メモメモ。

    変数の代入はset 変数名=内容。ダブルクォートとかは不要。
    set groupId=sg-xxxxxxxxxxxxxxxxxxxx

    変数の利用は%変数名%
    echoでカンマとかの特殊文字は半角のキャレットでエスケープが必要。
    echo {"GroupId": "%groupId%"^, "IpPermissions":

    forで回して改行を削るには"usebackq delims="とか<NUL set /p="%%A"とか。細かいところはよくわかってない。
    for /f "usebackq delims=" %%A in (`echo {"GroupId": "%groupId%"^, "IpPermissions":`) do <NUL set /p="%%A")>> res.json

    変数の利用時に半角コロン、変換前、イコール、変換後で変換が可能。
    下記の場合は、json変数に対して、半角のダブルクォートの前に半角円マークを付けて、出力してる。
    aws ec2 revoke-security-group-ingress --cli-input-json "%json:"=\"%"

    あなたへのおすすめ