-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.tf
76 lines (60 loc) · 1.61 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
backend "s3" {
bucket = "kostis-terraform-state"
key = "aws-demo-lambda"
region = "us-east-1"
}
}
provider "aws" {
region = "us-east-1"
}
locals {
function_name = "simple"
function_handler = "demo.handler"
function_runtime = "nodejs18.x"
function_timeout_in_seconds = 5
function_source_dir = "${path.module}/aws_lambda_functions/${local.function_name}"
}
resource "aws_lambda_function" "function" {
function_name = "${local.function_name}-${var.env_name}"
handler = local.function_handler
runtime = local.function_runtime
timeout = local.function_timeout_in_seconds
filename = "${local.function_source_dir}.zip"
source_code_hash = data.archive_file.function_zip.output_base64sha256
role = aws_iam_role.function_role.arn
environment {
variables = {
ENVIRONMENT = var.env_name
}
}
}
data "archive_file" "function_zip" {
source_dir = local.function_source_dir
type = "zip"
output_path = "${local.function_source_dir}.zip"
}
resource "aws_iam_role" "function_role" {
name = "${local.function_name}-${var.env_name}"
assume_role_policy = jsonencode({
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
},
]
})
}
resource "aws_lambda_function_url" "function" {
function_name = aws_lambda_function.function.function_name
authorization_type = "NONE"
}