-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.tf
102 lines (91 loc) · 3.21 KB
/
lambda.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
102
# ------------------------------------------------------------------------------------------
# Lambda function to invalidate CloudFront distributions
data "aws_iam_policy_document" "policy_for_cloudfront_invalidation_lambda" {
policy_id = "policy_for_cloudfront_invalidation_lambda"
statement {
actions = [
"cloudfront:CreateInvalidation",
]
resources = [
"arn:aws:cloudfront::${data.aws_caller_identity.current.account_id}:distribution/*",
]
sid = "1"
}
statement {
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
]
resources = [
"arn:aws:logs:*:${data.aws_caller_identity.current.account_id}:log-group:*",
]
sid = "2"
}
statement {
actions = [
"logs:PutLogEvents",
]
resources = [
"arn:aws:logs:*:${data.aws_caller_identity.current.account_id}:log-group:*:log-stream:*",
]
sid = "3"
}
statement {
actions = [
"s3:GetBucketTagging",
]
resources = [
"arn:aws:s3:::*",
]
sid = "4"
}
}
resource "aws_iam_policy" "policy_for_cloudfront_invalidation_lambda" {
name = "policy_for_cloudfront_invalidation_lambda"
policy = data.aws_iam_policy_document.policy_for_cloudfront_invalidation_lambda.json
}
data "aws_iam_policy_document" "policy_for_cloudfront_invalidation_lambda_assume_role_policy" {
policy_id = "policy_for_cloudfront_invalidation_lambda_assume_role_policy"
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = [
"lambda.amazonaws.com",
]
}
sid = "1"
}
}
resource "aws_iam_role" "policy_for_cloudfront_invalidation_lambda" {
name = "policy_for_cloudfront_invalidation_lambda"
assume_role_policy = data.aws_iam_policy_document.policy_for_cloudfront_invalidation_lambda_assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "policy_for_cloudfront_invalidation_lambda" {
role = aws_iam_role.policy_for_cloudfront_invalidation_lambda.name
policy_arn = aws_iam_policy.policy_for_cloudfront_invalidation_lambda.arn
}
data "archive_file" "cloudfront_invalidation_lambda_archive_file" {
type = "zip"
source_dir = "lambda_functions/lambda_invalidate_cloudfront"
output_path = "lambda_functions/lambda_invalidate_cloudfront.zip"
}
resource "null_resource" "cloudfront_invalidation_lambda_archive_file" {
triggers = {
hash = data.archive_file.cloudfront_invalidation_lambda_archive_file.output_base64sha256,
}
}
resource "aws_lambda_function" "cloudfront_invalidation_lambda" {
filename = "lambda_functions/lambda_invalidate_cloudfront.zip"
function_name = "cloudfront_invalidation_lambda"
handler = "lambda_invalidate_cloudfront.lambda_handler"
publish = true
runtime = "python3.9"
role = aws_iam_role.policy_for_cloudfront_invalidation_lambda.arn
source_code_hash = data.archive_file.cloudfront_invalidation_lambda_archive_file.output_base64sha256
}
resource "aws_lambda_alias" "cloudfront_invalidation_lambda_latest_alias" {
name = "cloudfront_invalidation_lambda_latest_alias"
function_name = aws_lambda_function.cloudfront_invalidation_lambda.function_name
function_version = "$LATEST"
}