AWS CloudFormation で IAM イベントをモニタリングし、イベント通知を設定するにはどうすればよいですか?
最終更新日: 2022 年 1 月 19日
AWS CloudFormation で AWS Identity and Access Management (IAM) のアクティビティをモニタリングしたいと考えています。例えば、特定の IAM イベントが発生するたびに E メール通知を受信したいと考えています。
簡単な説明
次の解決方法で示されている AWS CloudFormation テンプレートを使用して、IAM イベントをモニタリングし、Amazon EventBridge で通知を設定できます。
以下の点を考慮してください。
- テンプレートは、米国東部 (バージニア北部) - us-east-1 AWS リージョンにデプロイする必要があります。
- AWS::SNS::Topic リソースを変更して、追加の E メールアドレスを含めることができます。
- AWS::Events::Rule リソースを編集して、追加の API コールを追加または削除できます。
解決方法
次のテンプレートには、EventPattern プロパティを含む AWS::Events::Rule リソースが組み込まれています。EventPattern プロパティを使用して、さまざまなイベントソースおよび API コールが、ユースケースの特定のイベントを追加または制限できるようにすることができます。API 呼び出しごとに、イベントにおけるパラメータと情報は異なります。すべてのイベントに対応するフリーサイズのルールを作成することはできません。次のサンプルテンプレートは、多くの異なる API 呼び出しをグループ化する 2 つのルールのみを提供します。ただし、さまざまな API 呼び出しに対して E メールや通知を作成するために必要な数のルールを使用できます。モニタリングする API 呼び出しをカスタマイズおよび定義し、各呼び出しについてカスタマイズされた E メールと情報を定義できます。
1. 次の JSON または YAML バージョンのテンプレートをコピーし、環境の値で更新します。
JSON:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Monitor IAM events with EventBridge rules with AWS CloudFormation. This Stack must be deployed in 'us-east-1' (IAM).",
"Parameters": {
"EmailList": {
"Type": "String",
"Description": "Email to notify!",
"AllowedPattern": "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z]+",
"Default": "mail@example.com"
},
"SNSTopicName": {
"Type": "String",
"Description": "Name for the notification topic.",
"AllowedPattern": "[a-zA-Z0-9_-]+",
"Default": "iam-monitoring-topic"
},
"MonitorStatus": {
"Type": "String",
"Description": "Enable / Disable monitor.",
"AllowedValues": [
"ENABLED",
"DISABLED"
],
"Default": "ENABLED"
}
},
"Resources": {
"SNSMonitoringTopic": {
"Type": "AWS::SNS::Topic",
"Properties": {
"Subscription": [
{
"Endpoint": {
"Ref": "EmailList"
},
"Protocol": "email"
}
],
"TopicName": {
"Fn::Sub": "${AWS::StackName}-${SNSTopicName}"
}
}
},
"SNSMonitoringTopicTopicPolicy": {
"Type": "AWS::SNS::TopicPolicy",
"Properties": {
"Topics": [
{
"Ref": "SNSMonitoringTopic"
}
],
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "SnsIAMTopicPolicy",
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Action": [
"sns:Publish"
],
"Resource": {
"Ref": "SNSMonitoringTopic"
}
},
{
"Sid": "AllowAccessToTopicOwner",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"sns:GetTopicAttributes",
"sns:SetTopicAttributes",
"sns:AddPermission",
"sns:RemovePermission",
"sns:DeleteTopic",
"sns:Subscribe",
"sns:ListSubscriptionsByTopic",
"sns:Publish",
"sns:Receive"
],
"Resource": {
"Ref": "SNSMonitoringTopic"
},
"Condition": {
"StringEquals": {
"AWS:SourceOwner": {
"Ref": "AWS::AccountId"
}
}
}
}
]
}
}
},
"EventRulePolicyMonitor": {
"Type": "AWS::Events::Rule",
"Properties": {
"Name": {
"Fn::Sub": "${AWS::StackName}-policy-monitor"
},
"Description": "This EventBridge rule will capture IAM API Calls and events related to creation and deletion of policies.\n",
"State": {
"Ref": "MonitorStatus"
},
"EventPattern": {
"source": [
"aws.iam"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"iam.amazonaws.com"
],
"eventName": [
"CreatePolicy",
"DeletePolicy",
"PutGroupPolicy",
"DeleteGroupPolicy",
"PutRolePolicy",
"DeleteRolePolicy",
"PutUserPolicy",
"DeleteUserPolicy",
"CreatePolicyVersion",
"DeletePolicyVersion",
"AttachRolePolicy",
"DetachRolePolicy",
"AttachUserPolicy",
"DetachUserPolicy",
"AttachGroupPolicy",
"DetachGroupPolicy"
]
}
},
"Targets": [
{
"Arn": {
"Ref": "SNSMonitoringTopic"
},
"Id": "iam-policy-monitor",
"InputTransformer": {
"InputPathsMap": {
"eventName": "$.detail.eventName",
"policyName": "$.detail.requestParameters.policyName",
"policyArn": "$.detail.requestParameters.policyArn",
"eventTime": "$.detail.eventTime",
"userIdentity": "$.detail.userIdentity.arn",
"sourceIPAddress": "$.detail.sourceIPAddress"
},
"InputTemplate": "\"API Call '<eventName>' was issued on policy '<policyName><policyArn>'. This occurred at '<eventTime>' and was initiated by '<userIdentity>' from IP '<sourceIPAddress>'. Please review the details here: https://console.aws.amazon.com/iam/home?region=us-east-1#/policies/<policyArn>$jsonEditor?section=attached_entities .\"\n"
}
}
]
}
},
"EventRulePrincipalsMonitor": {
"Type": "AWS::Events::Rule",
"Properties": {
"Name": {
"Fn::Sub": "${AWS::StackName}-principals-monitor"
},
"Description": "This EventBridge rule will capture IAM API Calls and events related to creation and deletion of users, groups and roles.",
"State": {
"Ref": "MonitorStatus"
},
"EventPattern": {
"source": [
"aws.iam"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"iam.amazonaws.com"
],
"eventName": [
"CreateUser",
"CreateGroup",
"CreateRole",
"UpdateUser",
"UpdateGroup",
"UpdateRole",
"DeleteUser",
"DeleteGroup",
"DeleteRole"
]
}
},
"Targets": [
{
"Arn": {
"Ref": "SNSMonitoringTopic"
},
"Id": "iam-user-monitor",
"InputTransformer": {
"InputPathsMap": {
"eventName": "$.detail.eventName",
"userName": "$.detail.requestParameters.userName",
"roleName": "$.detail.requestParameters.roleName",
"groupName": "$.detail.requestParameters.groupName",
"eventTime": "$.detail.eventTime",
"userIdentity": "$.detail.userIdentity.arn",
"sourceIPAddress": "$.detail.sourceIPAddress"
},
"InputTemplate": "\"API Call '<eventName>' was issued on '<userName><roleName><groupName>'. This occurred at '<eventTime>' and was initiated by '<userIdentity>' from IP '<sourceIPAddress>'. \"\n"
}
}
]
}
}
}
}
YAML:
AWSTemplateFormatVersion: 2010-09-09
Description: >
- Monitor IAM events with EventBridge rules with AWS CloudFormation.
- This Stack must be deployed in 'us-east-1' (IAM).
Parameters:
EmailList:
Type: String
Description: "Email to notify!"
AllowedPattern: '[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]+'
Default: "mail@example.com"
SNSTopicName:
Type: String
Description: "Name for the notification topic."
AllowedPattern: '[a-zA-Z0-9_-]+'
Default: "iam-monitoring-topic"
MonitorStatus:
Type: String
Description: "Enable / Disable monitor."
AllowedValues:
- ENABLED
- DISABLED
Default: ENABLED
Resources:
SNSMonitoringTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !Ref EmailList
Protocol: email
TopicName: !Sub ${AWS::StackName}-${SNSTopicName}
SNSMonitoringTopicTopicPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
Topics:
- !Ref SNSMonitoringTopic
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: SnsIAMTopicPolicy
Effect: Allow
Principal:
Service: events.amazonaws.com
Action: [ 'sns:Publish' ]
Resource: !Ref SNSMonitoringTopic
- Sid: AllowAccessToTopicOwner
Effect: Allow
Principal:
AWS: '*'
Action: [ 'sns:GetTopicAttributes',
'sns:SetTopicAttributes',
'sns:AddPermission',
'sns:RemovePermission',
'sns:DeleteTopic',
'sns:Subscribe',
'sns:ListSubscriptionsByTopic',
'sns:Publish',
'sns:Receive' ]
Resource: !Ref SNSMonitoringTopic
Condition:
StringEquals:
'AWS:SourceOwner': !Ref 'AWS::AccountId'
EventRulePolicyMonitor:
Type: AWS::Events::Rule
Properties:
Name: !Sub ${AWS::StackName}-policy-monitor
Description: >
This EventBridge rule will capture IAM API Calls and
events related to creation and deletion of policies.
State: !Ref MonitorStatus
EventPattern:
source:
- aws.iam
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- iam.amazonaws.com
eventName:
- CreatePolicy
- DeletePolicy
- PutGroupPolicy
- DeleteGroupPolicy
- PutRolePolicy
- DeleteRolePolicy
- PutUserPolicy
- DeleteUserPolicy
- CreatePolicyVersion
- DeletePolicyVersion
- AttachRolePolicy
- DetachRolePolicy
- AttachUserPolicy
- DetachUserPolicy
- AttachGroupPolicy
- DetachGroupPolicy
Targets:
- Arn:
Ref: SNSMonitoringTopic
Id: iam-policy-monitor
InputTransformer:
InputPathsMap:
eventName: $.detail.eventName
policyName: $.detail.requestParameters.policyName
policyArn: $.detail.requestParameters.policyArn
eventTime: $.detail.eventTime
userIdentity: $.detail.userIdentity.arn
sourceIPAddress: $.detail.sourceIPAddress
InputTemplate: >
"API Call '<eventName>' was issued on policy '<policyName><policyArn>'. This occurred at '<eventTime>' and was initiated by '<userIdentity>' from IP '<sourceIPAddress>'. Please review the details here: https://console.aws.amazon.com/iam/home?region=us-east-1#/policies/<policyArn>$jsonEditor?section=attached_entities ."
EventRulePrincipalsMonitor:
Type: AWS::Events::Rule
Properties:
Name: !Sub ${AWS::StackName}-principals-monitor
Description: >
This EventBridge rule will capture IAM API Calls and
events related to creation and deletion of users, groups
and roles.
State: !Ref MonitorStatus
EventPattern:
source:
- aws.iam
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- iam.amazonaws.com
eventName:
- CreateUser
- CreateGroup
- CreateRole
- UpdateUser
- UpdateGroup
- UpdateRole
- DeleteUser
- DeleteGroup
- DeleteRole
Targets:
- Arn:
Ref: SNSMonitoringTopic
Id: iam-user-monitor
InputTransformer:
InputPathsMap:
eventName: $.detail.eventName
userName: $.detail.requestParameters.userName
roleName: $.detail.requestParameters.roleName
groupName: $.detail.requestParameters.groupName
eventTime: $.detail.eventTime
userIdentity: $.detail.userIdentity.arn
sourceIPAddress: $.detail.sourceIPAddress
InputTemplate: >
"API Call '<eventName>' was issued on '<userName><roleName><groupName>'. This occurred at '<eventTime>' and was initiated by '<userIdentity>' from IP '<sourceIPAddress>'. "
2. AWS CloudFormation コンソールまたは AWS コマンドラインインターフェイス (AWS CLI) を使用してテンプレートをデプロイします。
AWS CloudFormation コンソール
1. テンプレートをダウンロードします。
2. AWS CloudFormation コンソールを開きます。
3. ナビゲーションバーの AWS リージョンセレクターから、[us-east-1] を選択します。
4. [Create stack] (スタックの作成) を選択し、[With new resources (standard)] (新しいリソース (標準)) を選択します。
5. [Specify template] (テンプレートの指定) セクションで、[Upload a template file] (テンプレートファイルのアップロード) を選択します。
6. [Choose file] (ファイルの選択) をクリックし、手順 1 でダウンロードしたテンプレートを選択して、[Next] (次へ) を選択します。
7. [Stack name] (スタック名) セクションの [Stack name] (スタック名) に、スタックの名前を入力します。
8. [Parameters] (パラメータ) セクションの [EmailList] に、通知を受信するメールアドレスを入力します。
9. [MonitorStatus] で、[ENABLED] (有効) を選択します。
10. [SNSTopicName] で、デフォルト名のままにするか、Amazon Simple Notification Service (Amazon SNS) トピックに独自の名前を選択します。
11. セットアップウィザードの残りのステップを完了し、[Create stack] (スタックの作成) をクリックします。
12. (ステップ 8 で入力したメールアドレスを使用して) 受信トレイで確認メールを確認し、E メールに記載の指示に従ってサブスクリプションを確認します。
AWS CLI
注: AWS CLI コマンドの実行時にエラーが発生した場合は、AWS CLI の最新バージョンを使用していることを確認してください。
1. テンプレートをダウンロードし、テンプレートに sample-event-rule-iam-sns.yaml と名前を付けます。
2. AWS CLI を設定します。
3. オペレーティングシステムでコマンドラインを開き、テンプレートがあるフォルダに移動します。
4. 次のコマンドを実行してください。
aws cloudformation --region=us-east-1 \
create-stack \
--stack-name iam-sample-monitor \
--template-body file://sample-event-rule-iam-sns.yaml \
--parameters \
ParameterKey=EmailList,ParameterValue="mail@example.com"
注: mail@example.com は、通知を受信するメールアドレスに置き換えます。
5. (ステップ 4 で入力したメールアドレスを使用して) 受信トレイで確認メールを確認し、E メールに記載の指示に従ってサブスクリプションを確認します。
モニタリング通知をテストする
API Call 'CreatePolicy' was issued on policy 'test-policy'.
This occurred at '2020-11-13T00:00:00Z' and was initiated by 'arn:aws:sts::123456789012:assumed-role/your-role' from IP 'X.Y.Z.T'.
Please review the details here: https://console.aws.amazon.com/iam/home?region=us-east-1#/policies/$jsonEditor?section=attached_entities.