入力トランスフォーマーを使用して、 API コールの人が判読できる CloudWatch イベント通知を設定する方法を教えてください。
最終更新日: 2020 年 7 月 2 日
Amazon Elastic Compute Cloud (Amazon EC2) のセキュリティグループ API コールで、人が判読できる 通知を Amazon CloudWatch Events で受け取ることを考えています。入力トランスフォーマーを使用して、JSON ログイベントの詳細全体は受け取らずに通知を設定するにはどうすればよいですか?
簡単な説明
イベントパターンを使って、AWS CloudTrail ログデータでセキュリティグループ関連の API コールに関する Amazon CloudWatch Events 通知を作成できます。次に、入力トランスフォーマーを使用して、これらのイベントを JSON から人が判読できるようにカスタマイズした通知に変更できます。詳しくは、チュートリアル: イベントターゲットに渡されるものを入力トランスフォーマーを使用してカスタマイズするを参照してください。
解決方法
サンプルイベント
次のサンプルイベントでは、ユーザーが Virtual Private Cloud (VPC) で CreateSecurityGroup を使用してセキュリティグループを作成したときに、CloudTrail ログイベントが取得されます。次に、このイベントは、以下のように JSON 形式で CloudWatch に公開されます。
{
"version": "0",
"id": "41dff147-cfbc-492a-9a98-9dd00d082417",
"detail-type": "AWS API Call via CloudTrail",
"source": "aws.ec2",
"account": "123456789012",
"time": "2017-07-14T16:36:23Z",
"region": "us-east-1",
"resources": [],
"detail": {
"eventVersion": "1.05",
"userIdentity": {
"type": "Root",
"principalId": "123456789012",
"arn": "arn:aws:iam::123456789012:root",
"accountId": "123456789012",
"accessKeyId": "ASIAIRP4G1234567891Q",
"userName": "iamuser",
"sessionContext": {
"attributes": {
"mfaAuthenticated": "false",
"creationDate": "2017-07-14T16:27:38Z"
}
}
},
"eventTime": "2017-07-14T16:36:23Z",
"eventSource": "ec2.amazonaws.com",
"eventName": "CreateSecurityGroup",
"awsRegion": "us-east-1",
"sourceIPAddress": "72.21.196.64",
"userAgent": "console.ec2.amazonaws.com",
"requestParameters": {
"groupName": "test",
"groupDescription": "test",
"vpcId": "vpc-54a4232d"
},
"responseElements": {
"_return": true,
"groupId": "sg-82d531f3"
},
"requestID": "a4609e55-69ac-4a2d-8057-843dc8b4658b",
"eventID": "7774b898-59c1-48a5-9c41-af1bcaa04612",
"eventType": "AwsApiCall"
}
}
解決例
次の解決例を、JSON 形式で CloudWatch Events から独自にカスタマイズした、人による判読可能な通知を作成するモデルとして使用します。
1. セキュリティグループ関連の API コールに CloudWatch Events ルールを作成します。[イベントパターン] で、[カスタムイベントパターンの作成] を選択します。ユースケースのサンプルイベントのパターンを忘れず変更してください。通知の例は CreateSecurityGroup に対するものですが、次のサンプルイベントのパターンには、セキュリティグループ関連の API コールのアクションがすべて含まれています。
{
"source": [
"aws.ec2"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"ec2.amazonaws.com"
],
"eventName": [
"CreateSecurityGroup",
"AuthorizeSecurityGroupEgress",
"AuthorizeSecurityGroupIngress",
"DeleteSecurityGroup",
"RevokeSecurityGroupEgress",
"RevokeSecurityGroupIngress",
"DescribeStaleSecurityGroups",
"DescribeSecurityGroups",
"DescribeSecurityGroupReferences"
]
}
}
2. ルールの [ターゲット] で、[設定入力] の下にある [入力トランスフォーマー] を選択します。
3. 最初の入力ボックスで、[入力パス] を設定します。通知で使用するキーと値のペアを含めます。ご自身のユースケースに合わせて、次の入力パスのサンプルを変更してください。
{"sgname":"$.detail.requestParameters.groupName","sourceip":"$.detail.sourceIPAddress","vpcid":"$.detail.requestParameters.vpcId","action":"$.detail.eventName","sgid":"$.detail.responseElements.groupId","time":"$.detail.eventTime","region":"$.detail.awsRegion","user":"$.detail.userIdentity.userName","reqid":"$.detail.requestID"}
注意: CloudWatch Events は現在、入力パスマップで最大 10 個の変数をサポートしています。10 個を超える変数を抽出するには、イベント全体を AWS Lambda 関数に渡して解析します。次に、Amazon Simple Notification Service (Amazon SNS) に発行するのに必要な形式でメッセージ本文を作成します。
4. 2 番目の入力ボックスで、[入力テンプレート] を設定します。入力パスからの値のプレースホルダーがある文字列を含めます。この文字列は、人が判読できる通知テキストです。ご自身のユースケースに合わせて、次の入力テンプレートのサンプルを変更してください。
"User <user> has executed API call <action> from source IP <sourceip> on security group <sgid>/<sgname> that belongs to vpc <vpcid> in region <region> at time(UTC) <time>. The Request ID is <reqid>."
注意: 改行を含むテンプレートを保存する際に Invalid InputTemplate エラーが表示された場合は、各行は必ず二重引用符で閉じるようにしてください。例:
"<type> <arn> has executed action API <action> on Security Group <sgid> located in AWS region <region> at <time> (UTC)."
"Event Details:"
"Request ID: <requestid>"
"Event ID: <eventid>"
"Event Source: <eventsource>"
"Event Type: <eventtype>"
5. [Configure details] を選択します。
6. ルールの名前と説明を指定します。
7. [Create rule] を選択します。