-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmain.tf
More file actions
102 lines (85 loc) · 2.29 KB
/
main.tf
File metadata and controls
102 lines (85 loc) · 2.29 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 0.14.9"
}
provider "aws" {
profile = "default"
region = "us-east-1"
}
variable "event_bus_name" {
type = string
default = "default"
}
data "aws_cloudwatch_event_bus" "event_bus" {
name = var.event_bus_name
}
resource "aws_lambda_function" "publisher_function" {
function_name = "PublisherFunction"
filename = data.archive_file.lambda_zip_file.output_path
source_code_hash = data.archive_file.lambda_zip_file.output_base64sha256
handler = "app.handler"
role = aws_iam_role.iam_for_lambda.arn
runtime = "nodejs22.x"
}
data "archive_file" "lambda_zip_file" {
type = "zip"
source_file = "${path.module}/src/app.js"
output_path = "${path.module}/lambda.zip"
}
data "aws_iam_policy" "lambda_basic_execution_role_policy" {
name = "AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role" "iam_for_lambda" {
name_prefix = "PublisherFunctionRole-"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_basic_execution" {
role = aws_iam_role.iam_for_lambda.name
policy_arn = data.aws_iam_policy.lambda_basic_execution_role_policy.arn
}
resource "aws_iam_role_policy_attachment" "lambda_eventbridge" {
role = aws_iam_role.iam_for_lambda.name
policy_arn = aws_iam_policy.event_bridge_put_events_policy.arn
}
data "aws_iam_policy_document" "event_bridge_put_events_policy_document" {
statement {
effect = "Allow"
actions = [
"events:PutEvents"
]
resources = [
data.aws_cloudwatch_event_bus.event_bus.arn
]
}
}
resource "aws_iam_policy" "event_bridge_put_events_policy" {
name_prefix = "event_bridge_put_events_policy-"
path = "/"
policy = data.aws_iam_policy_document.event_bridge_put_events_policy_document.json
lifecycle {
create_before_destroy = true
}
}
output "publisher_function" {
value = aws_lambda_function.publisher_function.arn
description = "PublisherFunction function name"
}