Skip to content

Commit 6daecf7

Browse files
Create initial release
1 parent 632746e commit 6daecf7

File tree

4,299 files changed

+1390164
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,299 files changed

+1390164
-2
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
1-
# terraform-certbot-lambda
2-
Terraform module for deploying an AWS Lambda that generates Let's Encrypt certificates via Certbot
1+
# Certbot Lambda
2+
3+
This module deploys an AWS Lambda function that generates Let's Encrypt certificates via *certbot*, for the given domains. The Lambda is triggered by a CloudWatch event rule whose schedule can be set through the 'function_trigger_schedule_expression' variable.
4+
5+
## Examples
6+
The following example will deploy a Lambda that will generate certificates for *test.example.com*:
7+
```
8+
module "certbot_lambda_test" {
9+
source = "../../"
10+
11+
# This is used for naming resources
12+
name = "test"
13+
14+
# This email used by Let's Encrypt for sending notifications about certificates
15+
contact_email = "admin@example.com"
16+
17+
# This is the domain for the certificate
18+
certificate_domains = "test.example.com"
19+
20+
# This zone will be automatically updated to meet the DNS challenge required by Let's Encrypt
21+
hosted_zone_id = aws_route53_record.example_com.zone_id
22+
23+
# This is a cron-like expressions that determines when the Lambda is triggered
24+
function_trigger_schedule_expression = "cron(12 20 * * ? *)"
25+
}
26+
```

bucket.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# This bucket will be used for storing certificates.
3+
#
4+
resource "aws_s3_bucket" "certificates_store" {
5+
bucket = "${var.name_prefix}-certificates-${var.name}"
6+
acl = "private"
7+
force_destroy = true
8+
9+
versioning {
10+
enabled = true
11+
}
12+
13+
lifecycle {
14+
prevent_destroy = false
15+
}
16+
17+
server_side_encryption_configuration {
18+
rule {
19+
apply_server_side_encryption_by_default {
20+
sse_algorithm = "AES256"
21+
}
22+
}
23+
}
24+
25+
tags = local.tags
26+
}

cloudwatch.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Create a timer that runs every 12 hours
2+
resource "aws_cloudwatch_event_rule" "certbot_lambda_timer" {
3+
name = "${var.name_prefix}-timer-${var.name}"
4+
schedule_expression = "cron(0 */12 * * ? *)"
5+
}
6+
7+
# Specify the lambda function to run
8+
resource "aws_cloudwatch_event_target" "lets_encrypt_timer_target" {
9+
rule = aws_cloudwatch_event_rule.certbot_lambda_timer.name
10+
arn = module.certbot_lambda_jenkins.function_arn
11+
}
12+
13+
# Give cloudwatch permission to invoke the function
14+
resource "aws_lambda_permission" "permission" {
15+
action = "lambda:InvokeFunction"
16+
function_name = module.certbot_lambda_jenkins.function_name
17+
principal = "events.amazonaws.com"
18+
source_arn = aws_cloudwatch_event_rule.certbot_lambda_timer.arn
19+
}

lambda.tf

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#
2+
# Lambda function that takes care of requesting the creation and renewal of
3+
# LetsEncrypt certificates and stores them in an S3 bucket.
4+
#
5+
module "certbot_lambda_jenkins" {
6+
source = "git::https://github.com/binbashar/terraform-aws-lambda?ref=master"
7+
8+
function_name = "${var.name_prefix}-${var.name}"
9+
description = "CertBot Lambda that creates and renews certificates for ${var.certificate_domains}"
10+
handler = "main.lambda_handler"
11+
runtime = "python3.6"
12+
timeout = 300
13+
14+
source_path = "${path.module}/src/"
15+
16+
trusted_entities = ["events.amazonaws.com"]
17+
18+
policy = {
19+
json = data.aws_iam_policy_document.bucket_permissions.json
20+
}
21+
22+
environment = {
23+
variables = {
24+
EMAIL = var.contact_email
25+
DOMAINS = var.certificate_domains
26+
S3_BUCKET = aws_s3_bucket.certificates_store.id
27+
S3_PREFIX = var.name
28+
}
29+
}
30+
}
31+
32+
#
33+
# Lambda permissions on the bucket used to store certificates.
34+
#
35+
data "aws_iam_policy_document" "bucket_permissions" {
36+
statement {
37+
actions = [
38+
"s3:ListBucket"
39+
]
40+
resources = [
41+
aws_s3_bucket.certificates_store.arn
42+
]
43+
}
44+
45+
statement {
46+
actions = [
47+
"s3:PutObject"
48+
]
49+
resources = [
50+
aws_s3_bucket.certificates_store.arn,
51+
"${aws_s3_bucket.certificates_store.arn}/*"
52+
]
53+
}
54+
55+
statement {
56+
actions = [
57+
"route53:ListHostedZones",
58+
"route53:GetChange"
59+
]
60+
resources = ["*"]
61+
}
62+
63+
statement {
64+
actions = [
65+
"route53:ChangeResourceRecordSets"
66+
]
67+
resources = [
68+
"arn:aws:route53:::hostedzone/${var.hosted_zone_id}"
69+
]
70+
}
71+
}

locals.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
tags = merge(var.tags, map("Application", "certbot-lambda"))
3+
}
45.5 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pip
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 bw2
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

0 commit comments

Comments
 (0)