-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmain.tf
More file actions
103 lines (93 loc) · 2.59 KB
/
main.tf
File metadata and controls
103 lines (93 loc) · 2.59 KB
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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 0.14.9"
}
provider "aws" {
region = "us-east-1" # Change to your desired region
}
resource "aws_kinesis_stream" "sample_stream" {
name = "sample-stream"
shard_count = 1
retention_period = 24
}
data "archive_file" "lambda_zip_file" {
type = "zip"
source_file = "${path.module}/src/app.js"
output_path = "${path.module}/lambda.zip"
}
resource "aws_lambda_function" "sample_lambda" {
filename = data.archive_file.lambda_zip_file.output_path
source_code_hash = data.archive_file.lambda_zip_file.output_base64sha256
function_name = "sample-lambda"
role = aws_iam_role.lambda_role.arn
handler = "app.handler"
runtime = "nodejs22.x" # Change to your preferred runtime
}
resource "aws_iam_role" "lambda_role" {
name = "lambda-role"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_iam_policy" "lambda_kinesis_policy" {
name = "lambda-kinesis-policy"
policy = jsonencode(
{
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = [
"kinesis:GetRecords",
"kinesis:GetShardIterator",
"kinesis:DescribeStream",
"kinesis:DescribeStreamSummary",
"kinesis:ListShards",
"kinesis:ListStreams"
],
Resource = aws_kinesis_stream.sample_stream.arn
},
{
Effect = "Allow",
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
Resource = "arn:aws:logs:*:*:*"
}
]
}
)
}
resource "aws_iam_role_policy_attachment" "lambda_kinesis_policy_attachment" {
role = aws_iam_role.lambda_role.name
policy_arn = aws_iam_policy.lambda_kinesis_policy.arn
}
resource "aws_lambda_event_source_mapping" "sample_mapping" {
event_source_arn = aws_kinesis_stream.sample_stream.arn
function_name = aws_lambda_function.sample_lambda.arn
starting_position = "LATEST"
}
output "kinesis_data_stream" {
value = aws_kinesis_stream.sample_stream.arn
description = "Kinesis data stream with shards"
}
output "consumer_function" {
value = aws_lambda_function.sample_lambda.arn
description = "Consumer Function function name"
}