krz/aws-summary-report
Automated AWS summary reports, straight to your inbox.
clone: git clone https://gitbay.org/krz/aws-summary-report.git
1# acm.py
2import boto3
3from datetime import datetime, timedelta, timezone
4from tabulate import tabulate
5
6
7def get_section(config):
8 profile = config["aws"].get("profile")
9 region = config["aws"]["region"]
10 session = boto3.Session(
11 profile_name=profile if profile else None, region_name=region
12 )
13 client = session.client("acm")
14
15 today = datetime.now(timezone.utc)
16 deadline = today + timedelta(days=30)
17
18 certs = client.list_certificates(CertificateStatuses=["ISSUED"])[
19 "CertificateSummaryList"
20 ]
21 rows = []
22
23 for cert in certs:
24 detail = client.describe_certificate(CertificateArn=cert["CertificateArn"])[
25 "Certificate"
26 ]
27 not_after = detail.get("NotAfter")
28 if not_after and today <= not_after <= deadline:
29 rows.append([cert["DomainName"], not_after.strftime("%Y-%m-%d")])
30
31 if not rows:
32 return "Expiring TLS Certificates:\nNo certs expiring in the next 30 days."
33
34 table = tabulate(rows, headers=["Domain", "Expires"], tablefmt="simple_grid")
35 lines = [
36 "Expiring TLS Certificates (Next 30 Days):",
37 f"[https://{config['aws'].get('region')}.console.aws.amazon.com/acm/home#/certificates/list]",
38 table,
39 ]
40
41 return "\n".join(lines)