Last active
August 15, 2022 16:53
-
-
Save nhammad/0e7a6d3369eaae5bdc99ddad0ba2c309 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data "archive_file" "lambda_zip" { | |
type = "zip" | |
source_file = "${path.module}/src/turn_off_lambda.py" | |
output_file_mode = "0666" | |
output_path = "${path.module}/bin/turn_off_lambda.zip" | |
} | |
resource "aws_lambda_function" "processing_lambda" { | |
filename = data.archive_file.lambda_zip.output_path | |
function_name = local.processing_lambda_name | |
handler = "turn_off_lambda.lambda_handler" | |
source_code_hash = data.archive_file.lambda_zip.output_base64sha256 | |
role = aws_iam_role.processing_lambda_role.arn | |
runtime = "python3.9" | |
environment { | |
variables = { | |
action = var.action | |
alarm_arns_lst = jsonencode(var.alarm_arns_lst) | |
} | |
} | |
} | |
resource "aws_iam_role" "processing_role" { | |
name = local.role_name | |
path = "/service-role/" | |
assume_role_policy = data.aws_iam_policy_document.assume-role-document.json | |
inline_policy { | |
name = "lambda_policy" | |
policy = data.aws_iam_policy_document.alarms_policy_document.json | |
} | |
} | |
data "aws_iam_policy_document" "alarms_policy_document" { | |
statement { | |
actions = [ | |
"ec2:*" | |
] | |
resources = [ | |
"*", | |
] | |
} | |
statement { | |
actions = [ | |
"sns:*", | |
] | |
resources = [ | |
aws_sns_topic.turn-off-topic.arn, | |
] | |
} | |
statement { | |
actions = [ | |
"logs:CreateLogGroup", | |
"logs:CreateLogStream", | |
"logs:PutLogEvents", | |
"logs:AssociateKmsKey" | |
] | |
resources = ["*"] | |
} | |
data "aws_iam_policy_document" "assume-role-document" { | |
statement { | |
actions = ["sts:AssumeRole"] | |
principals { | |
type = "Service" | |
identifiers = ["lambda.amazonaws.com"] | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment