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

AWS SESで送信先をIAMで制限する方法

1
Posted at

はじめに

開発環境などで誤ってメール送信しないようにするための設定です。

IAMポリシーの設定内容

送信先のメールアドレスのドメインをConditionで指定します。
条件を満たさない場合(異なるドメインの場合)は、権限エラーになります。

※送信元(From)も制限したい場合はses:FromAddressを使います。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowSendMail"
            "Effect": "Allow",
            "Action": [
                "ses:SendEmail",
                "ses:SendRawEmail"
            ],
            "Resource": [
                "arn:aws:ses:*:XXXXXXXXXXXX:identity/*",
                "arn:aws:ses:*:XXXXXXXXXXXX:configuration-set/*"
            ],
            "Condition": {
                "ForAnyValue:StringLike": {
                    "ses:Recipients": [
                        "*@xxxxxxx.xx.xx",
                        "*@yyyyyyy.yy.yy"
                    ]
                }
            },
        }
    ]
}

CloudForamationsでの作成方法

運用環境(開発/ステージ/本番)により制限する対象が変わることもあるので、以下のように作成することで柔軟に対応できます。

Parameters:

  # 許可送信元メールアドレス配列
  AllowToMailAddresses:
    Type: CommaDelimitedList
    Default: "*@xxxxxxx.xx.xx, *@yyyyyyy.yy.yy"

Conditions:
  # 許可する送信元メールアドレスが存在するか判定
  IsRecipientsSpecified:
    Fn::Not:
      - Fn::Equals:
          - !Join [",", !Ref AllowToMailAddresses]
          - ""

Resources:

  XxxxRole:
    Type: AWS::IAM::Role
    Properties:
      # (省略)
              - Sid: AllowSendMail
                Effect: Allow
                Action:
                  - ses:SendEmail
                  - ses:SendRawEmail
                Resource:
                  - !Sub arn:aws:ses:*:${AWS::AccountId}:identity/*
                  - !Sub arn:aws:ses:*:${AWS::AccountId}:configuration-set/*
                Condition:
                  ForAnyValue:StringLike:
                    ses:Recipients:
                      # AllowToMailAddressesパラメータで指定が無い場合は「*」で全許可
                      !If 
                        - IsRecipientsSpecified
                        - !Ref AllowToMailAddresses
                        - "*"
1
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
1
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?