LoginSignup
1
2

More than 3 years have passed since last update.

AWS CLIでput-bucket-notification-configurationするときにjqで既存の設定と追加差分を上手いことマージする

Posted at

概要

CLIでのput-bucket-notification-configurationについて、差分でのputを実施。
(もう少し汎用化できるようにも思うが、自分用としてはnotification-configurationで対象を特定する形で良しとする。)

やりたいこと

  • aws cliベースで作業手順を作成している
  • S3へLambdaを発火させるイベントを追加するため、s3apiのput-bucket-notification-configurationを使用した
  • putなので既に存在していた設定が上書きされてしまった
    • 差分ベースの手順作成が難しい
    • 元の設定消えるので普通に事故
  • CLIだけで上手いこと差分をマージしたい!

やったこと

  • --notification-configurationオプションで特定の設定だけを更新するようにした。
    • ここではLambdaFunctionConfigurations
  • jqを使って上手いことマージした。

必要なもの

  • jq
    • windows10で作業したがmac OSでもできることが望ましい
  • aws cli
    • 言わずもがな
  • git bash
    • windows環境でshellを叩きたいので。shellが使えれば何でも。

手順

1. 対象のJSONをgetしてファイルへ出力する

get
# 対象のバケット
TARGET_BUCKET=bucket-foo
aws s3api get-bucket-notification-configuration --bucket ${TARGET_BUCKET} > get-bucket-notification-configuration.json

Lambdaを発火させるイベント追加の場合
->既にsomethingEventAが設定済み

get-bucket-notification-configuration.json
{
  "LambdaFunctionConfigurations": [
    {
      "Id": "somethingEventA",
      "LambdaFunctionArn": "arn:aws:lambda:xxxxxxxxx",
      "Events": [
        "s3:ObjectCreated:Put"
      ],
      "Filter": {
        "Key": {
          "FilterRules": [
            {
              "Name": "prefix",
              "Value": "foo/bar/"
            },
            {
              "Name": "suffix",
              "Value": ".zip"
            }
          ]
        }
      }
    }
  ]
}

2. マージしたい差分ファイルを用意する

--notification-configurationオプションを利用ように構成する

Lambdaを発火させるイベント追加の場合
新たに追加したいsomethingEventBを用意

diff-sample.json
{
  "LambdaFunctionConfigurations": [
    {
      "Id": "somethingEventB",
      "LambdaFunctionArn": "arn:aws:lambda:xxxxxxxxx",
      "Events": [
        "s3:ObjectCreated:Put"
      ],
      "Filter": {
        "Key": {
          "FilterRules": [
            {
              "Name": "prefix",
              "Value": "foo/bar/"
            },
            {
              "Name": "suffix",
              "Value": ".csv"
            }
          ]
        }
      }
    }
  ]
}

3. 2つのファイルをマージ

Lambda関数を送信先にする場合(=LambdaFunctionConfigurationsをマージする場合)

merge
# マージ対象のオブジェクトを指定
TARGET_OBJECT=LambdaFunctionConfigurations
# オブジェクトを指定してマージ
cat get-bucket-notification-configuration.json notification.json | jq -s ".[0].${TARGET_OBJECT} + .[1].${TARGET_OBJECT} | {\"${TARGET_OBJECT}\": .}" > merge-notification.json

4. マージしたファイルをput

--notification-configurationオプションを利用

put
aws s3api put-bucket-notification-configuration --bucket ${TARGET_BUCKET} --notification-configuration file://merge-notification.json

結果

check
aws s3api get-bucket-notification-configuration --bucket ${TARGET_BUCKET}
result
{
  "LambdaFunctionConfigurations": [
    {
      "Id": "somethingEventA",
      "LambdaFunctionArn": "arn:aws:xxxxxx",
      "Events": [
        "s3:ObjectCreated:Put"
      ],
      "Filter": {
        "Key": {
          "FilterRules": [
            {
              "Name": "Prefix",
              "Value": "foo/bar/"
            },
            {
              "Name": "Suffix",
              "Value": ".zip"
            }
          ]
        }
      }
    },
    {
      "Id": "somethingEventB",
      "LambdaFunctionArn": "arn:aws:xxxxxx",
      "Events": [
        "s3:ObjectCreated:Put"
      ],
      "Filter": {
        "Key": {
          "FilterRules": [
            {
              "Name": "prefix",
              "Value": "foo/bar/"
            },
            {
              "Name": "suffix",
              "Value": ".csv"
            }
          ]
        }
      }
    }
  ]
}

参考

1
2
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
1
2