-
Notifications
You must be signed in to change notification settings - Fork 1
/
invalidate_cache.tf
101 lines (91 loc) · 3.14 KB
/
invalidate_cache.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Lambda defined here invalidates CloudFront cache in case the cached object
# in S3 is changed
resource "aws_iam_role" "invalidate_cloud_front_role" {
count = var.ssl_certificate_arn != "" ? 1 : 0
name = "invalidate_cloud_front"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "invalidate_cloud_front_policy" {
count = var.ssl_certificate_arn != "" ? 1 : 0
name = "invalidate_cloud_front_policy"
description = "Allow to create CloudFront invalidations"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"cloudfront:CreateInvalidation"
],
"Resource": [
"*"
]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "invalidate_cloud_front_attachment" {
count = var.ssl_certificate_arn != "" ? 1 : 0
role = aws_iam_role.invalidate_cloud_front_role[count.index].name
policy_arn = aws_iam_policy.invalidate_cloud_front_policy[count.index].arn
}
data "archive_file" "invalidate_cloud_front_lambda_zip" {
count = var.ssl_certificate_arn != "" ? 1 : 0
type = "zip"
source_file = "${path.module}/invalidate_cloud_front.py"
output_path = "${path.module}/invalidate_cloud_front_lambda.zip"
}
resource "aws_lambda_function" "invalidate_cloud_front_lambda" {
count = var.ssl_certificate_arn != "" ? 1 : 0
filename = data.archive_file.invalidate_cloud_front_lambda_zip[count.index].output_path
function_name = "inalidate_cloud_front_on_s3_change"
role = aws_iam_role.invalidate_cloud_front_role[count.index].arn
handler = "invalidate_cloud_front.handle_s3_change"
source_code_hash = data.archive_file.invalidate_cloud_front_lambda_zip[count.index].output_base64sha256
runtime = "python3.7"
environment {
variables = {
CLOUDFRONT_DISTRIBUTION_ID = aws_cloudfront_distribution.cdn[0].id
}
}
}
resource "aws_lambda_permission" "allow_s3_event_notifications" {
count = var.ssl_certificate_arn != "" ? 1 : 0
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.invalidate_cloud_front_lambda[count.index].function_name
principal = "s3.amazonaws.com"
source_arn = aws_s3_bucket.storage_bucket.arn
}
resource "aws_s3_bucket_notification" "bucket_notification" {
count = (var.ssl_certificate_arn != "" ? 1 : 0) * (var.invalidate_cloud_front_on_s3_update ? 1 : 0)
bucket = aws_s3_bucket.storage_bucket.id
lambda_function {
lambda_function_arn = aws_lambda_function.invalidate_cloud_front_lambda[count.index].arn
events = ["s3:ObjectCreated:*", "s3:ObjectRemoved:*"]
}
}