Custom domain name for both cloudfront and lambda?


#1

Hi.

Im setting up an application in aws and I have added a custom domain name for my lambda functions. Im from a spring boot world and my first thought was that if I run a solution on www.mydomain.com then my website should be reached by www.mydomain.com and my api gateway should use www.mydomain.com/apii. But when I try to distribute my webpage with cloudfront Im not allowed to use the custom domain name since its already used for my api gateway.

Am I just doing it wrong or should I think differently. I mean should my cloudfront and lambda run on different custom domains? but then Ill have to allow cors right?

Looking forward to hear from you guys.

Best regards.
Obiwan =)


#2

I found a nice guide for this

In case somebody else needs it =)

But there is alot of configuration to do here in cloudfront. I hope somehow I can copy that when I want to move from dev to production.


#3

You should be doing your CloudFront configuration in CloudFormation.

You can do CloudFormation in your serverless.yml (under the resources property), but I probably wouldn’t do it in the same serverless.yml as your application - Your CloudFront distribution will rarely change, so it’s best to keep it separate (i.e. in a resource-only service).


#4

Thx for the answer rowanu. Ill look into cloudformation.

Regarding my original question I found out that I dont really need a domain pointing to my apigateway. I used serverless.yml files to configure the api. But the downside of this is that all my functions got their own tiny api’s. I have moved away from that and now I just use serverless to upload the function. Then I setup the api manually in the aws console. That way all the functions can be under one common api and the basic url doesnt change. That way I can have a dev url and a prod url in my frontend and I dont have to update it all the time.


#5

@ObiWan it seems like bad practice to manually click around Amazon’s GUIs. You should really manage your infrastructure from cloudformation, probably within serverless.yml. Otherwise it would be hard for other developers to work on your infrastructure, and it would be hard to run automated tests in your continuous integration pipelines (as they cannot easily spawn a new instance of your setup).


#6

I completely agree. But if I create all my functions in one serverless project, wont I get one big “function” which will take longer time to “wake” from hibernation when it runs? I have splittet all my functions into different projects and that means that I get an api for each if I configure it in the projects. Am I with serverless able to configure the apis for functions which is not in the current project. If so that would be neat, then I could have an api project and still have lots of small functions.