AWS Serverless Replay Cache is a serverless implementation of cache solution for dynamic and static content using Lambda@Edge.
- AWS CLI installed and configured
- SAM CLI installed and configured
- CloudFront Distribution created and with Behavior(s) configured
git clone https://github.com/aws-samples/aws-serverless-replay-cache.git
cp conf/config.json content-manager/
cp conf/config.json origin-request/
cp conf/config.json origin-response/
aws s3 mb s3://<SAM_BUCKET_NAME>
sam deploy --stack-name replay-cache --s3-bucket <SAM_BUCKET_NAME> --capabilities CAPABILITY_NAMED_IAM --parameter-overrides BucketName=<CACHE_BUCKET_NAME>
- Open the CloudFront console at https://console.aws.amazon.com/cloudfront
- In the navigation panel select Distributions, and then click in the ID of the CloudFront Distribution to be configured
- Under Origins and Origin Groups tab, click on Create Origin button
- Origin Domain Name select the bucket specified in the parameter <CACHE_BUCKET_NAME> of SAM deployment
- Restrict Bucket Access select Yes
- Origin Access Identity select Create a New Identity
- Grant Read Permissions Bucket select Yes, Update Bucket Policy
- Click on Create button
- Under Origins and Origin Groups tab, click on Create Origin Group button
- Origins select the website Origin and click on Add button
- Origins select the S3 Cache Bucket Origin and click on Add button
- Failover criteria select all 5xx errors
- Origin Group ID enter ReplayCache-OriginGroup
- Click on Create button
- Under Behaviors tab, edit or create a new Behavior that will serve cached content in case of a failover
- Origin or Origin Group select ReplayCache-OriginGroup
- Cache and origin request settings select Use legacy cache setting
- Cache Based on Selected Request Headers select Whitelist
- Whitelist Headers input enter Host value and click on Add Custom >> button
- Whitelist Headers list CloudFront-Forwarded-Proto and click on Add >> button
- Click on Create or Yes, Edit button
- Open the Lambda console at https://console.aws.amazon.com/lambda
- In the navigation panel select Functions, and then open replay-cache-origin-request function
- Click on Action button and select Deploy to Lambda@Edge option
- Select the appropriated Distribution and Behavior
- CloudFront event select Origin request
- Check the acknowledge checkbox
- Click on Deploy button
- Open the Lambda console at https://console.aws.amazon.com/lambda
- In the navigation panel select Functions, and then open replay-cache-origin-response function
- Click on Action button and select Deploy to Lambda@Edge option
- Select the appropriated Distribution and Behavior
- CloudFront event select Origin response
- Check the acknowledge checkbox
- Click on Deploy button
- Open the DynamoDB console at https://console.aws.amazon.com/dynamodb
- In the navigation panel select Tables, and then open replay-cache table
- Under Global Tables tab, click on Add region
- Select the appropriated Region
- Click on Create replica button
- Remove Lambda@Edge function associations. Please note it may take a few hours to be completed.
- Delete the DynamoDB Global Table regions
- Empty S3 content bucket:
aws s3 rm s3://<CACHE_BUCKET_NAME> --recursive
- Delete the CloudFormation Stack
aws cloudformation delete-stack --stack-name replay-cache
- I can't see anything being cached.
It's likely you didn't created a DynamoDB Global Table replica in the region that Amazon CloudFront is serving you. In each region you'd like to trigger the caching process must have its own replica. Notice, by default the table is created in us-east-1 region.
You can additionaly confirm this in the Origin Response function logs at Amazon CloudWatch in the respective region.
This library is licensed under the MIT-0 License. See the LICENSE file.