{"id":26584128,"url":"https://github.com/chrispsheehan/aws-subpath-api-hosting","last_synced_at":"2026-05-15T21:33:36.429Z","repository":{"id":255200151,"uuid":"846614106","full_name":"chrispsheehan/aws-subpath-api-hosting","owner":"chrispsheehan","description":"Host api at /api subpath to get around CORs issues. Handle private s3 redirects. PoC for A/B testing at speed.","archived":false,"fork":false,"pushed_at":"2025-07-26T12:56:11.000Z","size":545,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-26T18:40:50.967Z","etag":null,"topics":["api-gateway","aws","cloudfront","react","s3"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrispsheehan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-08-23T15:22:58.000Z","updated_at":"2025-07-26T12:56:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"7d4fe8a3-18e5-492b-8059-a125aad7df45","html_url":"https://github.com/chrispsheehan/aws-subpath-api-hosting","commit_stats":null,"previous_names":["chrispsheehan/aws-subpath-api-hosting"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chrispsheehan/aws-subpath-api-hosting","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrispsheehan%2Faws-subpath-api-hosting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrispsheehan%2Faws-subpath-api-hosting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrispsheehan%2Faws-subpath-api-hosting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrispsheehan%2Faws-subpath-api-hosting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrispsheehan","download_url":"https://codeload.github.com/chrispsheehan/aws-subpath-api-hosting/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrispsheehan%2Faws-subpath-api-hosting/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33080777,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-15T20:25:35.270Z","status":"ssl_error","status_checked_at":"2026-05-15T20:25:34.732Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-gateway","aws","cloudfront","react","s3"],"created_at":"2025-03-23T09:19:05.589Z","updated_at":"2026-05-15T21:33:36.424Z","avatar_url":"https://github.com/chrispsheehan.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# aws-subpath-api-hosting\n\nAccess an api via the same cloudfront distribution.\n\n*Problem 1*: Accessing [internal] APIs via domains can result in CORs issues and require mitigation.\n\n*Solution 1*: Access API via a path from the site making the request.\n\nThank you to Lewis for his guidance and [repo](https://github.com/Lewiscowles1986/aws-cors-cloudfront)\n\n*Problem 2*: When `/api` returns an error by default cloudfront will return its `custom_error_response`. A `custom_error_response` is required by react to catch refreshes direct navigation for non-public s3 buckets.\n\n*Solution 2*: Cloudfront function to add the `/index.html`.\n\n*Problem 1*: To A/B test the api served at `/api` with a static url and not having to update origins (which takes ages..).\n\n*Solution 2*: Cloudfront function to change the path of the request + cloudfront in front of target api with ordered cache behavior to direct to desired api. Updating a cloudfront function takes seconds.\n\n## access\n\n- obtain url from output as per below\n\n```sh\ncloudfront_url = \"https://d2t9lieeiaps7k.cloudfront.net\"\n```\n\n## infra\n\n![Infrastructure](./docs/infra.png)\n\n## private S3 bucket issues\n\nTo keep an S3 bucket private means that it cannot be in web hosting mode. This example only allows account cloudfront distributions to access the s3 bucket.\n\n**This creates an issue**. When directly navigating to a page and/or refreshing will result in a 404 as the `index.html` is not appended in the request. React SPAs handle routing on the client, so all routes must load index.html, which bootstraps the app and interprets the URL which will have a path of `/page_name?param=value`. S3 will fail as object `/page_name` will not exist.\n\nQuick/shonky workaround is to add a `custom_error_response` to return `\\index.html`\n\nA better solution can be seen in `resource \"aws_cloudfront_function\" \"handle_spa_routing\"`. This will append `/index.html `to those requests missing it.\n\nUrls which this function supports;\n- https://d3asn8cy0ghl7c.cloudfront.net/page1?this=that\n- https://d3asn8cy0ghl7c.cloudfront.net/page1\n- https://d3asn8cy0ghl7c.cloudfront.net\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrispsheehan%2Faws-subpath-api-hosting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrispsheehan%2Faws-subpath-api-hosting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrispsheehan%2Faws-subpath-api-hosting/lists"}