-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlambda.tf
65 lines (58 loc) · 1.77 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
60
61
62
63
64
65
provider "aws" {
region = "eu-west-1" # Specify your desired AWS region
}
# IAM Role for Lambda
resource "aws_iam_role" "lambda_role" {
name = "lambda_exec_role_2"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "lambda.amazonaws.com"
}
}]
})
}
# IAM Policy for Lambda logging to CloudWatch
resource "aws_iam_role_policy" "lambda_policy" {
role = aws_iam_role.lambda_role.id
policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"s3:*"
],
Effect = "Allow",
Resource = "*"
}]
})
}
# Zip the Lambda function code
data "archive_file" "lambda_zip" {
type = "zip"
source_file = "lambda_function.py"
output_path = "${path.module}/lambda_function.zip"
}
# Create the Lambda function
# see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function.html
resource "aws_lambda_function" "hello_world" {
source_code_hash = data.archive_file.lambda_zip.output_sha
function_name = "hello_world_lambda"
role = aws_iam_role.lambda_role.arn
handler = "lambda_function.lambda_handler"
runtime = "python3.9"
filename = data.archive_file.lambda_zip.output_path
source_code_hash = filebase64sha256(data.archive_file.lambda_zip.output_path)
}
# Create a Lambda permission to allow invocation
resource "aws_lambda_permission" "allow_invocation" {
statement_id = "AllowExecutionFromAPI"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.hello_world.function_name
principal = "apigateway.amazonaws.com"
}