[JAWS-UG CLI] SES:#4 Incoming用ルールセットの有効化、受信テスト (incoming-ses-spam-fileter Python版)


前提条件
========


SESへの権限
-----------

SESに対してフル権限があること。


AWS CLI
-------

以下のバージョンで動作確認済

* AWS CLI 1.10.63

```bash:コマンド:
aws --version
```

```text:結果(例):
      aws-cli/1.10.63 Python/2.7.11 Darwin/15.6.0 botocore/1.4.53
```

バージョンが古い場合は最新版に更新しましょう。

```bash:コマンド:
sudo -H pip install -U awscli
```


0. 準備
=======


0.1. リージョンの決定
---------------------

```bash:変数の設定:
export AWS_DEFAULT_REGION='us-west-2'
```


0.2. 変数の確認
---------------

プロファイルが想定のものになっていることを確認します。

```bash:変数の確認:
aws configure list
```

```text:結果(例):
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       lambdaFull-prjz-mbp13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region        us-west-2        env    AWS_DEFAULT_REGION
```


0.3. ルールセット名の指定
-------------------------

```bash:変数の設定:
SES_RULE_SET_NAME='default-rule-set'
```


1. 事前作業
===========

```bash:コマンド:
aws ses describe-active-receipt-rule-set 
```

```text:結果(例):
      (戻り値なし)
```


2. ルールセットの有効化
=======================

```bash:変数の確認:
cat << ETX

        SES_RULE_SET_NAME: ${SES_RULE_SET_NAME}

ETX
```

```bash:コマンド:
aws ses set-active-receipt-rule-set \
        --rule-set-name ${SES_RULE_SET_NAME}
```

```text:結果(例):
      (戻り値なし)
```


3. 事後作業
===========


3.1. 有効なルールセットの確認
-----------------------------

```bash:コマンド:
aws ses describe-active-receipt-rule-set 
```

```json:結果(例):
      {
        "Rules": [
          {
              "Name": "rule-inbound-spam-filter-20160912",
              "Recipients": [
                  "spa@example.org"
              ],
              "Enabled": true,
              "ScanEnabled": true,
              "Actions": [
                  {
                      "LambdaAction": {
                          "InvocationType": "RequestResponse",
                          "FunctionArn": "arn:aws:lambda:us-west-2:XXXXXXXXXXXX:function:inbound-ses-spam-filter-python-20160912"
                      }
                  }
              ],
              "TlsPolicy": "Optional"
          }
        ],
        "Metadata": {
          "CreatedTimestamp": "2016-09-10T01:23:45.678",
          "Name": "default-rule-set"
        }
      }
```


3.2. メールの受信テスト
-----------------------

```bash:コマンド:
echo ${SES_RECIPIENT}
```

メールクライアントから ${SES_RECIPIENT} 宛にテストメールを送信します。


3.3. ログの確認
---------------

```bash:変数の設定:
LOG_GROUP_NAME="/aws/lambda/${LAMBDA_FUNC_NAME}"
TIME_BEFORE_MIN='5'
```

```bash:変数の設定(OSX/BSD系OSの場合)
LOG_START_TIME=$( date -u -v-${TIME_BEFORE_MIN}M '+%s' )000 \
        && echo ${LOG_START_TIME}
```

```bash:変数の設定(Linux系OSの場合)
LOG_START_TIME=$( date -u -d "${TIME_BEFORE_MIN} mins ago" '+%s' )000 \
        && echo ${LOG_START_TIME}
```

```bash:変数の確認:
cat << ETX

        LOG_START_TIME: ${LOG_START_TIME}

ETX
```

```bash:コマンド:
aws logs filter-log-events \
        --log-group-name ${LOG_GROUP_NAME} \
        --start-time ${LOG_START_TIME}
```

```json:結果(例):
      [
        {
          "ingestionTime": 1473512559234,
          "timestamp": 1473512559200,
          "message": "START RequestId: d9aaa1cb-7756-11e6-930f-2f1cc5f7b514 Version: $LATESTn",
          "eventId": "32860428129553970586636407428524835670122560352684081152",
          "logStreamName": "2016/09/10/[$LATEST]b90e2350d9c243b0acbbc47e2cc390c4"
        },
        {
          "ingestionTime": 1473512574300,
          "timestamp": 1473512559201,
          "message": "2016-09-10T13:02:39.201053 Starting - inbound-ses-spam-filtern",
          "eventId": "32860428129576271331834938069880484760361662665159737344",
          "logStreamName": "2016/09/10/[$LATEST]b90e2350d9c243b0acbbc47e2cc390c4"
        },
        {
          "ingestionTime": 1473512574300,
          "timestamp": 1473512559201,
          "message": "2016-09-10T13:02:39.201108 Processing message: ebkcbvva9vrb53lhmbjuf2fjmla3v92u0ng70jg1n",
          "eventId": "32860428129576271331834938069880484760361662665159737345",
          "logStreamName": "2016/09/10/[$LATEST]b90e2350d9c243b0acbbc47e2cc390c4"
        },
        {
          "ingestionTime": 1473512574300,
          "timestamp": 1473512559201,
          "message": "2016-09-10T13:02:39.201175 Accepting message: ebkcbvva9vrb53lhmbjuf2fjmla3v92u0ng70jg1n",
          "eventId": "32860428129576271331834938069880484760361662665159737346",
          "logStreamName": "2016/09/10/[$LATEST]b90e2350d9c243b0acbbc47e2cc390c4"
        },
        {
          "ingestionTime": 1473512574300,
          "timestamp": 1473512559207,
          "message": "END RequestId: d9aaa1cb-7756-11e6-930f-2f1cc5f7b514n",
          "eventId": "32860428129710075803026121808729699069997552834195619843",
          "logStreamName": "2016/09/10/[$LATEST]b90e2350d9c243b0acbbc47e2cc390c4"
        },
        {
          "ingestionTime": 1473512574300,
          "timestamp": 1473512559207,
          "message": "REPORT RequestId: d9aaa1cb-7756-11e6-930f-2f1cc5f7b514tDuration: 0.75 mstBilled Duration: 100 ms tMemory Size: 128 MBtMax Memory Used: 43 MBtn",
          "eventId": "32860428129710075803026121808729699069997552834195619844",
          "logStreamName": "2016/09/10/[$LATEST]b90e2350d9c243b0acbbc47e2cc390c4"
        }
      ]
```


完了
====