-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.tf
More file actions
129 lines (108 loc) · 3.56 KB
/
Copy pathlambda.tf
File metadata and controls
129 lines (108 loc) · 3.56 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
resource "aws_iam_role" "lambda_role" {
name = "lambda_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "null_resource" "validate_python3" {
provisioner "local-exec" {
command = "PYTHONPATH=lambda python -m py_compile lambda/function.py"
on_failure = fail # Fail if the command returns a non-zero exit code
}
triggers = {
python_file_hash = filesha256("lambda/function.py")
calculator_py_hash = filesha256("lambda/calculator.py")
test_calc_hash = filesha256("lambda/test_calc.py")
}
}
resource "null_resource" "run_tests" {
provisioner "local-exec" {
command = "PYTHONPATH=lambda python -m unittest lambda/test_calc.py && python -m unittest lambda/test_calc_remote.py"
on_failure = fail # Fail if the command returns a non-zero exit code
}
triggers = {
python_file_hash = filesha256("lambda/test_calc.py")
remote_test_hash = filesha256("lambda/test_calc_remote.py")
calculator_py_hash = filesha256("lambda/calculator.py")
}
}
/*
resource "null_resource" "lambda_sam_local" {
provisioner "local-exec" {
command = "sam local invoke calculator_lambda -e test_payload.json"
}
triggers = {
python_file_hash = filesha256("lambda/function.py")
}
}
*/
# TODO: https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file
resource "null_resource" "zip_lambda" {
depends_on = [null_resource.validate_python3, null_resource.run_tests]
provisioner "local-exec" {
command = "zip -j lambda_function_payload.zip lambda/*.py"
on_failure = fail # Fail if the command returns a non-zero exit code
}
triggers = {
file_exists = fileexists("lambda_function_payload.zip")
function_py_hash = filesha256("lambda/function.py")
calculator_py_hash = filesha256("lambda/calculator.py")
testcalc_py_hash = filesha256("lambda/test_calc.py")
}
}
resource "aws_lambda_function" "my_lambda" {
function_name = "calculator_lambda"
depends_on = [null_resource.zip_lambda]
handler = "function.lambda_handler"
runtime = "python3.11" # match in function.py
role = aws_iam_role.lambda_role.arn
filename = "lambda_function_payload.zip"
#source_code_hash = filesha256("lambda_function_payload.zip")
}
resource "null_resource" "test_lambda" {
depends_on = [aws_lambda_function.my_lambda]
provisioner "local-exec" {
command = "bash test_lambda.sh" # FIXME
on_failure = fail # Fail if the command returns a non-zero exit code
}
triggers = {
source_code_hash = filesha256("lambda/function.py")
calculator_py_hash = filesha256("lambda/calculator.py")
test_hash = filesha256("lambda/test_calc.py")
#always_run = "${timestamp()}"
}
}
resource "null_resource" "check_response" {
depends_on = [null_resource.test_lambda]
provisioner "local-exec" {
command = "jq '.body == \"330\"' response.json"
on_failure = fail # Fail if the command returns a non-zero exit code
}
triggers = {
file_sha256 = filesha256("response.json")
#always_run = "${timestamp()}"
}
}
resource "null_resource" "check_sam_response" {
depends_on = [null_resource.test_lambda]
provisioner "local-exec" {
command = "bash test_sam_local.sh"
on_failure = fail # Fail if the command returns a non-zero exit code
}
triggers = {
#file_sha256 = filesha256("sam_response.json")
always_run = "${timestamp()}" # FIXME
}
}