|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import boto3 |
| 6 | +import certbot.main |
| 7 | + |
| 8 | +# Let’s Encrypt acme-v02 server that supports wildcard certificates |
| 9 | +CERTBOT_SERVER = 'https://acme-v02.api.letsencrypt.org/directory' |
| 10 | + |
| 11 | +# Temp dir of Lambda runtime |
| 12 | +CERTBOT_DIR = '/tmp/certbot' |
| 13 | + |
| 14 | + |
| 15 | +def rm_tmp_dir(): |
| 16 | + if os.path.exists(CERTBOT_DIR): |
| 17 | + try: |
| 18 | + shutil.rmtree(CERTBOT_DIR) |
| 19 | + except NotADirectoryError: |
| 20 | + os.remove(CERTBOT_DIR) |
| 21 | + |
| 22 | + |
| 23 | +def obtain_certs(email, domains): |
| 24 | + certbot_args = [ |
| 25 | + # Override directory paths so script doesn't have to be run as root |
| 26 | + '--config-dir', CERTBOT_DIR, |
| 27 | + '--work-dir', CERTBOT_DIR, |
| 28 | + '--logs-dir', CERTBOT_DIR, |
| 29 | + |
| 30 | + # Obtain a cert but don't install it |
| 31 | + 'certonly', |
| 32 | + |
| 33 | + # Run in non-interactive mode |
| 34 | + '--non-interactive', |
| 35 | + |
| 36 | + # Agree to the terms of service |
| 37 | + '--agree-tos', |
| 38 | + |
| 39 | + # Email of domain administrator |
| 40 | + '--email', email, |
| 41 | + |
| 42 | + # Use dns challenge with route53 |
| 43 | + '--dns-route53', |
| 44 | + '--preferred-challenges', 'dns-01', |
| 45 | + |
| 46 | + # Use this server instead of default acme-v01 |
| 47 | + '--server', CERTBOT_SERVER, |
| 48 | + |
| 49 | + # Domains to provision certs for (comma separated) |
| 50 | + '--domains', domains, |
| 51 | + ] |
| 52 | + return certbot.main.main(certbot_args) |
| 53 | + |
| 54 | + |
| 55 | +# /tmp/certbot |
| 56 | +# ├── live |
| 57 | +# │ └── [domain] |
| 58 | +# │ ├── README |
| 59 | +# │ ├── cert.pem |
| 60 | +# │ ├── chain.pem |
| 61 | +# │ ├── fullchain.pem |
| 62 | +# │ └── privkey.pem |
| 63 | +def upload_certs(s3_bucket, s3_prefix): |
| 64 | + client = boto3.client('s3') |
| 65 | + cert_dir = os.path.join(CERTBOT_DIR, 'live') |
| 66 | + for dirpath, _dirnames, filenames in os.walk(cert_dir): |
| 67 | + for filename in filenames: |
| 68 | + local_path = os.path.join(dirpath, filename) |
| 69 | + relative_path = os.path.relpath(local_path, cert_dir) |
| 70 | + s3_key = os.path.join(s3_prefix, relative_path) |
| 71 | + print(f'Uploading: {local_path} => s3://{s3_bucket}/{s3_key}') |
| 72 | + client.upload_file(local_path, s3_bucket, s3_key) |
| 73 | + |
| 74 | + |
| 75 | +def guarded_handler(event, context): |
| 76 | + # Input parameters |
| 77 | + email = event['email'] |
| 78 | + domains = event['domains'] |
| 79 | + s3_bucket = event['s3_bucket'] # The S3 bucket to publish certificates |
| 80 | + s3_prefix = event['s3_prefix'] # The S3 key prefix to publish certificates |
| 81 | + |
| 82 | + obtain_certs(email, domains) |
| 83 | + upload_certs(s3_bucket, s3_prefix) |
| 84 | + |
| 85 | + return 'Certificates obtained and uploaded successfully.' |
| 86 | + |
| 87 | + |
| 88 | +def lambda_handler(event, context): |
| 89 | + try: |
| 90 | + rm_tmp_dir() |
| 91 | + return guarded_handler(event, context) |
| 92 | + finally: |
| 93 | + rm_tmp_dir() |
| 94 | + |
0 commit comments