-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
66 lines (57 loc) · 1.6 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
resource "null_resource" "pip_install" {
triggers = {
shell_hash = "${sha256(file("${path.module}/requirements.txt"))}"
}
provisioner "local-exec" {
command = "python3 -m pip install -r requirements.txt -t ${path.module}/layer/python"
}
}
data "archive_file" "layer" {
type = "zip"
source_dir = "${path.module}/layer"
output_path = "${path.module}/layer.zip"
depends_on = [null_resource.pip_install]
}
resource "aws_lambda_layer_version" "layer" {
layer_name = "test-layer"
filename = data.archive_file.layer.output_path
source_code_hash = data.archive_file.layer.output_base64sha256
compatible_runtimes = ["python3.9", "python3.8", "python3.7", "python3.6"]
}
data "archive_file" "code" {
type = "zip"
source_dir = "${path.module}/lambda_code"
output_path = "${path.module}/code.zip"
}
resource "aws_iam_role" "iam_role" {
name = "lambda-iam-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_lambda_function" "lambda" {
function_name = "test-lambda"
handler = "lambda.main"
runtime = "python3.9"
filename = data.archive_file.code.output_path
source_code_hash = data.archive_file.code.output_base64sha256
role = aws_iam_role.iam_role.arn
layers = [aws_lambda_layer_version.layer.arn]
environment {
variables = {
"MESSAGE" = "Terraform sends its regards"
}
}
}