|
| 1 | +# provider |
| 2 | +provider "aws" { |
| 3 | + region = "${var.region}" |
| 4 | +} |
| 5 | + |
| 6 | +# create role and policies |
| 7 | +resource "aws_iam_role" "lambda_role" { |
| 8 | + name = "${var.lambda_name}_role" |
| 9 | + description = "Role for ${var.lambda_name} Lambda function [Created with Terraform]" |
| 10 | + |
| 11 | + assume_role_policy = <<EOF |
| 12 | +{ |
| 13 | + "Version": "2012-10-17", |
| 14 | + "Statement": [ |
| 15 | + { |
| 16 | + "Action": "sts:AssumeRole", |
| 17 | + "Principal": { |
| 18 | + "Service": "lambda.amazonaws.com" |
| 19 | + }, |
| 20 | + "Effect": "Allow", |
| 21 | + "Sid": "" |
| 22 | + } |
| 23 | + ] |
| 24 | +} |
| 25 | +EOF |
| 26 | +} |
| 27 | + |
| 28 | +resource "aws_iam_policy" "lambda-policy" { |
| 29 | + name = "${var.lambda_name}_policy" |
| 30 | + description = "Defines resources ${var.lambda_name} lambda function has access to [Created with Terraform]" |
| 31 | + |
| 32 | + policy = "${var.policy_document}" |
| 33 | +} |
| 34 | + |
| 35 | +# attach policy to role |
| 36 | +resource "aws_iam_role_policy_attachment" "attach-policy" { |
| 37 | + role = "${aws_iam_role.lambda_role.name}" |
| 38 | + policy_arn = "${aws_iam_policy.lambda-policy.arn}" |
| 39 | +} |
| 40 | + |
| 41 | +# creates lambda |
| 42 | +resource "aws_lambda_function" "self" { |
| 43 | + #count = "${var.enabled}" |
| 44 | + |
| 45 | + runtime = "${var.runtime}" |
| 46 | + function_name = "${var.lambda_name}" |
| 47 | + filename = "${var.lambda_zipfile}" |
| 48 | + role = "${aws_iam_role.lambda_role.arn}" |
| 49 | + handler = "${var.handler}" |
| 50 | + source_code_hash = "${var.source_code_hash}" |
| 51 | + description = "${var.description} [Created with Terraform]" |
| 52 | + |
| 53 | + publish = true |
| 54 | + timeout = "${var.timeout}" |
| 55 | + |
| 56 | + vpc_config = { |
| 57 | + subnet_ids = "${var.subnet_ids}" |
| 58 | + security_group_ids = "${var.security_group_ids}" |
| 59 | + } |
| 60 | + |
| 61 | + tags = { |
| 62 | + name = "${var.lambda_name}" |
| 63 | + createdWith = "Terraform" |
| 64 | + BUDGET_GROUP = "${var.project}" |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +# create CloudWatch cron |
| 69 | +resource "aws_cloudwatch_event_rule" "cron" { |
| 70 | + count = "${var.enabled}" |
| 71 | + name = "${var.lambda_name}-cron" |
| 72 | + description = "Sends event to ${var.lambda_name} cron based [Created with Terraform]" |
| 73 | + schedule_expression = "${var.schedule_expression}" |
| 74 | +} |
| 75 | + |
| 76 | +resource "aws_cloudwatch_event_target" "lambda" { |
| 77 | + count = "${var.enabled}" |
| 78 | + target_id = "runLambda" |
| 79 | + rule = "${aws_cloudwatch_event_rule.cron.name}" |
| 80 | + arn = "${aws_lambda_function.self.arn}" |
| 81 | +} |
| 82 | + |
| 83 | +resource "aws_lambda_permission" "cloudwatch" { |
| 84 | + count = "${var.enabled}" |
| 85 | + statement_id = "AllowExecutionFromCloudWatch" |
| 86 | + action = "lambda:InvokeFunction" |
| 87 | + function_name = "${aws_lambda_function.self.arn}" |
| 88 | + principal = "events.amazonaws.com" |
| 89 | + source_arn = "${aws_cloudwatch_event_rule.cron.arn}" |
| 90 | +} |
0 commit comments