forked from kingsoftgames/certbot-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
185 lines (156 loc) · 6.46 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
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
terraform {
# The configuration for this backend will be filled in by terragrunt
backend "s3" {}
required_version = ">= 0.15"
required_providers {
aws = ">= 3.48.0"
}
}
# ---------------------------------------------------------------------------------------------------------------------
# Data SOURCES
# ---------------------------------------------------------------------------------------------------------------------
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_arn" "current" {
arn = data.aws_caller_identity.current.arn
}
# ---------------------------------------------------------------------------------------------------------------------
# IAM ROLE AND POLICY
# ---------------------------------------------------------------------------------------------------------------------
data "aws_iam_policy_document" "route53" {
statement {
effect = "Allow"
actions = [
"route53:ListHostedZones",
"route53:GetChange",
]
resources = ["*"]
}
statement {
effect = "Allow"
actions = [
"route53:ChangeResourceRecordSets",
]
resources = ["arn:${local.aws_partition}:route53:::hostedzone/${var.hosted_zone_id}"]
}
}
data "aws_iam_policy_document" "s3" {
statement {
effect = "Allow"
actions = ["s3:ListBucket"]
resources = ["arn:${local.aws_partition}:s3:::${var.upload_s3.bucket}"]
}
statement {
effect = "Allow"
actions = ["s3:PutObject"]
resources = ["arn:${local.aws_partition}:s3:::${var.upload_s3.bucket}/${var.upload_s3.prefix}*"]
}
}
# Allow certbot to use dns challenges with route53
resource "aws_iam_role_policy" "route53" {
count = var.create_aws_route53_iam_role ? 1 : 0
name = "certbot-dns-route53"
role = aws_iam_role.lambda.id
policy = data.aws_iam_policy_document.route53.json
}
# Allow uploading generated certs to S3
resource "aws_iam_role_policy" "s3" {
count = var.create_aws_s3_iam_role ? 1 : 0
name = "certbot-upload-s3"
role = aws_iam_role.lambda.id
policy = data.aws_iam_policy_document.s3.json
}
# AWS managed policy, which provides write permissions to CloudWatch Logs.
resource "aws_iam_role_policy_attachment" "lambda_basic_execution" {
role = aws_iam_role.lambda.id
policy_arn = "arn:${local.aws_partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
data "aws_iam_policy_document" "lambda_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
# Note: both Global/China regions use "lambda.amazonaws.com" as service ID.
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "lambda" {
name_prefix = local.name_prefix
assume_role_policy = data.aws_iam_policy_document.lambda_role.json
# aws_iam_instance_profile.instance_profile in this module sets create_before_destroy to true, which means
# everything it depends on, including this resource, must set it as well, or you'll get cyclic dependency errors
# when you try to do a terraform destroy.
lifecycle {
create_before_destroy = true
}
}
# ---------------------------------------------------------------------------------------------------------------------
# LAMBDA
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_lambda_function" "certbot" {
function_name = var.lambda_name
role = aws_iam_role.lambda.arn
runtime = var.lambda_runtime
memory_size = var.lambda_memory_size
timeout = var.lambda_timeout
handler = local.lambda_handler
filename = local.lambda_filename
source_code_hash = local.lambda_hash
description = local.lambda_description
environment {
variables = local.lambda_environment
}
# Ignore changes in path that happen when different people apply. We have source_code_hash to track actual code changes.
lifecycle {
ignore_changes = [filename]
}
}
# ---------------------------------------------------------------------------------------------------------------------
# CLOUDWATCH EVENTS RULE THAT TRIGGERS ON A SCHEDULE
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_cloudwatch_event_rule" "schedule" {
name_prefix = local.name_prefix
description = "Triggers lambda function ${aws_lambda_function.certbot.function_name} on a regular schedule."
schedule_expression = "cron(${var.cron_expression})"
}
resource "aws_cloudwatch_event_target" "schedule" {
rule = aws_cloudwatch_event_rule.schedule.name
arn = aws_lambda_function.certbot.arn
# Since certbot lambda function gets all input from environment variables,
# an empty JSON object is good enough.
input = "{}"
}
resource "aws_lambda_permission" "schedule" {
source_arn = aws_cloudwatch_event_rule.schedule.arn
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.certbot.function_name
# Note: both Global/China regions use "events.amazonaws.com" as service ID of CloudWatch Events.
principal = "events.amazonaws.com"
}
# ---------------------------------------------------------------------------------------------------------------------
# LOCAL VARIABLES
# ---------------------------------------------------------------------------------------------------------------------
# Put locals to the end due to fmt issue with heredocs: https://github.com/hashicorp/terraform/issues/21434
locals {
name_prefix = "${var.lambda_name}-"
aws_region = data.aws_region.current.name
aws_partition = data.aws_arn.current.partition
aws_account_id = data.aws_caller_identity.current.account_id
certbot_version = "1.17.0"
certbot_emails = join(",", var.emails)
certbot_domains = join(",", var.domains)
lambda_handler = "main.lambda_handler"
lambda_filename = length(var.lambda_filename) > 0 ? var.lambda_filename : "${path.module}/../certbot/certbot-${local.certbot_version}.zip"
lambda_hash = filebase64sha256(local.lambda_filename)
lambda_description = var.lambda_description != "" ? var.lambda_description : "Run certbot for ${local.certbot_domains}"
lambda_environment = merge({
EMAILS = local.certbot_emails
DOMAINS = local.certbot_domains
DNS_PLUGIN = var.certbot_dns_plugin
S3_BUCKET = var.upload_s3.bucket
S3_PREFIX = var.upload_s3.prefix
S3_REGION = var.upload_s3.region
}, var.lambda_custom_environment)
}