Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added subscription endpoint for PagerDuty #7

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Cost Optimization: By leveraging the insights provided by the module, you can op
The module is designed to handle large-scale AWS environments, accommodating growing workloads and enabling you to seamlessly monitor cost anomalies across your entire infrastructure.
Whether you are a small startup or a large enterprise, the AWS Cost Anomaly Detection Terraform module offers an efficient and reliable solution to automate the detection and management of cost anomalies in your AWS environment. Start using this module today and gain valuable insights into your cloud spending, empowering you to make informed decisions and optimize your AWS costs.

# PagerDuty setup
PagerDuty service shall be an Amazon Cloudwatch service. Once created you should copy the PagerDuty Integration URL to the endpoint_url input variable
# Module documentation:

## Requirements
Expand All @@ -41,6 +43,7 @@ No modules.
| [aws_ce_anomaly_subscription.realtime_subscription](https://registry.terraform.io/providers/hashicorp/aws/5.6.2/docs/resources/ce_anomaly_subscription) | resource |
| [aws_sns_topic.cost_anomaly_updates](https://registry.terraform.io/providers/hashicorp/aws/5.6.2/docs/resources/sns_topic) | resource |
| [aws_sns_topic_policy.default](https://registry.terraform.io/providers/hashicorp/aws/5.6.2/docs/resources/sns_topic_policy) | resource |
| [aws_sns_topic_subscription.pagerduty](https://registry.terraform.io/providers/hashicorp/aws/5.6.2/docs/resources/sns_topic_subscription) | resource |
| [aws_sns_topic_subscription.topic_email_subscription](https://registry.terraform.io/providers/hashicorp/aws/5.6.2/docs/resources/sns_topic_subscription) | resource |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/5.6.2/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.sns_topic_policy](https://registry.terraform.io/providers/hashicorp/aws/5.6.2/docs/data-sources/iam_policy_document) | data source |
Expand All @@ -49,12 +52,17 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_create_pagerduty"></a> [create\_pagerduty](#input\_create\_pagerduty) | Set to true in order to send notifications to PagerDuty | `bool` | `false` | no |
| <a name="input_emails"></a> [emails](#input\_emails) | List of email addresses to notify | `list(any)` | n/a | yes |
| <a name="input_environment"></a> [environment](#input\_environment) | The account environment (Prod / Dev etc.) | `string` | n/a | yes |
| <a name="input_pagerduty_endpoint"></a> [pagerduty\_endpoint](#input\_pagerduty\_endpoint) | The PagerDuty HTTPS endpoint where SNS notifications will be sent to | `string` | n/a | yes |
| <a name="input_raise_amount_absolute"></a> [raise\_amount\_absolute](#input\_raise\_amount\_absolute) | The Absolut increase in USD to trigger the detector. (ANOMALY\_TOTAL\_IMPACT\_ABSOLUTE) | `string` | n/a | yes |
| <a name="input_raise_amount_percent"></a> [raise\_amount\_percent](#input\_raise\_amount\_percent) | An Expression object used to specify the anomalies that you want to generate alerts for. The precentage service cost increase than the expected | `string` | n/a | yes |
| <a name="input_region"></a> [region](#input\_region) | AWS Region | `string` | n/a | yes |
| <a name="input_resource_tags"></a> [resource\_tags](#input\_resource\_tags) | Tags to set for all resources | `map(string)` | `{}` | no |

## Outputs

No outputs.
| Name | Description |
|------|-------------|
| <a name="output_sns_arn"></a> [sns\_arn](#output\_sns\_arn) | n/a |
24 changes: 20 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ resource "aws_sns_topic_subscription" "topic_email_subscription" {
endpoint = var.emails[count.index]
}

resource "aws_sns_topic_subscription" "pagerduty" {
count = var.create_pagerduty ? 1 : 0
endpoint = var.pagerduty_endpoint
endpoint_auto_confirms = true
protocol = "https"
topic_arn = aws_sns_topic.cost_anomaly_updates.arn
}

data "aws_iam_policy_document" "sns_topic_policy" {
policy_id = "__default_policy_ID"
Expand Down Expand Up @@ -104,10 +111,19 @@ resource "aws_ce_anomaly_subscription" "realtime_subscription" {
name = "RealtimeAnomalySubscription"
frequency = "IMMEDIATE"
threshold_expression {
dimension {
key = "ANOMALY_TOTAL_IMPACT_PERCENTAGE"
values = [var.raise_amount_percent]
match_options = ["GREATER_THAN_OR_EQUAL"]
or {
dimension {
key = "ANOMALY_TOTAL_IMPACT_PERCENTAGE"
values = [var.raise_amount_percent]
match_options = ["GREATER_THAN_OR_EQUAL"]
}
}
or {
dimension {
key = "ANOMALY_TOTAL_IMPACT_ABSOLUTE"
values = [var.raise_amount_absolute]
match_options = ["GREATER_THAN_OR_EQUAL"]
}
}
}
monitor_arn_list = [
Expand Down
3 changes: 3 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "sns_arn" {
value = aws_sns_topic.cost_anomaly_updates.arn
}
15 changes: 15 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,23 @@ variable "raise_amount_percent" {
description = "An Expression object used to specify the anomalies that you want to generate alerts for. The precentage service cost increase than the expected"
}

variable "raise_amount_absolute" {
type = string
description = "The Absolut increase in USD to trigger the detector. (ANOMALY_TOTAL_IMPACT_ABSOLUTE)"
}
variable "resource_tags" {
description = "Tags to set for all resources"
type = map(string)
default = {}
}

variable "create_pagerduty" {
type = bool
default = false
description = "Set to true in order to send notifications to PagerDuty"
}

variable "pagerduty_endpoint" {
description = "The PagerDuty HTTPS endpoint where SNS notifications will be sent to"
type = string
}
Loading