-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.tf
More file actions
218 lines (172 loc) · 4.85 KB
/
main.tf
File metadata and controls
218 lines (172 loc) · 4.85 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.47.0"
}
}
}
provider "aws" {
profile = var.profile
region = var.region
}
variable "lambda_function_name" {
default = "ami-updater"
type = string
description = "The name for the Lambda function. It will be used as prefix for roles, policies and log group."
}
variable "log_group_name" {
default = "/aws/lambda/ami-updater"
type = string
description = "The name of the log group in CloudWatch Logs."
}
variable "keep_amis" {
type = number
description = "Number of AMIs per Launch Template to be kept."
default = 3
}
variable "region" {
type = string
description = "The region where the Lambda will be configured."
}
variable "profile" {
type = string
description = "The AWS CLI profile to use. This is the AWS account to install the function."
}
data "aws_caller_identity" "current" {}
# Lambda assume policy
data "aws_iam_policy_document" "lambda_assume_role_policy" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
# Scheduler assume policy
data "aws_iam_policy_document" "scheduler_assume_role_policy" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["scheduler.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
# Lambda logging policy
data "aws_iam_policy_document" "lambda_logging_policy" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["arn:aws:logs:${var.region}:${data.aws_caller_identity.current.account_id}:log-group:${var.log_group_name}:*"]
}
}
# Lambda EC2 policy
data "aws_iam_policy_document" "lambda_ec2_policy" {
statement {
effect = "Allow"
actions = [
"ec2:DeregisterImage"
]
resources = ["arn:aws:ec2:${var.region}::image/*"]
}
statement {
effect = "Allow"
actions = [
"ec2:DescribeImages",
"ec2:DescribeLaunchTemplates",
"ec2:DescribeLaunchTemplateVersions"
]
resources = ["*"]
}
statement {
effect = "Allow"
actions = [
"ec2:ModifyLaunchTemplate",
"ec2:DeleteLaunchTemplateVersions",
"ec2:CreateLaunchTemplateVersion"
]
resources = ["arn:aws:ec2:${var.region}:${data.aws_caller_identity.current.account_id}:launch-template/*"]
}
statement {
effect = "Allow"
actions = [
"ec2:DeleteSnapshot"
]
resources = ["arn:aws:ec2:${var.region}::snapshot/*"]
}
}
# Scheduler Lambda invoke policy
data "aws_iam_policy_document" "scheduler_invoke_lambda_policy" {
statement {
effect = "Allow"
actions = [
"lambda:InvokeFunction"
]
resources = [aws_lambda_function.lambda_function.arn]
}
}
# Lambda role with assume policy and inline policies for logging and EC2 access
resource "aws_iam_role" "lambda_role" {
name = "${var.lambda_function_name}-lambda-role"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
inline_policy {
name = "logging"
policy = data.aws_iam_policy_document.lambda_logging_policy.json
}
inline_policy {
name = "launch-template-management"
policy = data.aws_iam_policy_document.lambda_ec2_policy.json
}
}
# Scheduler role with assume policy and inline policies for invoking Lambda function
resource "aws_iam_role" "scheduler_role" {
name = "${var.lambda_function_name}-scheduler-role"
assume_role_policy = data.aws_iam_policy_document.scheduler_assume_role_policy.json
inline_policy {
name = "invoke-lambda-function"
policy = data.aws_iam_policy_document.scheduler_invoke_lambda_policy.json
}
}
# Cloudwatch log group
resource "aws_cloudwatch_log_group" "cw_log_group" {
name = var.log_group_name
retention_in_days = 14
}
# Scheduler rule
resource "aws_scheduler_schedule" "scheduler_daily" {
name = "${var.lambda_function_name}-daily"
description = "Update the Launch Templates every 12 hours"
group_name = "default"
flexible_time_window {
mode = "OFF"
}
schedule_expression = "rate(12 hours)"
target {
arn = aws_lambda_function.lambda_function.arn
role_arn = aws_iam_role.scheduler_role.arn
}
}
# Lambda function
resource "aws_lambda_function" "lambda_function" {
filename = "package.zip"
function_name = var.lambda_function_name
role = aws_iam_role.lambda_role.arn
handler = "main.lambda_handler"
source_code_hash = fileexists("package.zip") ? filebase64sha256("package.zip") : null
runtime = "python3.13"
architectures = ["arm64"]
timeout = 60
publish = true
environment {
variables = {
KEEP_AMIS = var.keep_amis
}
}
}