generated from gsweene2/terraform-starter-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
165 lines (137 loc) · 3.36 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
terraform {
required_version = ">= 0.12"
}
provider "aws" {
region = var.aws_region
}
# IAM Role Creation
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# IAM Policy Creation: Allow Cloudwatch Logging
resource "aws_iam_policy" "allow_logging" {
name = "allow_logging"
path = "/"
description = "IAM policy for logging from a lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
}
]
}
EOF
}
# IAM Policy Creation: Allow Kinesis Processing
resource "aws_iam_policy" "allow_kinesis_processing" {
name = "allow_kinesis_processing"
path = "/"
description = "IAM policy for logging from a lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"kinesis:ListShards",
"kinesis:ListStreams",
"kinesis:*"
],
"Resource": "arn:aws:kinesis:*:*:*",
"Effect": "Allow"
},
{
"Action": [
"stream:GetRecord",
"stream:GetShardIterator",
"stream:DescribeStream",
"stream:*"
],
"Resource": "arn:aws:stream:*:*:*",
"Effect": "Allow"
}
]
}
EOF
}
# Attach IAM Policies to Roles
resource "aws_iam_role_policy_attachment" "lambda_logs" {
role = aws_iam_role.iam_for_lambda.name
policy_arn = aws_iam_policy.allow_logging.arn
}
resource "aws_iam_role_policy_attachment" "kinesis_processing" {
role = aws_iam_role.iam_for_lambda.name
policy_arn = aws_iam_policy.allow_kinesis_processing.arn
}
# Create Kinesis Data Stream
resource "aws_kinesis_stream" "kinesis_stream" {
name = "terraform-kinesis-stream"
shard_count = 2
retention_period = 48
shard_level_metrics = [
"IncomingBytes",
"IncomingRecords",
"OutgoingBytes",
"OutgoingRecords",
]
tags = {
CreatedBy = "terraform-kinesis-lambda"
}
}
# Build Lambda Zip
resource "null_resource" "build_zip" {
provisioner "local-exec" {
command = "zip lambda_function.zip lambda_function.py"
}
}
# Create AWS Lambda
resource "aws_lambda_function" "test_lambda" {
filename = "lambda_function.zip"
function_name = "terraform-kinesis-lambda"
role = aws_iam_role.iam_for_lambda.arn
handler = "lambda_function.lambda_handler"
source_code_hash = filebase64sha256("lambda_function.zip")
runtime = "python3.8"
environment {
variables = {
DATA_STREAM_NAME = "someKinesis"
}
}
}
resource "aws_lambda_function_event_invoke_config" "example" {
function_name = aws_lambda_function.test_lambda.function_name
maximum_event_age_in_seconds = 60
maximum_retry_attempts = 0
}
# Create Lambda Event Source Mapping
resource "aws_lambda_event_source_mapping" "example" {
event_source_arn = aws_kinesis_stream.kinesis_stream.arn
function_name = aws_lambda_function.test_lambda.arn
starting_position = "LATEST"
depends_on = [
aws_iam_role_policy_attachment.kinesis_processing
]
}