-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
* Importing existing SNS * Updating to use lambda
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
health-lambda-function-payload.zip |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import boto3 | ||
import json | ||
import os | ||
|
||
def lambda_handler(event, context): | ||
sns_client = boto3.client('sns') | ||
topic_arn = os.environ.get('SNS_TOPIC_ARN') | ||
event_rule_name = os.environ.get('EVENT_RULE_NAME') | ||
|
||
event_subject = f'ALARM: {event_rule_name}: {event["detail"]["eventTypeCode"]}' | ||
event_message = "".join( | ||
[ | ||
f'{event["detail"]["service"]}\n', | ||
f'Event Type: {event["detail"]["eventTypeCode"]}\n', | ||
f'Status: {event["detail"]["statusCode"]}\n', | ||
event["detail"]["eventDescription"][0]["latestDescription"], | ||
] | ||
) | ||
|
||
response = sns_client.publish( | ||
TopicArn=topic_arn, | ||
Message=event_message, | ||
Subject=event_subject, | ||
MessageAttributes={ | ||
'string': { | ||
'DataType': 'String', | ||
'StringValue': 'String' | ||
} | ||
} | ||
) | ||
|
||
print(response) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
resource "aws_iam_role" "health_lambda_iam" { | ||
name = var.event_rule_name | ||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Action = "sts:AssumeRole" | ||
Effect = "Allow" | ||
Sid = "" | ||
Principal = { Service = "lambda.amazonaws.com" } | ||
}, | ||
] | ||
}) | ||
inline_policy { | ||
name = "sns" | ||
policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Action = ["sns:Publish"] | ||
Effect = "Allow" | ||
Resource = var.sns_topic_name != "" ? aws_sns_topic.health_event_topic[0].arn : var.sns_topic_arn | ||
}, | ||
{ | ||
Action = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"] | ||
Effect = "Allow" | ||
Resource = "arn:aws:logs:*:*:*" | ||
} | ||
] | ||
}) | ||
} | ||
} | ||
|
||
data "archive_file" "health_lambda" { | ||
Check warning on line 34 in lambda.tf GitHub Actions / Lint
Check warning on line 34 in lambda.tf GitHub Actions / Lint
|
||
type = "zip" | ||
source_file = "${path.module}/health-lambda.py" | ||
output_path = "${path.module}/health-lambda-function-payload.zip" | ||
} | ||
|
||
resource "aws_lambda_function" "health_lambda" { | ||
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
Check failure on line 40 in lambda.tf GitHub Actions / scan
|
||
filename = "${path.module}/health-lambda-function-payload.zip" | ||
function_name = var.event_rule_name | ||
role = aws_iam_role.health_lambda_iam.arn | ||
handler = "health-lambda.lambda_handler" | ||
source_code_hash = data.archive_file.health_lambda.output_base64sha256 | ||
runtime = "python3.12" | ||
environment { | ||
variables = { | ||
SNS_TOPIC_ARN = var.sns_topic_name != "" ? aws_sns_topic.health_event_topic[0].arn : var.sns_topic_arn | ||
EVENT_RULE_NAME = var.event_rule_name | ||
} | ||
} | ||
} | ||
|
||
resource "aws_cloudwatch_event_target" "health_lambda" { | ||
rule = aws_cloudwatch_event_rule.console.name | ||
target_id = "health_lambda" | ||
arn = aws_lambda_function.health_lambda.arn | ||
} | ||
|
||
resource "aws_lambda_permission" "health_lambda" { | ||
statement_id = "AllowExecutionFromCloudWatch" | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.health_lambda.function_name | ||
principal = "events.amazonaws.com" | ||
source_arn = aws_cloudwatch_event_rule.console.arn | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
output "event_bridge_rule_id" { | ||
value = aws_cloudwatch_event_rule.console[0].id | ||
value = aws_cloudwatch_event_rule.console.id | ||
} | ||
|
||
output "sns_topic_arn" { | ||
value = aws_sns_topic.health_event_topic[0].arn | ||
value = var.sns_topic_name != "" ? aws_sns_topic.health_event_topic[0].arn : var.sns_topic_arn | ||
} |