Origin Routing Lambda

Conflicting paths

  1. /some-path/some-page: origin 1 (web app)
  2. /some-path/other-page: origin 2 (CMS)

No logical pattern to differentiate paths

Requirements

  1. Business users can configure pages on origin 2 that display on origin 1 cache behaviours
  2. Code deployments are not required
  3. Ideally, business users do not have to explicitly/manually define which pages to display

In other words; origin 2 pages should only display if the page is not present in origin 1.

Some solutions?

Origin Routing Lambda solution

Code & Pipeline

  • Tightly coupled with CloudFront deployment
  • Same repo as CloudFront Infrastructure as Code (IaC)
  • L@E deployed just before CloudFront in pipeline

Latency overhead

r = requests.head(f"https://{hostname}{path}", headers=formatted_headers, params=params, timeout=1)

Cache

If you want the function to execute for every request that CloudFront receives for the distribution, use the viewer request or viewer response events. Origin request and origin response events occur only when a requested object isn’t cached in an edge location and CloudFront forwards a request to the origin.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-how-to-choose-event.html

Testing

  • Smoke test, HTTP request to endpoints and assert response is coming from correct origin
  • Unit tests, “given x response from origin, assert y behaviour”

Some exceptions

inheritance_pages = [
    "/my-page",
    "/my-page/nested-page",
    "/other-page",
]
if path in inheritance_pages:
    logging.info(f"Path {path} is included in list of pages used in CMS for inheritance purposes, leaving origin as-is.")
    return request

Troubleshooting L@E