1/16/2025 • VirtueCloud

At VirtueCloud , we’ve observed that managing certificate rotations is a common challenge many DevOps teams face. During our consultations with clients, it became clear that automating certificate expiration alerts is a crucial yet often overlooked aspect of day-to-day operations.
To address this, we propose an efficient way to streamline the process using AWS services, helping clients avoid potential disruptions and downtime. Although AWS Certificate Manager (ACM) with Private Certificate Authority (PCA) and KMS is great for signing certificates, a more straightforward approach using AWS Secrets Manager is sometimes a better fit.
Here’s a breakdown of how we did it:
Prerequisites
- AWS account
- Basic knowledge of AWS services
- AWS CLI configured (optional)
The Problem
We decided to store our certificates in AWS Secrets Manager. The challenge that followed was how to track the expiration dates and rotate certificates before they expired.
Manual tracking of expiration dates is:
- Error-prone
- Easy to forget
- Time-consuming
- Stressful (nobody wants an expired certificate in production!)
The Solution
We set up a simple, automated system using three key AWS services:
- EventBridge for scheduling
- Lambda for checking expiration dates
- SNS for sending alerts
How It Works
- Lambda periodically inspects secrets stored in Secrets Manager.
- EventBridge triggers the inspection based on a schedule.
- SNS sends alerts to the team when a certificate is nearing expiration.
Here’s a basic Lambda function to inspect certificates and send alerts:
import json
import boto3
from datetime import datetime
import OpenSSL.crypto
def lambda_handler(event, context):
secretsmanager = boto3.client('secretsmanager')
sns = boto3.client('sns')
try:
# Get list of secrets
secrets = secretsmanager.list_secrets()
for secret in secrets['SecretList']:
# Get the secret value
secret_value = secretsmanager.get_secret_value(SecretId=secret['Name'])
secret_dict = json.loads(secret_value['SecretString'])
# Look for certificate entries (ending in .cert)
for key, value in secret_dict.items():
if key.endswith('.cert'):
# Parse the certificate
cert_data = value.replace('\\n', '\n')
cert = OpenSSL.crypto.load_certificate(
OpenSSL.crypto.FILETYPE_PEM,
cert_data
)
# Get expiration date
expiry = datetime.strptime(
cert.get_notAfter().decode('ascii'),
'%Y%m%d%H%M%SZ'
)
# Calculate days until expiration
days_until_expiry = (expiry - datetime.utcnow()).days
# Alert if expiring within 60 days
if days_until_expiry <= 60:
sns.publish(
TopicArn='arn:aws:sns:REGION:ACCOUNT_ID:secret-rotation-alerts',
Subject=f'Certificate Expiration Alert - {key}',
Message=f'''
Certificate {key} in secret {secret["Name"]} is expiring soon!
Expiration Date: {expiry.date()}
Days until expiration: {days_until_expiry}
Please rotate this certificate soon.
'''
)
return {
'statusCode': 200,
'body': 'Certificate Inspection completed successfully'
}
except Exception as e:
print(f"Error: {str(e)}")
return {
'statusCode': 500,
'body': f'Error checking certificates: {str(e)}'
}
Setting It Up
Step 1: Create an IAM Role for Lambda
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"secretsmanager:ListSecrets",
"secretsmanager:DescribeSecret"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"sns:Publish"
],
"Resource": "arn:aws:sns:REGION:ACCOUNT_ID:secret-rotation-alerts"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
} Step 2: Create an SNS Topic
aws sns create-topic --name secret-rotation-alerts
aws sns subscribe --topic-arn <your-topic-arn> --protocol email --notification-endpoint your@email.comThen confirm the subscription via email.
Step 3: Create Lambda Function
Step 4: Create EventBridge Rule
1. Go to the EventBridge Console and click Rules > Create rule.

2. Configure the rule to run quarterly:
- Name: inspect-secret-quarterly
- Description: “Inspect secrets every quarter for rotation”
- Rule type: Schedule
- Cron expression: 0 9 1 1,4,7,10 ? *

3. Set the Lambda function as the target for this rule

Why Every 3 Months?
Most certificates are valid for one year, so quarterly inspections offer ample time for renewal and avoid unnecessary Lambda executions. This frequency helps catch any missed rotations.
Step 5: Test the Setup
1. Go to the Lambda Console.
2. Select your function and click the Test tab.
3. Create a test event and execute the function.
4. Check CloudWatch logs for execution results and your email for any alerts.
You can easily modify the Lambda function to inspect specific secrets using tags or adjust the alert threshold from 60 days to another time frame. For more flexible alerts, consider integrating with Slack or Teams via SNS.
This setup is very cost-effective, with minimal Lambda executions, only a few SNS notifications, and low CloudWatch logging costs. In most cases, the entire solution costs under a dollar per month.
While this blog post demonstrates a simple solution using Secrets Manager, AWS offers other approaches for certificate management, such as ACM Private Certificate Authority (PCA) with KMS. However, ACM PCA comes with different cost implications:
Given these factors, the Secrets Manager solution is ideal when you need:
In contrast, ACM PCA is better suited for enterprise environments that require:
Ultimately, the right solution depends on your specific requirements and the scale of your operations. Select the method that aligns with your goals, whether it’s Secrets Manager for simplicity and cost-efficiency or a more advanced setup like ACM PCA for larger, more complex environments.
If you're interested in automating the entire rotation process, AWS Secrets Manager offers a built-in rotation feature for certain types of secrets. For custom secrets, you could extend this setup by creating an additional Lambda function that:
However, implementing full secret automation is a more advanced topic — one we’ll cover in a future post!
Automating secret rotation tracking reduces human error and saves significant time. By leveraging AWS services like Lambda, EventBridge, and SNS, you can create a robust and low-cost solution to ensure certificate rotation happens on time without manual intervention.
We hope this guide helps others streamline their secret management process! Feel free to reach out to us at connect@virtuecloud.io you have any questions or suggestions for enhancing this solution.
Related articles you might find interesting

Why platform teams are re-routing north-south traffic through the Kubernetes Gateway API, what HTTPRoute changes on the ground, and how to migrate without a big-bang rewrite.


Hassle-Free ECS: Terraform Automation + CI/CD Pipeline