forked from funyholic/aws-cloudfront-auto-invalidator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
83 lines (71 loc) · 1.96 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
82
83
data "archive_file" "lambda" {
type = "zip"
output_path = "${path.module}/tmp/lambda.zip"
source_file = "${path.module}/src/app.py"
}
data "aws_iam_policy_document" "assume" {
statement {
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "this" {
name_prefix = "${var.name}-role-"
assume_role_policy = data.aws_iam_policy_document.assume.json
tags = merge(
var.tags,
{
"Name" = "${var.name}-role"
}
)
}
resource "aws_iam_role_policy_attachment" "basic_execution" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "cloudfront_read" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/CloudFrontReadOnlyAccess"
}
data "aws_iam_policy_document" "cloudfront_invalidations" {
statement {
actions = [
"cloudfront:CreateInvalidation"
]
resources = [
"*"
]
}
}
resource "aws_iam_role_policy" "cloudfront_invalidations" {
name_prefix = "cloudfront-invalidations-"
policy = data.aws_iam_policy_document.cloudfront_invalidations.json
role = aws_iam_role.this.name
}
resource "aws_lambda_function" "this" {
filename = data.archive_file.lambda.output_path
function_name = var.name
handler = "app.lambda_handler"
memory_size = var.memory_size
role = aws_iam_role.this.arn
runtime = var.runtime
source_code_hash = data.archive_file.lambda.output_base64sha256
timeout = var.timeout
tags = merge(
var.tags,
{
"Name" = "${var.name}"
}
)
}
resource "aws_lambda_permission" "this" {
statement_id = "AllowExecutionFromS3Bucket"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.this.arn
principal = "s3.amazonaws.com"
}