
CloudFront - API Gateway Security: API Key Rotation
Ensuring Secure Communication Between CloudFront and API Gateway: Automated API Key Rotation using AWS Lambda.
- CloudFront manages client requests, passing them through to the API Gateway while including the API key in a custom header labeled "x-api-key." This key serves only as a "shared secret" between these two AWS services, establishing a secure and authenticated communication channel.
- An API Gateway set up to require an API key for each request, granting access solely through CloudFront and blocking direct internet access.
- A Lambda function that executes the API key rotation process.
- An EventBridge Scheduler sends required parameters and triggers the Lambda request monthly.
- Enhanced Security: Frequent API key rotation greatly minimizes vulnerability periods against potential compromises.
- Reduced Operational Load: Removes the necessity for manual rotations, allowing personnel to focus on more valuable tasks.
- Compliance and Audit: Aids in meeting security standards and maintains a clear log of rotation timings.
- Zero Downtime: The architecture guarantees a smooth key transition without any service disruptions.
- Check existing API keys: First, locate the specified usage plan to check if an associated API key already exists.
- Generate a new API key: Produce a new API key on the API Gateway using the provided name.
- Associate the key: Link the new key to the configured usage plan.
- Update the CloudFront distribution: Update the "x-api-key" header in the CloudFront origin configuration with the new key value.
- Remove the old key: After the CloudFront update is finalized, delete the old API key to maintain a clean environment.
1
2
3
4
5
6
{
"API_KEY_NAME": "kloudpepper-api-key",
"DISTRIBUTION_ID": "E1MTSB8DGIOBI2",
"ORIGIN_ID": "api-gateway-origin",
"USAGE_PLAN_ID": "0aj0l3"
}
- Terraform Code: This code is used to provision essential infrastructure components such as API Gateway, CloudFront, Lambda, and EventBridge Scheduler. It provides examples of creating these resources and the necessary configurations for their integration.
- Cross-account roles: To modify the relevant resources, the Lambda function must assume a role in the target account.
- Cross-account trust configuration: Establishing trust relationships between the IAM roles of both accounts is essential.
- Lambda location: It's essential to determine in which account to deploy the Lambda function—whether in the account where CloudFront resides or where API Gateway is available—given that it requires permissions in both accounts.