-
Notifications
You must be signed in to change notification settings - Fork 7
/
executor.tf
142 lines (110 loc) · 3.42 KB
/
executor.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# defining aws region
provider "aws" {
region = "${var.aws_region}"
}
# defining policies for aws s3 read and write access
data "aws_iam_policy_document" "s3-access" {
statement {
actions = [
"s3:GetObject",
"s3:PutObject",
]
resources = [
"arn:aws:s3:::*",
]
}
}
resource "aws_iam_policy" "s3-access" {
name = "s3-access"
path = "/"
policy = "${data.aws_iam_policy_document.s3-access.json}"
}
# defining aws roles and policies for a lambda function
data "aws_iam_policy_document" "lambda-assume-role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "lambda-exec" {
name = "lambda-exec-role"
assume_role_policy = "${data.aws_iam_policy_document.lambda-assume-role.json}"
}
resource "aws_iam_role_policy_attachment" "lambda-exec" {
role = "${aws_iam_role.lambda-exec.name}"
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "s3-access" {
role = "${aws_iam_role.lambda-exec.name}"
policy_arn = "${aws_iam_policy.s3-access.arn}"
}
# defining aws roles and policies for a step functions state machine
data "aws_iam_policy_document" "sfn-assume-role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["states.${var.aws_region}.amazonaws.com"]
}
}
}
resource "aws_iam_role" "sfn-exec" {
name = "sfn-exec"
assume_role_policy = "${data.aws_iam_policy_document.sfn-assume-role.json}"
}
data "aws_iam_policy_document" "lambda-invoke" {
statement {
actions = [
"lambda:InvokeFunction"
]
resources = [
"*",
]
}
}
resource "aws_iam_policy" "lambda-invoke" {
name = "lambda-invoke"
policy = "${data.aws_iam_policy_document.lambda-invoke.json}"
}
resource "aws_iam_role_policy_attachment" "lambda-invoke" {
role = "${aws_iam_role.sfn-exec.name}"
policy_arn = "${aws_iam_policy.lambda-invoke.arn}"
}
# creating an aws s3 bucket
resource "aws_s3_bucket" "lambda-bucket" {
bucket = "${var.bucket_name}"
}
# adding the lambda archive to the defined bucket
resource "aws_s3_bucket_object" "lambda-package" {
bucket = "${aws_s3_bucket.lambda-bucket.bucket}"
key = "${var.lambda_function_bucket_name}"
source = "${var.lambda_archive_file}"
etag = "${md5(file(var.lambda_archive_file))}"
depends_on = ["aws_s3_bucket.lambda-bucket"]
}
# defining aws lambda function
resource "aws_lambda_function" "lambda-function" {
function_name = "${var.lambda_function_name}"
s3_bucket = "${aws_s3_bucket_object.lambda-package.bucket}"
s3_key = "${aws_s3_bucket_object.lambda-package.key}"
handler = "${var.lambda_handler}"
runtime = "${var.lambda_runtime}"
timeout= "${var.lambda_timeout}"
role = "${aws_iam_role.lambda-exec.arn}"
}
# defining aws step functions state machine
resource "aws_sfn_state_machine" "sfn_state_machine" {
name = "${var.step_function_name}"
role_arn = "${aws_iam_role.sfn-exec.arn}"
definition = "${data.template_file.sfn-definition.rendered}"
}
# step function definition template
data "template_file" "sfn-definition" {
template = "${file(var.step_function_definition_file)}"
vars {
lambda-arn = "${aws_lambda_function.lambda-function.arn}"
}
}