├── .github └── PULL_REQUEST_TEMPLATE.md ├── Documentation └── architecture.png ├── LICENSE ├── NOTICE ├── README.md └── Setup ├── install.py ├── uninstall.py ├── xraycloudwatchevent.json ├── xraycloudwatchevent.py └── xraycloudwatchevent.template /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | *Issue #, if available:* 2 | 3 | *Description of changes:* 4 | 5 | 6 | By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. 7 | -------------------------------------------------------------------------------- /Documentation/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/aws-xray-cloudwatch-event/f27361fbf9106ac5965fc56cefd5bca50cf906da/Documentation/architecture.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | AWS Xray Cloudwatch Event 2 | Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Notification & Alarms for AWS X-Ray using Amazon CloudWatch 2 | 3 | ## License 4 | 5 | This sample application is licensed under the Apache 2.0 License. 6 | 7 | ## Before you begin 8 | 1. You will need an active AWS account to proceed. Create one at https://aws.amazon.com/ . 9 | 2. Permission to run AWS CloudFormation template that will create Amazon S3 buckets, AWS Lambda functions, Amazon CloudWatch events, CloudWatch rules and AWS IAM Roles. To learn more about CloudFormation, see https://aws.amazon.com/cloudformation/. 10 | 3. You will need Python to deploy the CloudFormation template and run the sample app. You can download and install Python from https://www.python.org/. 11 | 4. To generate alerts and notifications, you will need an application that is instrumented and sending data to X-Ray. To get started with AWS X-Ray please visit: https://aws.amazon.com/xray/ 12 | 13 | ## Overview 14 | This sample app will help you setup SMS and email alerts when services in your application have elevated latency, or error and fault rates. 15 | 16 | ![Alt text](/Documentation/architecture.png?raw=true "Sample app architecture and overview") 17 | 18 | ## Information on pricing 19 | This sample app uses AWS X-Ray, Amazon CloudWatch, AWS Lambda and Amazon SNS. You will be charged based on pricing for each of these individual services. Please refer to the overview section above to understand the architecture of this app and the resources it'll create. The pricing information for the services used in this application is as follows: 20 | 21 | AWS X-Ray: https://aws.amazon.com/xray/pricing/ 22 | Amazon CloudWatch: https://aws.amazon.com/cloudwatch/pricing/ 23 | AWS Lambda: https://aws.amazon.com/lambda/pricing/ 24 | Amazon SNS: https://aws.amazon.com/sns/pricing/ 25 | 26 | ## Getting started 27 | ### a. Information for notification 28 | The sample app requires the following information to successfully notify you. Please note that your AWS account will be charged by Amazon SNS for these notifications beyond the free tier. For Amazon SNS pricing information please visit: https://aws.amazon.com/sns/pricing/ 29 | 1. You can configure notifications as follows: 30 | ``` 31 | { 32 | "alerts": 33 | { 34 | "default": 35 | { 36 | "responseunitthreshold":, 37 | "errorpercentagethreshold":, 38 | "faultpercentagethreshold":, 39 | "throttlepercentagethreshold": 40 | } 41 | }, 42 | "analyzeservicemapminutes":, 43 | "evaluationperiodforcwalarm":, 44 | "communications": 45 | { 46 | "sms": 47 | [ 48 | "type:Phone number (without dashes) with country and area code. Format: 19876543210", 49 | "type:Phone number (without dashes) with country and area code. Format: 19876543210" 50 | ], 51 | "email": 52 | [ 53 | "type: email. Format: abcd@efgh.com", 54 | "type: email. Format: abcd@efgh.com" 55 | ] 56 | } 57 | 58 | } 59 | ``` 60 | 2. (optional) If you would like to only get notified for specific services in your application, you can add individual service sections: 61 | ``` 62 | "alerts": 63 | { 64 | ": 65 | { 66 | "responseminutesthreshold":, 67 | "errorpercentagethreshold":, 68 | "faultpercentagethreshold":, 69 | "throttlepercentagethreshold":, 70 | "type": 71 | }, 72 | ": 73 | { 74 | "responseminutesthreshold":, 75 | "errorpercentagethreshold":, 76 | "faultpercentagethreshold":, 77 | "throttlepercentagethreshold":, 78 | "type": 79 | } 80 | } 81 | ``` 82 | 83 | ### b. Install the sample app 84 | The CloudFormation template will create the required resources such as S3 buckets, CloudWatch events, CloudWatch rules, IAM roles and Lambda function. Note: You have to update xraycloudwatchevent.json as described above prior to installing the sample app. 85 | 86 | 1. Git clone this repository. 87 | 2. Go to Setup/ directory. 88 | ``` 89 | cd Setup/ 90 | ``` 91 | 3. Run install.py script 92 | ``` 93 | python install.py 94 | ``` 95 | 4. Provide your sample app name and region when requested by the install.py script. 96 | 97 | 98 | ### c. Uninstall the sample app 99 | Uninstalling the sample app is easy as well. It removes any relevant S3 bucket and resources created by CloudFormation for this app. 100 | 101 | 1. Go to Setup/ directory. 102 | ``` 103 | cd Setup/ 104 | ``` 105 | 2. Run uninstall.py script 106 | ``` 107 | python uninstall.py 108 | ``` 109 | 3. Provide your sample app name and region when requested by the uninstall.py script. 110 | 111 | ## Using the sample app 112 | 1. The sample app is an AWS Lambda function, which is triggered by a CloudWatch event at the scheduled time defined by you. 113 | 2. This sample app analyzes your AWS X-Ray service graph, aggregated at the provided time window and sends out a CloudWatch event in case the latency, error or fault rates breach the threshold. You can define a custom threshold using the xraycloudwatch.json file. 114 | 3. On matching the pattern defined in the CloudWatch Rule, the CloudWatch Event will invoke the SNS topic to send a SMS message to the provided phone number. 115 | 4. The CloudWatch Alarm will be trigerred if TriggeredRules > 0 in the evaluation period, which is also defined in the xraycloudwatch.json file, to invoke another SNS topic that sends out an email. 116 | -------------------------------------------------------------------------------- /Setup/install.py: -------------------------------------------------------------------------------- 1 | ''' 2 | /*Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"). 5 | You may not use this file except in compliance with the License. 6 | A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the "license" file accompanying this file. 7 | This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 8 | See the License for the specific language governing permissions and limitations under the License.*/ 9 | ''' 10 | 11 | import os 12 | import json 13 | 14 | appname = raw_input("Enter a unique name for your app {pattern:^[a-z0-9]+$}:") 15 | 16 | print("Your appname:"+appname+" will be used for naming your CloudFormation stack, public s3 bucket and as a prefix as a prefix to identify all the Lambda functions and IAM Roles associated with your app") 17 | 18 | print("The region should be the same as your service running in X-Ray. If your service runs in multiple regions then you should have multiple instances of this sample app running in each region.") 19 | 20 | sampleappregion = raw_input("Enter the aws region where you would like this sample app to be deployed. (Default: us-west-2): ") or "us-west-2" 21 | 22 | # Setting time to analyze servicemap. Default set to 6 hours if not found in xraycloudwatcheventfile.json. 23 | xraycloudwatcheventfile = open('xraycloudwatchevent.json', 'r').read() 24 | xraycloudwatcheventdict = json.loads(xraycloudwatcheventfile) 25 | 26 | analyzeservicemapminutes = xraycloudwatcheventdict['analyzeservicemapminutes'] if 'analyzeservicemapminutes' in xraycloudwatcheventdict else 360 27 | evaluationperiodforcwalarm = xraycloudwatcheventdict['evaluationperiodforcwalarm'] if 'evaluationperiodforcwalarm' in xraycloudwatcheventdict else 1 28 | 29 | print("Setting to analyze your service map every: %d minutes" % analyzeservicemapminutes) 30 | 31 | # Zip the Lambda function and node folders 32 | print("Zipping the file that has to be uploaded to AWS Lambda") 33 | zipcommand = "zip -q -r Archive.zip xraycloudwatchevent.py" 34 | os.system(zipcommand) 35 | 36 | # Create s3 bucket to store the Archive 37 | print("Creating S3 bucket that will have the Archive.zip file for AWS Lambda") 38 | s3createcommand = "aws s3api create-bucket --create-bucket-configuration LocationConstraint=%s --acl private --bucket lambdacodexcw" % sampleappregion 39 | os.system(s3createcommand) 40 | 41 | # Upload Archive.zip to s3 bucket 42 | print("Uploading Archive.zip to the S3 bucket") 43 | s3uploadcommand = "aws s3 cp Archive.zip s3://lambdacodexcw" 44 | os.system(s3uploadcommand) 45 | 46 | # Deploy resources in a CloudFormation stack 47 | periodcwalarm=analyzeservicemapminutes*60 # Converting analyzeservicemapminutes from minutes to seconds 48 | print("Deploying resources from the Cloudformation template") 49 | cfcommand = "aws --region %s cloudformation deploy --template-file xraycloudwatchevent.template --stack-name %s --parameter-overrides appname=%s analyzeservicemapminutes=%d periodcwalarm=%d evaluationperiodforcwalarm=%d --capabilities CAPABILITY_NAMED_IAM" % (sampleappregion, appname, appname, analyzeservicemapminutes,periodcwalarm,evaluationperiodforcwalarm) 50 | print(cfcommand) 51 | os.system(cfcommand) 52 | 53 | print("Completed deploying resources from the Cloudformation template.") 54 | 55 | # Upload xraycloudwatchevent.json to s3 bucket 56 | print("Uploading xraycloudwatchevent.json to the S3 bucket") 57 | s3uploadcommand = "aws s3 cp xraycloudwatchevent.json s3://"+appname+"-xraycloudwatcheventbucket" 58 | os.system(s3uploadcommand) 59 | 60 | # Delete bucket that has the lambda code 61 | deletes3lambdabucket = "aws s3 rb s3://lambdacodexcw --force" 62 | os.system(deletes3lambdabucket) 63 | 64 | print("Deleted temporary s3 bucket") 65 | -------------------------------------------------------------------------------- /Setup/uninstall.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | appname = raw_input("Enter your sample app's name that you chose while installing the sample app {pattern:^[a-z0-9]+$}: ") 4 | sampleappregion = raw_input("Enter the aws region where had deployed this sample app. (Default: us-west-2): ") or "us-west-2" 5 | 6 | # Empty the app's s3 buckets first 7 | deleteappbucket = "aws s3 rm s3://%s-xraycloudwatcheventbucket --recursive --only-show-errors" % appname 8 | print("Deleting your app's s3 bucket contents using commands: %s" % deleteappbucket) 9 | os.system(deleteappbucket) 10 | 11 | # Deleting the s3 bucket 12 | deleteimages = "aws s3 rm s3://%s-xraycloudwatcheventbucket --recursive --only-show-errors" % appname 13 | print(deleteimages) 14 | os.system(deleteimages) 15 | print("Deleted the app's s3 bucket") 16 | 17 | # Delete cloudformation stack 18 | deletecloudformationstack = "aws --region %s cloudformation delete-stack --stack-name %s" % (sampleappregion, appname) 19 | print("Deleting your sample app's CloudFormation stack using command: %s" % deletecloudformationstack) 20 | os.system(deletecloudformationstack) 21 | print("Deleted CloudFormation stack for the app. Please check https://console.aws.amazon.com/cloudformation for any details.") 22 | -------------------------------------------------------------------------------- /Setup/xraycloudwatchevent.json: -------------------------------------------------------------------------------- 1 | { 2 | "alerts": 3 | { 4 | "default": 5 | { 6 | "responseunitthreshold":2, 7 | "errorpercentagethreshold":10, 8 | "faultpercentagethreshold":10, 9 | "throttlepercentagethreshold":20 10 | } 11 | }, 12 | "analyzeservicemapminutes":5, 13 | "evaluationperiodforcwalarm":1, 14 | "communications": 15 | { 16 | "sms": 17 | [ 18 | "19876543210" 19 | ], 20 | "email": 21 | [ 22 | "abcd@efgh.com", 23 | "stuv@wxyz.com" 24 | ] 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Setup/xraycloudwatchevent.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"). 5 | You may not use this file except in compliance with the License. 6 | A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the "license" file accompanying this file. 7 | This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 8 | See the License for the specific language governing permissions and limitations under the License. 9 | ''' 10 | 11 | import os 12 | import boto3 13 | import datetime 14 | import json 15 | from botocore.exceptions import ClientError 16 | from collections import defaultdict 17 | 18 | # The APP_NAME is retrieved from Lambda's environment variable 19 | APP_NAME = os.environ['APP_NAME'] 20 | 21 | # The region should be the same as your service running in X-Ray. 22 | # If your service runs in multiple regions then you should have multiple instances of this app running 23 | REGION_NAME = os.environ['AWS_REGION'] 24 | 25 | # Message types 26 | LOG_MESSAGE = "logmessages" 27 | ERROR_MESSAGE = "errormessages" 28 | 29 | # Change this value based on your preference for verbosity of logs 30 | VERBOSE_TIER = True 31 | 32 | # Alert types for CloudWatch event 33 | RESPONSE_ALERTS = 'Response' 34 | ERROR_ALERTS = 'Error' 35 | THROTTLE_ALERTS = 'Throttle' 36 | FAULT_ALERTS = 'Fault' 37 | 38 | # Default minutes for every scan of X-Ray-GetServiceGraph API 39 | service_graph_minutes = 10 40 | 41 | # SNS Topic for the CloudWatch Event. This SNS topic will be used by CloudWatch Event to send the appropriate notification. 42 | CW_EVENT_SNS = os.environ['CW_EVENT_SNS'] 43 | 44 | # SNS Topic for the CloudWatch Alarm. This SNS topic will be used by CloudWatch Alarm to send the appropriate notification when the alarm goes to an ALARM state. 45 | CW_ALARM_SNS = os.environ['CW_ALARM_SNS'] 46 | 47 | # Boolean to check if the sample app can subscribe to email and phone numbers listed 48 | SUBSCRIBE_TO_EMAIL_SMS = os.environ['SUBSCRIBE_TO_EMAIL_SMS'] 49 | 50 | xrayclient = boto3.client( 51 | 'xray', 52 | region_name=REGION_NAME 53 | ) 54 | 55 | snsclient = boto3.client( 56 | 'sns', 57 | region_name=REGION_NAME 58 | ) 59 | 60 | s3resource = boto3.resource( 61 | 's3', 62 | region_name=REGION_NAME 63 | ) 64 | 65 | cweventsclient = boto3.client('events') 66 | 67 | cwclient = boto3.client('cloudwatch') 68 | 69 | def print_message(printString, status=LOG_MESSAGE): 70 | # Print log messages only when verbose is selected 71 | if status == LOG_MESSAGE and VERBOSE_TIER is True: 72 | print(printString) 73 | 74 | # Print all error messages 75 | if status == ERROR_MESSAGE: 76 | print(printString) 77 | 78 | 79 | def get_trace_ids_from_trace_summary(tracesummary_dict): 80 | trace_ids = list() 81 | 82 | if type(tracesummary_dict) != dict: 83 | print_message("Trace Summary is expected to be a dict", ERROR_MESSAGE) 84 | return trace_ids 85 | 86 | if type(tracesummary_dict) == dict: 87 | if 'TraceSummaries' in tracesummary_dict: 88 | print_message("Found TraceSummaries key in tracesummary_dict") 89 | for valueOuter in tracesummary_dict['TraceSummaries']: 90 | if type(valueOuter) == dict: 91 | print_message("valueOuter is a dict") 92 | trace_ids.append(valueOuter['Id']) 93 | 94 | return trace_ids 95 | 96 | 97 | def handle_datetime(x): 98 | if isinstance(x, datetime.datetime): 99 | return x.isoformat() 100 | raise TypeError("Unknown type") 101 | 102 | 103 | def put_metric_in_cloudwatch(): 104 | try: 105 | cwclient.put_metric_data( 106 | Namespace = 'XCW/'+ APP_NAME, 107 | MetricData = [ 108 | { 109 | 'MetricName': APP_NAME + 'xraycloudwatchmetric', 110 | 'Timestamp': datetime.datetime.utcnow(), 111 | 'Value': 1, 112 | 'Unit': 'Count' 113 | }, 114 | ] 115 | ) 116 | except ClientError as ceputmetricdata: 117 | print_message("Error while calling put_metric_data: %s" % ceputmetricdata, ERROR_MESSAGE) 118 | raise 119 | 120 | 121 | 122 | def put_event_in_cloudwatch(source, detail_type, detail, purpose=None, topic_arn=False): 123 | # put_event for response time 124 | print_message("Putting %s event for %s" % (source, purpose)) 125 | try: 126 | response_for_put_event = cweventsclient.put_events( 127 | Entries=[ 128 | { 129 | 'Time': datetime.datetime.utcnow(), 130 | 'Source': source, 131 | 'DetailType': detail_type, 132 | 'Detail': detail 133 | } 134 | ] 135 | ) 136 | print_message(json.dumps(response_for_put_event, default=handle_datetime, indent=4, sort_keys=True)) 137 | except ClientError as ceputevent: 138 | print_message("Error while calling put_event for %s : %s" % (purpose, ceputevent), ERROR_MESSAGE) 139 | raise 140 | 141 | 142 | def subscribe_to_sms(communications_dict): 143 | topic_name = CW_EVENT_SNS 144 | topic_arn = False 145 | existing_subscription_endpoints = [] 146 | 147 | try: 148 | topic = snsclient.create_topic(Name=topic_name) # Returns back the topic_arn if topic is already present 149 | topic_arn = topic['TopicArn'] 150 | print_message("TopicArn: %s" % topic_arn) 151 | 152 | # Get all existing endpoints for subscriptions 153 | try: 154 | response_for_listof_subscriptions = snsclient.list_subscriptions_by_topic( 155 | TopicArn=topic_arn 156 | ) 157 | #browse through each subscription endpoint 158 | for each_sub in response_for_listof_subscriptions['Subscriptions']: 159 | existing_subscription_endpoints.append(each_sub['Endpoint']) 160 | except ClientError as celistsubs: 161 | print_message("Error while calling list_subscriptions_by_topic: %s" % celistsubs, ERROR_MESSAGE) 162 | raise 163 | 164 | if type(communications_dict) != dict: 165 | print_message("Expected communications_dict to be a dictionary. Cannot subscribe to SMS or Email otherwise.",ERROR_MESSAGE) 166 | return False 167 | 168 | if 'sms' in communications_dict: 169 | for sms_number in communications_dict['sms']: 170 | if(sms_number in existing_subscription_endpoints): 171 | print_message("Number already subscribed") 172 | else: 173 | if SUBSCRIBE_TO_EMAIL_SMS == 'Yes': 174 | print_message("Number to subscribe for SMS: %s" % sms_number) 175 | snsclient.subscribe( 176 | TopicArn=topic_arn, 177 | Protocol='sms', 178 | Endpoint=sms_number 179 | ) 180 | else: 181 | print_message("SUBSCRIBE_TO_EMAIL_SMS set to false. Not subscribing for SMS") 182 | except ClientError as cesnscreatetopic: 183 | print_message("Error while calling create_topic for %s: %s" % (topic_name, cesnscreatetopic), ERROR_MESSAGE) 184 | raise 185 | 186 | return topic_arn 187 | 188 | 189 | def subscribe_to_email(communications_dict): 190 | topic_name = CW_ALARM_SNS 191 | topic_arn = False 192 | existing_subscription_endpoints = [] 193 | 194 | try: 195 | topic = snsclient.create_topic(Name=topic_name) # Returns back the topic_arn if topic is already present 196 | topic_arn = topic['TopicArn'] 197 | print_message("TopicArn: %s" % topic_arn) 198 | 199 | # Get all existing endpoints for subscriptions 200 | try: 201 | response_for_listof_subscriptions = snsclient.list_subscriptions_by_topic( 202 | TopicArn=topic_arn 203 | ) 204 | #browse through each subscription endpoint 205 | for each_sub in response_for_listof_subscriptions['Subscriptions']: 206 | existing_subscription_endpoints.append(each_sub['Endpoint']) 207 | except ClientError as celistsubs: 208 | print_message("Error while calling list_subscriptions_by_topic: %s" % celistsubs, ERROR_MESSAGE) 209 | raise 210 | 211 | if type(communications_dict) != dict: 212 | print_message("Expected communications_dict to be a dictionary. Cannot subscribe to SMS or Email otherwise.",ERROR_MESSAGE) 213 | return False 214 | 215 | if 'email' in communications_dict: 216 | for email_address in communications_dict['email']: 217 | if(email_address in existing_subscription_endpoints): 218 | print_message("Email address already subscribed") 219 | else: 220 | if SUBSCRIBE_TO_EMAIL_SMS == 'Yes': 221 | print_message("Email address: %s to subscribe for sending emails" % email_address) 222 | snsclient.subscribe( 223 | TopicArn=topic_arn, 224 | Protocol='email', 225 | Endpoint=email_address 226 | ) 227 | else: 228 | print_message("SUBSCRIBE_TO_EMAIL_SMS set to false. Not subscribing to email") 229 | except ClientError as cesnscreatetopic: 230 | print_message("Error while calling create_topic for %s: %s" % (topic_name, cesnscreatetopic), ERROR_MESSAGE) 231 | raise 232 | 233 | return topic_arn 234 | 235 | 236 | def check_set_rule_put_event_in_cloudwatch(alert_type_string, service_name_string, xraycloudwatcheventdict, subscribe=True, put_event=True): 237 | topic_arn = False # This will contain the topicArn on successful registration of SMS 238 | rule_name = APP_NAME + '-xcw.alerts' 239 | 240 | communications_dict = xraycloudwatcheventdict['communications'] if 'communications' in xraycloudwatcheventdict else dict() 241 | 242 | if put_event != True: 243 | print_message("Skipping checking for rule and putting event for %s" % alert_type_string) 244 | return 245 | 246 | # Subscribe to SMS 247 | if subscribe == True: 248 | topic_arn = subscribe_to_sms(communications_dict) 249 | 250 | # Emails are subscribed for CloudWatch Alarm 251 | topic_arn_for_email = subscribe_to_email(communications_dict) 252 | 253 | # put_event in CloudWatch 254 | print_message("Putting CloudWatch Event for %s for %s" % (alert_type_string, service_name_string)) 255 | 256 | now = datetime.datetime.now() 257 | 258 | source_string = "%s" % (rule_name) 259 | detail_type_string = "XCW Notification for Alerts" 260 | detail_string = '{"status":"%s %s at %s."}' % (alert_type_string, service_name_string, now.strftime("%Y-%m-%d %H:%M")) 261 | 262 | put_event_in_cloudwatch(source_string, 263 | detail_type_string, 264 | detail_string, 265 | alert_type_string, 266 | topic_arn 267 | ) 268 | 269 | put_metric_in_cloudwatch() 270 | 271 | def put_aggregated_breaches(aggregate_alert_dict,xraycloudwatcheventdict): 272 | lenagg = len(aggregate_alert_dict.keys()) 273 | 274 | for alert_type_key, list_value in aggregate_alert_dict.items(): 275 | lenlist = len(list_value) 276 | if (lenlist > 0): 277 | service_name_string = "%s" % list_value[0] if lenlist == 1 else "%s+%d nodes" % (list_value[0],lenlist-1) 278 | 279 | if(lenagg > 0): 280 | first_alert_string = "%s" % list(aggregate_alert_dict.keys())[0] if lenagg == 1 else "%s and other thresholds have been breached for" % list(aggregate_alert_dict.keys())[0] 281 | check_set_rule_put_event_in_cloudwatch(first_alert_string,service_name_string,xraycloudwatcheventdict) 282 | 283 | return 284 | 285 | def get_service_graph_and_analyze(xraycloudwatcheventdict): 286 | if type(xraycloudwatcheventdict) == dict: 287 | if 'analyzeservicemapminutes' in xraycloudwatcheventdict: 288 | service_graph_minutes = xraycloudwatcheventdict['analyzeservicemapminutes'] 289 | print_message("analyzeservicemapminutes found in xraycloudwatcheventfile: %d" % service_graph_minutes) 290 | 291 | print_message("Calling get_service_graph") 292 | 293 | try: 294 | service_graph_response = xrayclient.get_service_graph( 295 | StartTime=datetime.datetime.utcnow() - datetime.timedelta(minutes=service_graph_minutes), 296 | EndTime=datetime.datetime.utcnow(), 297 | ) 298 | print_message("Response from get_service_graph") 299 | print_message(json.dumps(service_graph_response, default=handle_datetime, indent=4, sort_keys=True)) 300 | except ClientError as ceservice: 301 | print_message("Cannot continue. Error while getting service_graph_response: %s" % ceservice) 302 | raise 303 | 304 | if 'Services' not in service_graph_response: 305 | print_message("No 'Services' found in your X-Ray get_service_graph API call",ERROR_MESSAGE) 306 | return 307 | 308 | # Dict that aggregates different alerts 309 | aggregate_alert_dict = defaultdict(list) 310 | 311 | for value_services in service_graph_response['Services']: 312 | service_name = value_services['Name'] 313 | node_type = value_services['Type'] 314 | 315 | # Go through summarystatistics 316 | total_count = value_services['SummaryStatistics']['TotalCount'] if 'SummaryStatistics' in value_services else 0 317 | error_count = value_services['SummaryStatistics']['ErrorStatistics']['TotalCount'] if 'SummaryStatistics' in value_services else 0 318 | throttle_count = value_services['SummaryStatistics']['ErrorStatistics']['ThrottleCount'] if 'SummaryStatistics' in value_services else 0 319 | fault_count = value_services['SummaryStatistics']['FaultStatistics']['TotalCount'] if 'SummaryStatistics' in value_services else 0 320 | total_response_time = value_services['SummaryStatistics']['TotalResponseTime'] if 'SummaryStatistics' in value_services else 0 321 | 322 | print_message("SummaryStatistics for %s of type %s: TotalCount: %d; ErrorCount: %d; ThrottleCount: %d; faultCount:%d; TotalResponseTime:%d" % (service_name,node_type, total_count, error_count, throttle_count, fault_count, total_response_time)) 323 | 324 | # Check if the service_name is present in the xraycloudwatchevent.json file or if user wants alerts for all nodes in servicemap 325 | if (service_name in xraycloudwatcheventdict['alerts'] and node_type in xraycloudwatcheventdict['alerts'][service_name]['type'] and total_count > 0) or ('default' in xraycloudwatcheventdict['alerts'] and total_count > 0): #check if the service name is found in your xraycloudwatch json file 326 | avg_response_time = total_response_time/total_count 327 | error_percent = (error_count/total_count) * 100 328 | throttle_percent = (throttle_count/total_count) * 100 329 | fault_percent = (fault_count/total_count) * 100 330 | 331 | #set key_service_name to default if service_name not found in xraycloudwatcheventdict 332 | key_service_name = service_name if service_name in xraycloudwatcheventdict['alerts'] else 'default' 333 | 334 | print_message("Looking at user defined responseTime for %s and found %s : %d" % (service_name,key_service_name, xraycloudwatcheventdict['alerts'][key_service_name]['responseunitthreshold'])) 335 | if avg_response_time >= xraycloudwatcheventdict['alerts'][key_service_name]['responseunitthreshold']: 336 | aggregate_alert_dict[RESPONSE_ALERTS].append(service_name) 337 | else: 338 | print_message("ResponseTime is: %d for %s . Not setting up CloudWatch events for %s" % (avg_response_time, key_service_name, service_name)) 339 | 340 | print_message("Looking at user defined errorPercent for %s and found %d" % (service_name, xraycloudwatcheventdict['alerts'][key_service_name]['errorpercentagethreshold'])) 341 | if error_percent >= xraycloudwatcheventdict['alerts'][key_service_name]['errorpercentagethreshold']: 342 | aggregate_alert_dict[ERROR_ALERTS].append(service_name) 343 | else: 344 | print_message("ErrorPercent is: %d for %s . Not setting up CloudWatch events for %s" % (error_percent, key_service_name, service_name)) 345 | 346 | print_message("Looking at user defined throttlePercent for %s and found %d" % (service_name, xraycloudwatcheventdict['alerts'][key_service_name]['throttlepercentagethreshold'])) 347 | if throttle_percent >= xraycloudwatcheventdict['alerts'][key_service_name]['throttlepercentagethreshold']: 348 | aggregate_alert_dict[THROTTLE_ALERTS].append(service_name) 349 | else: 350 | print_message("ThrottlePercent is: %d for %s . Not setting up CloudWatch events for %s" % (throttle_percent, key_service_name, service_name)) 351 | 352 | print_message("Looking at user defined faultPercent for %s and found %d" % (service_name, xraycloudwatcheventdict['alerts'][key_service_name]['faultpercentagethreshold'])) 353 | if fault_percent >= xraycloudwatcheventdict['alerts'][key_service_name]['faultpercentagethreshold']: 354 | aggregate_alert_dict[FAULT_ALERTS].append(service_name) 355 | else: 356 | print_message("FaultPercent is: %d for %s . Not setting up CloudWatch events for %s" % (fault_percent, key_service_name, service_name)) 357 | 358 | put_aggregated_breaches(aggregate_alert_dict,xraycloudwatcheventdict) 359 | 360 | 361 | def main(): 362 | print_message("Getting json from S3") 363 | try: 364 | s3resource.Bucket(APP_NAME + '-xraycloudwatcheventbucket').download_file('xraycloudwatchevent.json', '/tmp/xraycloudwatchevent.json') 365 | print_message("Downloading file to xraycloudwatchevent.json file") 366 | xraycloudwatcheventfile = open('/tmp/xraycloudwatchevent.json').read() 367 | except ClientError as ces: 368 | print_message("Error while getting xraycloudwatchevent.json: %s" % ces) 369 | raise 370 | 371 | print_message("xraycloudwatchevent.json file:") 372 | print_message(xraycloudwatcheventfile) 373 | 374 | xraycloudwatcheventdict = json.loads(xraycloudwatcheventfile) 375 | 376 | get_service_graph_and_analyze(xraycloudwatcheventdict) 377 | 378 | 379 | def handler(event, context): 380 | main() 381 | -------------------------------------------------------------------------------- /Setup/xraycloudwatchevent.template: -------------------------------------------------------------------------------- 1 | #Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | #Licensed under the Apache License, Version 2.0 (the "License"). 4 | #You may not use this file except in compliance with the License. 5 | #A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the "license" file accompanying this file. 6 | #This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 7 | #See the License for the specific language governing permissions and limitations under the License. 8 | --- 9 | AWSTemplateFormatVersion: '2010-09-09' 10 | Parameters: 11 | appname: 12 | Type: String 13 | Description: This parameter will be used for naming your CloudFormation stack, public s3 bucket and as a prefix to identify all the Lambda functions and IAM Roles associated with your app. 14 | MinLength: '1' 15 | MaxLength: '21' 16 | AllowedPattern: "^[a-z0-9]+$" 17 | ConstraintDescription: 'Malformed input-Parameter: appname must match pattern ^[a-z0-9]+$ with max length of 21 characters' 18 | analyzeservicemapminutes: 19 | Type: Number 20 | MinValue: 5 21 | MaxValue: 360 22 | Default: 5 23 | Description: This parameter (in minutes) will be used to set the rate for schedule to run the Lambda function. 24 | ConstraintDescription: 'Malformed input-Parameter: analyzeservicemapevery must match pattern ^[0-9]+$ with min value of 5 (5 minutes) max value of 360 (6 hours)' 25 | periodcwalarm: 26 | Type: Number 27 | MinValue: 300 28 | MaxValue: 21600 29 | Default: 300 30 | Description: This parameter is calculated in seconds from analyzeservicemapminutes parameter. 31 | ConstraintDescription: 'Malformed input-Parameter: analyzeservicemapevery must match pattern ^[0-9]+$ with min value of 300 (5 minutes) max value of 21600 (6 hours)' 32 | evaluationperiodforcwalarm: 33 | Type: Number 34 | MinValue: 1 35 | MaxValue: 10 36 | Default: 1 37 | Description: This parameter (in number of periods) is used for the CloudWatch Alarm for setting EvaluationPeriod. 38 | ConstraintDescription: 'Malformed input-Parameter: evaluationperiodforcwalarm must match pattern ^[0-9]+$ with min value of 1 max value of 10' 39 | Resources: 40 | s3XCW: 41 | Type: AWS::S3::Bucket 42 | Properties: 43 | BucketName: 44 | Fn::Join: 45 | - '-' 46 | - - Ref: appname 47 | - 'xraycloudwatcheventbucket' 48 | AccessControl: Private 49 | CorsConfiguration: 50 | CorsRules: 51 | - AllowedHeaders: 52 | - "*" 53 | AllowedMethods: 54 | - POST 55 | - GET 56 | - PUT 57 | - DELETE 58 | - HEAD 59 | AllowedOrigins: 60 | - "*" 61 | ExposedHeaders: 62 | - Etag 63 | Id: corsxcw 64 | xcwSNSAlerts: 65 | Type: "AWS::SNS::Topic" 66 | Properties: 67 | TopicName: 68 | Fn::Join: 69 | - '-' 70 | - - Ref: appname 71 | - 'cloudwatcheventsnstopic' 72 | DisplayName: 73 | Fn::Join: 74 | - '-' 75 | - - 'XCW ALERT FOR YOUR APP' 76 | - Ref: appname 77 | xcwSNSCWAlarmAlert: 78 | Type: "AWS::SNS::Topic" 79 | Properties: 80 | TopicName: 81 | Fn::Join: 82 | - '-' 83 | - - Ref: appname 84 | - 'cloudwatchalarmsnstopic' 85 | DisplayName: 86 | Fn::Join: 87 | - '-' 88 | - - 'XCW ALARM ALERT FOR YOUR APP' 89 | - Ref: appname 90 | xcwEventRule: 91 | Type: "AWS::Events::Rule" 92 | Properties: 93 | Description: 94 | Fn::Join: 95 | - '-' 96 | - - Ref: appname 97 | - 'CloudWatch event for XCW Alerts' 98 | Name: 99 | Fn::Join: 100 | - '-' 101 | - - 'XRAYALERTSFOR' 102 | - Ref: appname 103 | EventPattern: 104 | source: 105 | - Fn::Join: 106 | - '-' 107 | - - Ref: appname 108 | - 'xcw.alerts' 109 | detail-type: 110 | - 'XCW Notification for Alerts' 111 | State: 'ENABLED' 112 | Targets: 113 | - 114 | Arn: 115 | Ref: 'xcwSNSAlerts' 116 | Id: 'TargetForAWSXRAYSNSAlerts' 117 | InputTransformer: 118 | InputPathsMap: 119 | alertToShow: "$.detail.status" 120 | InputTemplate: "\"XCW: . Please visit 'https://console.aws.amazon.com/xray/home' for more details.\"" 121 | PolicyForEventsToPublishSNS: 122 | Type: AWS::SNS::TopicPolicy 123 | Properties: 124 | PolicyDocument: 125 | Id: Policy1509913587609 126 | Version: '2012-10-17' 127 | Statement: 128 | - Sid: Stmt1509913581341 129 | Action: 130 | - 'sns:Publish' 131 | Effect: Allow 132 | Resource: 133 | - Ref: 'xcwSNSAlerts' 134 | Principal: 135 | Service: 'events.amazonaws.com' 136 | Topics: 137 | - Ref: 'xcwSNSAlerts' 138 | PolicyForAlarmToPublishSNS: 139 | Type: AWS::SNS::TopicPolicy 140 | Properties: 141 | PolicyDocument: 142 | Id: Policy1509826587100 143 | Version: '2012-10-17' 144 | Statement: 145 | - Sid: Stmt1509913591520 146 | Action: 147 | - 'sns:Publish' 148 | Effect: Allow 149 | Resource: 150 | - Ref: 'xcwSNSCWAlarmAlert' 151 | Principal: '*' 152 | Condition: 153 | ArnEquals: 154 | AWS:SourceArn: 155 | Fn::GetAtt: 156 | - xcwCloudWatchAlarm 157 | - Arn 158 | Topics: 159 | - Ref: 'xcwSNSCWAlarmAlert' 160 | LambdaIAMRole: 161 | Type: AWS::IAM::Role 162 | Properties: 163 | AssumeRolePolicyDocument: 164 | Version: '2012-10-17' 165 | Statement: 166 | - Effect: Allow 167 | Principal: 168 | Service: 169 | - lambda.amazonaws.com 170 | Action: 171 | - sts:AssumeRole 172 | Path: "/" 173 | ManagedPolicyArns: 174 | - arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess 175 | - arn:aws:iam::aws:policy/AWSXrayReadOnlyAccess 176 | - arn:aws:iam::aws:policy/CloudWatchEventsFullAccess 177 | - arn:aws:iam::aws:policy/AWSLambdaExecute 178 | - arn:aws:iam::aws:policy/service-role/AWSLambdaRole 179 | RoleName: 180 | Fn::Join: 181 | - '-' 182 | - - Ref: appname 183 | - 'LambdaXCWRole' 184 | S3ManagedPolicyRWL: 185 | Type: "AWS::IAM::Policy" 186 | Properties: 187 | PolicyDocument: 188 | Version: '2012-10-17' 189 | Statement: 190 | - Sid: Stmt1505428188000 191 | Effect: Allow 192 | Action: 193 | - s3:GetObject 194 | - s3:ListBucket 195 | - s3:PutObject 196 | - s3:CreateBucket 197 | - s3:ListObjects 198 | - s3:PutObjectAcl 199 | - s3:DeleteObject 200 | Resource: 201 | - Fn::Join: 202 | - '' 203 | - - 'arn:aws:s3:::' 204 | - Ref: s3XCW 205 | - Fn::Join: 206 | - '' 207 | - - 'arn:aws:s3:::' 208 | - Ref: s3XCW 209 | - "/*" 210 | PolicyName: "AmazonS3ReadWriteListAccess" 211 | Roles: 212 | - 213 | Ref: "LambdaIAMRole" 214 | SNSManagedPolicyXCW: 215 | Type: "AWS::IAM::Policy" 216 | Properties: 217 | PolicyDocument: 218 | Version: '2012-10-17' 219 | Statement: 220 | - Sid: Stmt1506359188000 221 | Effect: Allow 222 | Action: 223 | - sns:CreateTopic 224 | - sns:Subscribe 225 | - sns:ListSubscriptionsByTopic 226 | Resource: 227 | - Fn::Join: 228 | - ':' 229 | - - 'arn:aws:sns' 230 | - Ref: AWS::Region 231 | - Ref: AWS::AccountId 232 | - '*' 233 | PolicyName: "XCWSNSManagedPolicy" 234 | Roles: 235 | - 236 | Ref: "LambdaIAMRole" 237 | CloudWatchManagedPolicyXCW: 238 | Type: "AWS::IAM::Policy" 239 | Properties: 240 | PolicyDocument: 241 | Version: '2012-10-17' 242 | Statement: 243 | - Sid: Stmt1509379184000 244 | Effect: Allow 245 | Action: 246 | - cloudwatch:PutMetricData 247 | Resource: 248 | - '*' 249 | PolicyName: "XCWCloudWatchManagedPolicy" 250 | Roles: 251 | - 252 | Ref: "LambdaIAMRole" 253 | CloudWatchLambdaTriggerPermissionXCW: 254 | Type: "AWS::Lambda::Permission" 255 | Properties: 256 | FunctionName: 257 | Fn::GetAtt: 258 | - xcwLambda 259 | - Arn 260 | Action: 'lambda:InvokeFunction' 261 | Principal: events.amazonaws.com 262 | SourceArn: 263 | Fn::GetAtt: 264 | - xcwScheduledRuleForInvokingLambda 265 | - Arn 266 | LambdaIAMInstanceProfile: 267 | Type: AWS::IAM::InstanceProfile 268 | Properties: 269 | Path: "/" 270 | Roles: 271 | - Ref: LambdaIAMRole 272 | xcwLambda: 273 | Type: AWS::Lambda::Function 274 | Properties: 275 | FunctionName: 276 | Fn::Join: 277 | - '-' 278 | - - Ref: appname 279 | - 'xraycloudwatchevent' 280 | Code: 281 | S3Bucket: lambdacodexcw 282 | S3Key: Archive.zip 283 | Handler: xraycloudwatchevent.handler 284 | Role: 285 | Fn::GetAtt: 286 | - LambdaIAMRole 287 | - Arn 288 | Runtime: python3.6 289 | Timeout: '25' 290 | TracingConfig: 291 | Mode: Active 292 | Environment: 293 | Variables: 294 | APP_NAME: !Ref appname 295 | CW_EVENT_SNS: !GetAtt xcwSNSAlerts.TopicName 296 | CW_ALARM_SNS: !GetAtt xcwSNSCWAlarmAlert.TopicName 297 | SUBSCRIBE_TO_EMAIL_SMS: 'Yes' 298 | xcwScheduledRuleForInvokingLambda: 299 | Type: AWS::Events::Rule 300 | Properties: 301 | Description: Scheduled Rule for invoking the Lambda function that analyzes AWS X-Ray service graph and triggers CloudWatch 302 | Name: 303 | Fn::Join: 304 | - '-' 305 | - - 'SCHEDULEDLAMBDAFOR' 306 | - Ref: appname 307 | ScheduleExpression: 308 | Fn::Join: 309 | - '' 310 | - - 'rate(' 311 | - Ref: analyzeservicemapminutes 312 | - ' minutes)' 313 | State: 'ENABLED' 314 | Targets: 315 | - 316 | Id: LambdaTargetForScheduledEvent 317 | Arn: 318 | Fn::GetAtt: 319 | - xcwLambda 320 | - Arn 321 | xcwCloudWatchAlarm: 322 | Type: AWS::CloudWatch::Alarm 323 | Properties: 324 | AlarmName: 325 | Fn::Join: 326 | - '-' 327 | - - 'XRayCloudWatchAlarm' 328 | - Ref: appname 329 | ActionsEnabled: Yes 330 | AlarmActions: 331 | - Ref: 'xcwSNSCWAlarmAlert' 332 | AlarmDescription: 'XCW alarm for notifications' 333 | ComparisonOperator: GreaterThanThreshold 334 | EvaluationPeriods: !Ref evaluationperiodforcwalarm 335 | TreatMissingData: notBreaching 336 | MetricName: 'TriggeredRules' 337 | Namespace: 'AWS/Events' 338 | Dimensions: 339 | - Name: 'RuleName' 340 | Value: !Ref xcwEventRule 341 | Period: !Ref periodcwalarm 342 | Statistic: Sum 343 | Threshold: '0' 344 | Unit: Count 345 | 346 | 347 | 348 | 349 | 350 | --------------------------------------------------------------------------------