{"id":20639096,"url":"https://github.com/korniichuk/cloudwatch-alarm","last_synced_at":"2026-03-09T15:31:21.679Z","repository":{"id":95417668,"uuid":"154524148","full_name":"korniichuk/cloudwatch-alarm","owner":"korniichuk","description":"Amazon CloudWatch Alarm 2.0. CloudWatch to Slack smart notification","archived":false,"fork":false,"pushed_at":"2019-03-28T20:15:25.000Z","size":7673,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-17T03:44:10.254Z","etag":null,"topics":["amazon-cloudwatch","amazon-lambda","amazon-sns","aws","aws-cloudwatch","aws-lambda","aws-sns","cloudwatch","cloudwatch-alarms","cloudwatch-events","python","python-3","python3","slack","slack-api"],"latest_commit_sha":null,"homepage":"http://www.korniichuk.com","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/korniichuk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-24T15:23:47.000Z","updated_at":"2021-08-31T23:14:35.000Z","dependencies_parsed_at":"2023-03-14T07:16:22.581Z","dependency_job_id":null,"html_url":"https://github.com/korniichuk/cloudwatch-alarm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/korniichuk/cloudwatch-alarm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korniichuk%2Fcloudwatch-alarm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korniichuk%2Fcloudwatch-alarm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korniichuk%2Fcloudwatch-alarm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korniichuk%2Fcloudwatch-alarm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/korniichuk","download_url":"https://codeload.github.com/korniichuk/cloudwatch-alarm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korniichuk%2Fcloudwatch-alarm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30301109,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-09T14:33:48.460Z","status":"ssl_error","status_checked_at":"2026-03-09T14:33:48.027Z","response_time":61,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["amazon-cloudwatch","amazon-lambda","amazon-sns","aws","aws-cloudwatch","aws-lambda","aws-sns","cloudwatch","cloudwatch-alarms","cloudwatch-events","python","python-3","python3","slack","slack-api"],"created_at":"2024-11-16T15:22:24.413Z","updated_at":"2026-03-09T15:31:21.654Z","avatar_url":"https://github.com/korniichuk.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cloudwatch-alarm\n**Name:** cloudwatch-alarm\u003cbr\u003e\n**Description:** Amazon CloudWatch Alarm 2.0. CloudWatch to Slack smart notification\u003cbr\u003e\n**GitHub:** https://github.com/korniichuk/cloudwatch-alarm\n\n## Table of Contents\n* **[Introduction](#introduction)**\n  * **[Amazon CloudWatch Alarm 2.0](#amazon-cloudwatch-alarm-20)**\n  * **[Standard Amazon CloudWatch Alarm](#standard-amazon-cloudwatch-alarm)**\n* **[How it works](#how-it-works)**\n* **[Requirements](#requirements)**\n* **[Python lib versions](#python-lib-versions)**\n* **[Create Slack webhook](#create-slack-webhook)**\n* **[Create Amazon SNS topic](#create-amazon-sns-topic)**\n* **[Create Amazon CloudWatch Alarm](#create-amazon-cloudwatch-alarm)**\n* **[Create Amazon S3 bucket](#create-amazon-s3-bucket)**\n* **[Create Amazon Lambda function](#create-amazon-lambda-function)**\n  * **[Create function base from blueprint](#create-function-base-from-blueprint)**\n  * **[Upgrade function](#upgrade-function)**\n* **[Edit AWS IAM role](#edit-aws-iam-role)**\n* **[Test Amazon Lambda function](#test-amazon-lambda-function)**\n* **[Change Slack message retention](#change-slack-message-retention)**\n\n## Introduction\n### Amazon CloudWatch Alarm 2.0\n![amazon_cloudwatch_alarm_smart.png](img/amazon_cloudwatch_alarm_smart.png \"Amazon CloudWatch Alarm 2.0\")\n\n### Standard Amazon CloudWatch Alarm\n![amazon_cloudwatch_alarm_standard.png](img/amazon_cloudwatch_alarm_standard.png \"Standard Amazon CloudWatch Alarm\")\n\n## How it works\n![how_it_works.jpg](img/how_it_works.jpg \"How it works\")\n\n## Requirements\nPlease, install Python packages:\n```\n$ pip install -t . -r requirements.txt\n```\n\n## Python lib versions\n* [boto3](https://pypi.org/project/boto3/) ver. 1.9.41\n* [botocore](https://pypi.org/project/botocore/) ver. 1.12.41\n\n## Create Slack webhook\nNavigate to https://`\u003cyour-team-domain\u003e`.slack.com/apps, like https://example.slack.com/apps. Search for and select `Incoming WebHooks`. Click `Add Configuration` button:\n\n![slack_-_add_configuration.png](img/slack_-_add_configuration.png \"Create Slack webhook. Add configuration\")\n\nChoose the default channel where messages will be sent (like `#example`) and click `Add Incoming WebHooks integration`. Copy and save the webhook URL (like https://hooks.slack.com/services/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) from the setup instructions.\n\n**Note:** You need admin rights to your Slack.\n\n**Note:** Slack marks public channels with `#` (e.g. `#public`) and private channels without `#` (e.g. `private`).\n\n## Create Amazon SNS topic\nCreate new [Amazon SNS](https://aws.amazon.com/sns/) topic: https://docs.aws.amazon.com/sns/latest/dg/sns-getting-started.html#CreateTopic. Like `example` topic with `arn:aws:sns:eu-west-1:539199393808:example` ARN.\n\n![sns_-_topic_details.png](img/sns_-_topic_details.png \"Create Amazon SNS topic. Topic details\")\n\n## Create Amazon CloudWatch Alarm\nNavigate to https://console.aws.amazon.com/cloudwatch/ and create new Amazon CloudWatch Alarm. Click `Select metric` button:\n\n![cloudwatch_-_select_metric.png](img/cloudwatch_-_select_metric.png \"Create Amazon CloudWatch Alarm. Select metric\")\n\nSelect metric, like `EC2 -\u003e Per-Instance Metrics -\u003e CPUUtilization`:\n\n![cloudwatch_-_cpuutilization.png](img/cloudwatch_-_cpuutilization.png \"Create Amazon CloudWatch Alarm. CPUUtilization\")\n\nChoose `Source` tab and select `Image API` checkbox. Copy and save json code. Click `Select metric` button:\n\n![cloudwatch_-_image_api.png](img/cloudwatch_-_image_api.png \"Create Amazon CloudWatch Alarm. Image API\")\n\nSaved json code example:\n```\n{\n    \"metrics\": [\n        [ \"AWS/EC2\", \"CPUUtilization\", \"InstanceId\", \"i-0d91fdf2dbb765977\", { \"period\": 300, \"stat\": \"Average\" } ]\n    ],\n    \"title\": \"The percentage of CPU utilization\",\n    \"start\": \"-PT3H\",\n    \"end\": \"P0D\",\n    \"timezone\": \"+0100\"\n}\n```\n\nSet up `Alarm details`. Enter `Name` (e.g. `CPUUtilization`) and `Description` (e.g. `The percentage of CPU utilization`). Specify the alarm condition. For example: select `\u003e=`, enter `80`, and enter `3` datapoints.\n\n![cloudwatch_-_alarm_details.png](img/cloudwatch_-_alarm_details.png \"Create Amazon CloudWatch Alarm. Alarm details\")\n\nConnect CloudWatch Alarm to [created SNS topic](#create-amazon-sns-topic) and finally click `Create Alarm` button:\n![cloudwatch_-_actions.png](img/cloudwatch_-_actions.png \"Create Amazon CloudWatch Alarm. Actions\")\n\n**AWS docs:** https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html\n\n## Create Amazon S3 bucket\nNavigate to https://console.aws.amazon.com/s3/ and create new S3 bucket (e.g. `example`). Create new lifecyle rule for bucket (e.g. `DeleteTmpAfter72h`) with `tmp/` prefix filter:\n\n![s3_-_lifecycle_rule.png](img/s3_-_lifecycle_rule.png \"Create Amazon S3 bucket. Lifecycle rule\")\n\nConfigure expiration as below. Expire after 3 days. Permanently delete after 3 days. Clean up incomplete multipart uploads after 1 day.\n\n![s3_-_configure_expiration](img/s3_-_configure_expiration.png \"Create Amazon S3 bucket. Configure expiration\")\n\nEdit public access settings to your S3 bucket. Select your S3 bucket (e.g. `example`) and click `Edit public access settings` button. Deselect all checkboxes and click `Save` button:\n\n![s3_-_edit_public_access_settings.png](img/s3_-_edit_public_access_settings.png \"Create Amazon S3 bucket. Edit public access settings\")\n\nEnter `confirm` to `confirm` field and click `Confirm` button:\n\n![s3_-_confirm_public_access_settings.png](img/s3_-_confirm_public_access_settings.png \"Create Amazon S3 bucket. Confirm public access settings\")\n\n## Create Amazon Lambda function\n### Create function base from blueprint\nNavigate to https://console.aws.amazon.com/lambda/. Click `Create function`. Click `Blueprints`. Search for and select `cloudwatch-alarm-to-slack-python3`. Click `Configure` button:\n\n![lambda_-_blueprints.png](img/lambda_-_blueprints.png \"Create Amazon Lambda function. Blueprints\")\n\nEnter function name (e.g. `example`) to `Name` field. In `Role` select `Create a new role from one or more templates.` In `Role name` field enter role name (e.g. `example`).\n\n![lambda_-_basic_information.png](img/lambda_-_basic_information.png \"Create Amazon Lambda function. Basic information\")\n\nGo to `SNS trigger` section and select [your SNS topic](#create-amazon-sns-topic). For example: `example` topic with `arn:aws:sns:eu-west-1:539199393808:example` ARN. Select the `Enable trigger` checkbox.\n\n![lambda_-_sns_trigger.png](img/lambda_-_sns_trigger.png \"Create Amazon Lambda function. SNS trigger\")\n\n[Create KMS Key](http://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html) or use an existing KMS Key. For example: `arn:aws:kms:eu-west-1:539199393808:key/4c3126f2-f3bf-453e-b32d-58c9356d84b7`.\n\nGo to `Lambda function code` section. Go to `Encryption configuration` subsection and select the `Enable helpers for encryption in transit` checkbox. Select `AWS KMS key to encrypt in transit` (e.g. `arn:aws:kms:eu-west-1:539199393808:key/4c3126f2-f3bf-453e-b32d-58c9356d84b7`).\n\n![lambda_-_encryption_configuration.png](img/lambda_-_encryption_configuration.png \"Create Amazon Lambda function. Encryption configuration\")\n\nPaste [Slack channel](#create-slack-webhook) into the `slackChannel` environment variable, like `example`. The Slack channel does not contain private info, so do NOT click `Encrypt` button.\n\nPaste [Slack webhook URL](#create-slack-webhook) into the `kmsEncryptedHookUrl` environment variable. You must exclude the protocol from the URL (e.g. `hooks.slack.com/services/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`). Click `Encrypt` button. Finally click `Create function` button:\n\n![lambda_-_environment_variables.png](img/lambda_-_environment_variables.png \"Create Amazon Lambda function. Environment variables\")\n\n### Upgrade function\nClone [cloudwatch-alarm](https://github.com/korniichuk/cloudwatch-alarm) repository:\n```\n$ git clone https://github.com/korniichuk/cloudwatch-alarm.git\n```\n\nModify `cloudwatch-alarm/lambda_function.py` file. Replace `confluence_url` and `cloudwatch_url` var values. For example, from:\n```\nconfluence_url = 'http://www.korniichuk.com'\ncloudwatch_url = 'http://www.korniichuk.com'\n```\n\nto:\n```\nconfluence_url = 'https://bit.ly/1d3LdqJ'\ncloudwatch_url = 'https://amzn.to/2NeZooo'\n```\n\nUpdate `metric` var value with [saved Image API json](#create-amazon-cloudwatch-alarm). For example, from:\n```\nmetric = \"\"\"{\n    \"metrics\": [[\n        \"LogMetrics\",\n        \"MetricName\",\n        {\"period\": 3600, \"stat\": \"Sum\"}]],\n    \"title\": \"Title\",\n    \"start\": \"-P1D\",\n    \"end\": \"P0D\",\n    \"timezone\": \"+0100\"}\"\"\"\n```\n\nto:\n```\nmetric = \"\"\"{\n    \"metrics\": [[\n        \"AWS/EC2\",\n        \"CPUUtilization\",\n        \"InstanceId\",\n        \"i-0d91fdf2dbb765977\",\n        {\"period\": 300, \"stat\": \"Average\"}]],\n    \"title\": \"The percentage of CPU utilization\",\n    \"start\": \"-PT3H\",\n    \"end\": \"P0D\",\n    \"timezone\": \"+0100\"}\"\"\"\n```\n\n**Docs:** [Metric widget structure and syntax](https://docs.aws.amazon.com/en_us/AmazonCloudWatch/latest/APIReference/CloudWatch-Metric-Widget-Structure.html)\n\nReplace `bucket_name` var value to [your bucket name](#create-amazon-s3-bucket). For example, from:\n```\nbucket_name = 'korniichuk'\n```\n\nto:\n```\nbucket_name = 'example'\n```\n\nNavigate to cloned `cloudwatch-alarm` repository and install Python packages:\n```\n$ cd cloudwatch-alarm\n$ pip install -t . -r requirements.txt\n```\n\nPrepare `zip.zip` with `lambda_function.py` file and Python dependencies:\n```\n$ rm -r *.dist-info __pycache__\n$ zip -r zip.zip .\n```\n\nNavigate to your lambda function (e.g. `example`). Go to `Function code` section and select `Upload a .zip file` from `Code entry type` dropdown. Click `Upload` button. Upload `zip.zip` file. Finally click `Save` button:\n\n![lambda_-_upload_function_package.png](img/lambda_-_upload_function_package.png \"Upgrade function. Upload function package\")\n\n**Note:** We use [boto3](https://pypi.org/project/boto3/) ver. 1.9.41 and [botocore](https://pypi.org/project/botocore/) ver. 1.12.41 because [default Lambda's packages](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html) do not support [get_metric_widget_image](https://boto3.amazonaws.com/v1/documentation/api/1.9.41/reference/services/cloudwatch.html#CloudWatch.Client.get_metric_widget_image) function.\n\n## Edit AWS IAM role\nNavigate to https://console.aws.amazon.com/iam/. Select [your role](#create-function-base-from-blueprint) (e.g. `example`). Select `Permissions` tab. Click `Attach policies` button:\n\n![iam_-_permissions.png](img/iam_-_permissions.png \"Edit AWS IAM role. Permissions\")\n\nSearch for and select `AmazonS3FullAccess` and `CloudWatchFullAccess`. Click `Attach policy` button. See final result below.\n\n![iam_-_summary.png](img/iam_-_summary.png \"Edit AWS IAM role. Summary\")\n\n**Note:** Do not use `*FullAccess` roles on production. You can grant access to concrete S3 bucket (e.g. `example`). You can precisize `CloudWatchFullAccess` policy to `cloudwatch:*` plus `logs:*` only.\n\n## Test Amazon Lambda function\nNavigate to your lambda function (e.g. `example`). Click `Select a test event..` and select `Configure test events`:\n\n![lambda_-_configure_test_events.png](img/lambda_-_configure_test_events.png \"Test Amazon Lambda function. Configure test events\")\n\nEnter event name to `Event name` field (e.g. `Example`). Copy json code from [test-event.json](test-event.json) file to code area or enter your own. Click `Create` button:\n\n![lambda_-_create_new_test_event.png](img/lambda_-_create_new_test_event.png \"Test Amazon Lambda function. Create new test event\")\n\nSelect created event (e.g. `Example`) and click `Test` button:\n\n![lambda_-_run_test.png](img/lambda_-_run_test.png \"Test Amazon Lambda function. Run test\")\n\n**Note:** You can [maximize CPU utilization](https://stackoverflow.com/questions/2925606/how-to-create-a-cpu-spike-with-a-bash-command) with `$ dd if=/dev/zero of=/dev/null` command.\n\n## Change Slack message retention\nNavigate to Slack channel. Click `Channel Settings` icon. Select `Edit message retention...`:\n\n![slack_-_channel_settings.png](img/slack_-_channel_settings.png \"Change Slack message retention. Channel settings\")\n\nChange default message retention to `3 Days`. Click `Save` button:\n\n![slack_-_retention_settings.png](img/slack_-_retention_settings.png \"Change Slack message retention. Renetion settings\")\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorniichuk%2Fcloudwatch-alarm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkorniichuk%2Fcloudwatch-alarm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorniichuk%2Fcloudwatch-alarm/lists"}