-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.tf
59 lines (50 loc) · 1.47 KB
/
lambda.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
data "archive_file" "lambda_code" {
type = "zip"
source_file = "${path.module}/lambda/src/main"
output_path = "${path.module}/lambda/src/main.zip"
}
resource "aws_s3_bucket" "lambda-code-gateway-demo" {
bucket = "lambda-code-gateway-demo"
tags = {
Name = "Lambda API Gateway demo code bucket"
}
}
resource "aws_s3_bucket_versioning" "versioned_demo_bucket" {
bucket = aws_s3_bucket.lambda-code-gateway-demo.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_object" "lambda-code" {
bucket = aws_s3_bucket.lambda-code-gateway-demo.id
key = "main.zip"
source = data.archive_file.lambda_code.output_path
}
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_lambda_function" "lambda_demo" {
function_name = "lambda_gateway_demo"
role = aws_iam_role.iam_for_lambda.arn
handler = "main"
runtime = "go1.x"
s3_bucket = aws_s3_object.lambda-code.bucket
s3_key = aws_s3_object.lambda-code.key
}
// We need this to enable writing logs to CloudWatch
resource "aws_iam_role_policy_attachment" "lambda_policy" {
role = aws_iam_role.iam_for_lambda.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}