Score:1

On AWS Cloudfront, I'd like to route /api/* requests to an external api

tr flag

I'm fairly new to Cloudfront ( AWS in general ), and looking set up a behaviour such that any requests that is for example

test.com/api/*

goes to an existing api that we also own, except it's on a seperate domain, e.g.

staging.test.com/api

Is this an allowed behaviour? I probably will need to allow for this the receiving API in CORS but just looking for how to set this up as a behaviour in Cloudfront.

I have tried using lambda functions along with the route

e.g.


    
    var response = {
        statusCode: 302,
        statusDescription: 'Found',
        headers:
            { "location": { "value": "https://staging.test.com.au/api/" + redirectPath } }
        }

    return response;

That doesn't seem to be working.

So was just wondering what is the correct way of going about this, I can only find references on using an API gateway instead which isn't what I want.

Score:0
id flag

You can do it by using origin and behavior. Define origin for and staging.test.com/api and add new behavior with path pattern as /api/*.

Details: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html?icmpid=docs_cf_help_panel#DownloadDistValuesPathPattern

Ben Perram avatar
tr flag
Heya thanks, we ended up solving this, basically the way you suggested but we had a load balancer that sat in front of our API so we ended up pointing it to that as our origin which worked.
Iren Saltalı avatar
id flag
Great, I'm glad it worked out for you. :)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.