We are now going to create a filter within our CloudWatch Log Group. This filter will generate a CloudWatch metric which we will use as to create our alarm.
To create your filter, complete the following configuration steps:
Navigate to CloudWatch in your console and click on Log Groups on the side menu.
Locate the pattern1-logging-loggroup you created in the previous section and click on the the log group as shown:
Select the tick box beside the log groups, click on Actions and then Create metric filter as shown:
Enter below filter under Filter pattern
{ $.errorCode = "*" && $.eventSource= "kms.amazonaws.com" && $.userIdentity.sessionContext.sessionIssuer.arn = "<ECS Task Role ARN>" }
Note: Replace < ECS Task Role ARN > with the value of OutputPattern1ECSTaskRole. This value was provided in the Output section in the pattern1-app. If you need a reminder, you can refer to section 2.3.
When you have completed this, you can click Next.
It is important at this stage to understand the importance of filtering using this rule. The filter which we created in the previous step will look for all error codes which come from an eventSource of kms.amazonaws.com where the identity of the request matches the ECS Task role ARN.
This means that When KMS triggers an event by our application, the event registered within CloudTrail will look like this:
{
"eventVersion": "1.05",
"userIdentity": {
"type": "AssumedRole",
...
"sessionContext": {
"sessionIssuer": {
"type": "Role",
"principalId": "AROAQKTRYBJEYHGY4HLFO",
"arn": "arn:aws:iam::xxxxxxxxxxx:role/pattern1-application-Pattern1ECSTaskRole",
"accountId": "xxxxxxxxxxx",
"userName": "pattern1-application-Pattern1ECSTaskRole"
},
...
}
},
"eventTime": "2020-11-16T22:25:39Z",
"eventSource": "kms.amazonaws.com",
"eventName": "Decrypt",
"awsRegion": "ap-southeast-2",
"errorCode": "IncorrectKeyException",
"errorMessage": "The key ID in the request does not identify a CMK that can perform this operation.",
.....
"responseElements": null,
"requestID": "11748bbd-ddcd-4ee2-9f42-9cec69f414b1",
"eventID": "1f620618-46e5-4f78-93cc-0b7bccfff5d2",
"readOnly": true,
"eventType": "AwsApiCall",
"recipientAccountId": "xxxxxxxxxxx"
}
Our configured filter rule will perform filtering based on the JSON keys which are presented by the event as follows:
$.eventSource
: Describes the EventSource of “kms.amazon.com” signifying that it is a KMS event.$.errorCode
: Describes any value with key “ErrorCode” signifying that an error event is being generated.$.userIdentity.sessionContext.sessionIssuer.arn
: filters for the the userIdentity that executes the event. This is the assumed role that is used by ECS, which indicates that this call was made from our application running in the container.Now that we have explained the details of how our filter operates, we can complete the configuration.
In the Assign Metric form, enter the following configuration detail:
pattern1-logging-metricfilter
as the Filter name.Pattern1Application/KMSSecurity
as the Metric namespace.KMSSecurityError
as the Metric name.1
as the Metric Value.Your completed configuration should match the following screenshot:
When you have verified your configuration, click Next and Create metric filter
Once your Metric filter has been created, you should be able to view it under the Metric filters tab of your LogGroups. We will now create the Metric Alarm from this filter.
Complete the following steps:
Select the Metric filter you just created, then click on CreateAlarm as shown:
Change the name of the metric to KMSsecurityError
and set the Period to 10 seconds as shown:
Within the conditions dialog box, configure the following:
Your configuration should match the following screenshot:
When your configuration is complete, click Next
In the Notification dialog box, configure the following:
pattern1-logging-topic
as the topic name.When your configuration is complete, click Create topic then click Next
Complete the following configuration to complete the alarm setup:
pattern1-logging-alarm
as the Alarm name and click NextThis completes the creation of the filter and alarm for the lab. Proceed to Section 5 to test functionality.
END OF SECTION 4