Skip to content

Commit

Permalink
adding TF w/ Slack Notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Amichay Oren committed Apr 19, 2023
1 parent bff386b commit cfde353
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
Empty file added functions/notifier/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions functions/notifier/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json

import requests

SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/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"


def slackNotifier(request):
def send_slack_notification(message):
headers = {"Content-type": "application/json"}
payload = {"text": message}

response = requests.post(
SLACK_WEBHOOK_URL, headers=headers, data=json.dumps(payload)
)

if response.status_code == 200:
print("Notification sent successfully")
else:
print("Failed to send notification")

message = (
"Slack Message Sent Directly from GCP following Terraform Deployment"
)
send_slack_notification(message=message)
return message
1 change: 1 addition & 0 deletions functions/notifier/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
73 changes: 73 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
provider "google" {
project = var.project_id
region = var.region
}

terraform {
backend "gcs" {
bucket = "tf-state-bucket"
prefix = ""
}
}

variable "slack_notification_url" {
default = "https://hooks.slack.com/services/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"
}

variable "env" {
default = "dev"
}

variable "project_id" {
default = "development-380917"
}

variable "region" {
default = "us-east4"
}

locals {
resource_prefix = var.env == "prod" ? "" : "${var.env}-"
}



# --------------------------
# -- Function Storage Bucket
# --------------------------
resource "google_storage_bucket" "serverless_function_bucket" {
name = "${local.resource_prefix}serverless-function-bucket"
location = "US"
}



# --------------------------
# -- Slack Notifier Function
# --------------------------
data "archive_file" "slack_notifier" {
type = "zip"
output_path = "/tmp/slack_notifier.zip"
source_dir = "functions/notifier"
}

resource "google_storage_bucket_object" "slack_notifier_zip" {
name = "${local.resource_prefix}slack-notifier.zip"
bucket = google_storage_bucket.serverless_function_bucket.name
content_type = "application/zip"
source = data.archive_file.slack_notifier.output_path
depends_on = [
google_storage_bucket.serverless_function_bucket
]
}

resource "google_cloudfunctions_function" "slack_notifier" {
name = "${local.resource_prefix}slack-notifier"
description = "Slack Notifier Function"
runtime = "python311"
source_archive_bucket = google_storage_bucket.serverless_function_bucket.name
source_archive_object = google_storage_bucket_object.slack_notifier_zip.name
trigger_http = true
entry_point = "slackNotifier"
available_memory_mb = 128
}
2 changes: 1 addition & 1 deletion tests/test_link.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests

base_url: str = "https://api.nine30.com/link"
base_url: str = "https://api.nine30.com/bank/link"


def no_test_link_missing_parameter() -> None:
Expand Down

0 comments on commit cfde353

Please sign in to comment.