-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
81 lines (73 loc) · 1.88 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
76
77
78
79
80
81
resource "aws_iam_role" "lambda_role" {
name = var.role_name
tags = var.tags
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_iam_role_policy" "lambda_policy" {
name = var.policy_name
role = aws_iam_role.lambda_role.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ec2:DescribeInstances",
"ec2:StartInstances",
"ec2:StopInstances"
]
Resource = "*"
},
{
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "arn:aws:logs:*:*:*"
}
]
})
}
resource "null_resource" "download_binary" {
triggers = {
binary_url = var.binary_url
sha256 = var.binary_sha256
}
provisioner "local-exec" {
command = <<EOT
curl -L ${var.binary_url} -o ${path.module}/bootstrap && \
echo "${var.binary_sha256} ${path.module}/bootstrap" | sha256sum -c - && \
chmod +x ${path.module}/bootstrap
EOT
}
}
data "archive_file" "lambda_zip" {
type = "zip"
source_file = "${path.module}/bootstrap"
output_path = "${path.module}/function.zip"
depends_on = [null_resource.download_binary]
}
resource "aws_lambda_function" "ec2_uptime_maestro" {
filename = data.archive_file.lambda_zip.output_path
function_name = var.function_name
role = aws_iam_role.lambda_role.arn
handler = "main"
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
runtime = "provided.al2"
timeout = var.timeout
memory_size = var.memory_size
tags = var.tags
}