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

feat: API alarms module #19

Merged
merged 1 commit into from
Feb 14, 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
5 changes: 5 additions & 0 deletions .github/workflows/tf_apply_production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env:
TF_VAR_api_config: ${{ secrets.API_CONFIG }}
TF_VAR_log_analytics_workspace_id: ${{ secrets.LOG_ANALYTICS_WORKSPACE_ID }}
TF_VAR_log_analytics_workspace_key: ${{ secrets.LOG_ANALYTICS_WORKSPACE_KEY }}
TF_VAR_slack_webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}

permissions:
id-token: write
Expand Down Expand Up @@ -51,3 +52,7 @@ jobs:
- name: Apply cloudfront
working-directory: terragrunt/env/production/cloudfront
run: terragrunt apply --terragrunt-non-interactive -auto-approve

- name: Apply alarms
working-directory: terragrunt/env/production/alarms
run: terragrunt apply --terragrunt-non-interactive -auto-approve
4 changes: 3 additions & 1 deletion .github/workflows/tf_plan_production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env:
TF_VAR_api_config: ${{ secrets.API_CONFIG }}
TF_VAR_log_analytics_workspace_id: ${{ secrets.LOG_ANALYTICS_WORKSPACE_ID }}
TF_VAR_log_analytics_workspace_key: ${{ secrets.LOG_ANALYTICS_WORKSPACE_KEY }}
TF_VAR_slack_webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}

permissions:
id-token: write
Expand All @@ -29,7 +30,8 @@ jobs:
strategy:
fail-fast: false
matrix:
include:
include:
- module: alarms
- module: api
- module: cloudfront
- module: hosted_zone
Expand Down
57 changes: 57 additions & 0 deletions terragrunt/aws/alarms/alarms.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
resource "aws_cloudwatch_log_metric_filter" "api_error" {
name = local.error_logged_api
pattern = "?ERROR ?Error ?error ?failed"
log_group_name = local.api_cloudwatch_log_group

metric_transformation {
name = local.error_logged_api
namespace = local.metric_namespace
value = "1"
}
}

resource "aws_cloudwatch_metric_alarm" "api_error" {
alarm_name = local.error_logged_api
alarm_description = "Errors logged by the API lambda function"
comparison_operator = "GreaterThanOrEqualToThreshold"

metric_name = aws_cloudwatch_log_metric_filter.api_error.metric_transformation[0].name
namespace = aws_cloudwatch_log_metric_filter.api_error.metric_transformation[0].namespace
period = "60"
evaluation_periods = "1"
statistic = "Sum"
threshold = "1"
treat_missing_data = "notBreaching"

alarm_actions = [local.sns_topic_arn]
ok_actions = [local.sns_topic_arn]
}

resource "aws_cloudwatch_log_metric_filter" "api_secret_detected" {
name = local.secret_detected_api
pattern = "Secret detected"
log_group_name = local.api_cloudwatch_log_group

metric_transformation {
name = local.secret_detected_api
namespace = local.metric_namespace
value = "1"
}
}

resource "aws_cloudwatch_metric_alarm" "api_secret_detected" {
alarm_name = local.secret_detected_api
alarm_description = "GitHub alert that a secret has been detected"
comparison_operator = "GreaterThanOrEqualToThreshold"

metric_name = aws_cloudwatch_log_metric_filter.api_secret_detected.metric_transformation[0].name
namespace = aws_cloudwatch_log_metric_filter.api_secret_detected.metric_transformation[0].namespace
period = "60"
evaluation_periods = "1"
statistic = "Sum"
threshold = "1"
treat_missing_data = "notBreaching"

alarm_actions = [local.sns_topic_arn]
ok_actions = [local.sns_topic_arn]
}
10 changes: 10 additions & 0 deletions terragrunt/aws/alarms/inputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "api_function_name" {
description = "The name of the API function."
type = string
}

variable "slack_webhook_url" {
description = "The URL of the Slack webhook."
type = string
sensitive = true
}
7 changes: 7 additions & 0 deletions terragrunt/aws/alarms/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
locals {
api_cloudwatch_log_group = "/aws/lambda/${var.api_function_name}"
error_logged_api = "ErrorLoggedAPI"
metric_namespace = "GitHubSecretScanning"
secret_detected_api = "SecretDetectedAPI"
sns_topic_arn = "arn:aws:sns:${var.region}:${var.account_id}:internal-sre-alert"
}
12 changes: 12 additions & 0 deletions terragrunt/aws/alarms/slack.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module "cloudwatch_alarms_slack" {
source = "github.com/cds-snc/terraform-modules?ref=v5.0.2//notify_slack"

function_name = var.product_name
project_name = var.product_name
slack_webhook_url = var.slack_webhook_url
sns_topic_arns = [
local.sns_topic_arn,
]

billing_tag_value = var.billing_code
}
44 changes: 44 additions & 0 deletions terragrunt/env/production/alarms/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions terragrunt/env/production/alarms/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
terraform {
source = "../../../aws//alarms"
}

dependencies {
paths = ["../api"]
}

dependency "api" {
config_path = "../api"

mock_outputs_allowed_terraform_commands = ["init", "fmt", "validate", "plan", "show"]
mock_outputs_merge_with_state = true
mock_outputs = {
api_function_name = "github-secret-scanning-api"
}
}

inputs = {
api_function_name = dependency.api.outputs.function_name
}

include {
path = find_in_parent_folders()
}