はじめに
開発環境などで誤ってメール送信しないようにするための設定です。
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
- "*"