Skip to content

Commit aab00af

Browse files
committed
initial commit
0 parents  commit aab00af

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
## Module for creating cron based lambdas
2+
3+
What it does:
4+
- Creates a role for lambda
5+
- Creates and attaches policy to the role
6+
- Creates Cloudwatch cron, connects it to lambda and grants permission to run lambda.
7+
- Creates lambda function
8+
9+
## Module Variables
10+
- `region` - The AWS region. Defaults to us-east-1"
11+
- `enabled` - bool, defaults to `true`
12+
- `lambda_name` - Name for lambda function
13+
- `project` - Project lambda belongs to
14+
- `runtime` - Runtime for lambda function
15+
- `handler` - Handler for lambda function
16+
- `lambda_zipfile` - Path to zip file that contains lambda function
17+
- `source_code_hash` - The hash for lambda Zip file"
18+
- `lambda_policy_document` - Path to policy document for [lambda function](http://docs.aws.amazon.com/lambda/latest/dg/intro-permission-model.html#lambda-intro-execution-role)
19+
- `description` - Description for lambda function
20+
- `timeout` - Timeout for lambda function
21+
- `subnet_ids` - The list of subnets functions belongs to
22+
- `security_group_ids` - The list of security groups
23+
24+
## Usage:
25+
```sh
26+
module "lambda_test" {
27+
source = "../../terraform/modules/aws_lambda_cron"
28+
lambda_name = "test_inventory"
29+
runtime = "python3.6"
30+
lambda_zipfile = "${path.module}/function/function.zip"
31+
source_code_hash = "${base64sha256(file("function/function.zip"))}"
32+
handler = "redshift_inventory.lambda_handler"
33+
schedule_expression = "cron(0 */2 * * ? *)"
34+
policy_document = "${file("policies/lambda-policy.json")}"
35+
36+
# vpc_config parameters
37+
# don't set if you want lambda run off VPC
38+
# refer to terraform documentation
39+
# both variables are lists
40+
subnet_ids = ["subnet-7e19af35"]
41+
security_group_ids = ["sg-aa4519da"]
42+
# end vpc_config parameters
43+
44+
project = "Infra"
45+
description = "Test function"
46+
}
47+
```
48+
## Outputs
49+
- `lambda_arn` - the ARN for lmbda
50+
- `role_name` - the NAME for the role
51+
- `role_arn` - the ARN for role
52+
53+
## Author
54+
This module is created and maintained by [leandevops](https://github.com/leandevops)

main.tf

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "lambda_arn" {
2+
value = "${aws_lambda_function.self.*.arn}"
3+
}
4+
5+
output "role_name" {
6+
value = "${aws_iam_role.lambda_role.name}"
7+
}
8+
9+
output "role_arn" {
10+
value = "${aws_iam_role.lambda_role.arn}"
11+
}

variables.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
##############################
2+
# input and output variables
3+
##############################
4+
variable region {
5+
description = "The AWS region. Defaults to us-east-1"
6+
default = "us-east-1"
7+
}
8+
9+
variable enabled {
10+
default = true
11+
}
12+
13+
variable lambda_name {
14+
description = "Name for lambda function"
15+
}
16+
17+
variable project {
18+
description = "Project lambda belongs to"
19+
}
20+
21+
variable runtime {
22+
description = "Runtime for lambda function"
23+
}
24+
25+
variable handler {
26+
description = "Handler for lambda function"
27+
}
28+
29+
variable lambda_zipfile {
30+
description = "Zip that contains lambda function"
31+
}
32+
33+
variable source_code_hash {
34+
description = "The hash for lambda Zip file"
35+
}
36+
37+
variable description {
38+
description = "Description for lambda function"
39+
}
40+
41+
variable timeout {
42+
description = "Timeout for lambda function"
43+
default = 30
44+
}
45+
46+
variable policy_document {
47+
description = "policy document for lambda function"
48+
}
49+
50+
variable schedule_expression {
51+
description = "Expression for CloudWatcvh cron"
52+
}
53+
54+
variable subnet_ids {
55+
description = "List of VPC subnet ids that lambda belongs to"
56+
default = []
57+
}
58+
59+
variable security_group_ids {
60+
description = "List of security groups ids"
61+
default = []
62+
}

0 commit comments

Comments
 (0)