Serverless-application-model: Custom Domain Names?

Created on 15 Dec 2016  路  15Comments  路  Source: aws/serverless-application-model

Is there a way in SAM to define your Custom Domain Name for an API Gateway endpoint, as well as the base path for it?

areexamples contributorgood-first-issue typdocumentation

Most helpful comment

For me this works:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: subsidy_request_mailer

Parameters:
  DomainName:
    Type: String
    Default: api.example.com
  HostedZoneName:
    Type: String
    Default: example.com. # don't miss the  dot at the end

Resources:

  SendFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: send/
      Handler: app.lambda_handler
      Runtime: nodejs8.10
      Events:
        send:
          Type: Api
          Properties:
            Path: /send
            Method: get

  APIDomainName:
    Type: AWS::ApiGateway::DomainName
    Properties:
      CertificateArn: arn:aws:acm:us-east-1:...:certificate/...
      DomainName: !Ref DomainName

  APIBasePathMapping:
    Type: AWS::ApiGateway::BasePathMapping
    Properties:
      DomainName: !Ref APIDomainName
      RestApiId: !Ref ServerlessRestApi
      Stage: Prod

  APIDomain:
    Type: AWS::Route53::RecordSetGroup
    Properties:
      HostedZoneName: !Ref HostedZoneName
      RecordSets:
      - Name: !Ref DomainName
        Type: A
        AliasTarget:
          DNSName: !GetAtt APIDomainName.DistributionDomainName
          HostedZoneId: Z2FDTNDATAQYW2 # static ID for CloudFront aliases

All 15 comments

I am afraid not.. AFAIK CloudFormation hasn't supported APIGW custom domains yet. So SAM does not

@sanathkr thanks for the info. I'll just close this for the time being, then.

@frehner any update on this ?

I鈥檓 not working on this at all. You will want to ask one of the maintainers

@frehner thanks, sorry for the confusion.

@sanathkr any update on the ability to define a custom domain name in SAM or CloudFormation ?

For me this works:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: subsidy_request_mailer

Parameters:
  DomainName:
    Type: String
    Default: api.example.com
  HostedZoneName:
    Type: String
    Default: example.com. # don't miss the  dot at the end

Resources:

  SendFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: send/
      Handler: app.lambda_handler
      Runtime: nodejs8.10
      Events:
        send:
          Type: Api
          Properties:
            Path: /send
            Method: get

  APIDomainName:
    Type: AWS::ApiGateway::DomainName
    Properties:
      CertificateArn: arn:aws:acm:us-east-1:...:certificate/...
      DomainName: !Ref DomainName

  APIBasePathMapping:
    Type: AWS::ApiGateway::BasePathMapping
    Properties:
      DomainName: !Ref APIDomainName
      RestApiId: !Ref ServerlessRestApi
      Stage: Prod

  APIDomain:
    Type: AWS::Route53::RecordSetGroup
    Properties:
      HostedZoneName: !Ref HostedZoneName
      RecordSets:
      - Name: !Ref DomainName
        Type: A
        AliasTarget:
          DNSName: !GetAtt APIDomainName.DistributionDomainName
          HostedZoneId: Z2FDTNDATAQYW2 # static ID for CloudFront aliases

RFC for Custom Domain Names #783

I tried @timoschilling snippet, but I get Cannot import certificates for REGIONAL while EDGE is active and I cannot find anything on the internet with this error message.

Good question. Please any AWS Guru ?

The solution is to set

EndpointConfiguration:
  Types: 
    - REGIONAL

Solid example @timoschilling -- saved me a TON of time.

@timoschilling +1 saved me a tonne of time. Is there an example similar to this in the docs anywhere or would it be worth putting in a PR?

@timoschilling your example was the best example I could find all all of the interwebs. I was amazed that your example works. I am sure it saved me a ton of time over working out a workaround for SAM myself. AWS should definitely grab this into the documentation.

The documentation SAM, API Gateway, and github issues, none of them had working examples, just obfuscated discussions about SAM not supporting custom domains, and obscure stuff about EDGE or REGIONAL (behind manual CloudFront). I was amazed SAM didn't have support for custom domains out of the box. Made me wonder if I shouldn't switch from SAM to Serverless!

Some notes to help others who might want to following the same example:

(1) One key point is that the !Ref ServerlessRestApi in @timoschilling's example works because the AWS::Serverless-2016-10-31 transform creates a AWS::ApiGateway::RestApi resource that happens to have that name. That's a pretty brittle assumption, so if you have trouble, check the resources generated by the stack that it still has that name.

(2) Second, there is a tiny bug in @timoschilling's example, where is says:

  HostedZoneName:
    Type: String
    Default: example.com

The zone name has to be a rooted zone name ending in a '.', e.g. example.com.
Otherwise CloudFront will not find the domain.

(2) The CertificateArn: needs to be a us-east-1 certificate regardless of where you are deploying the stack. CloudFront gets all it certs from us-east-1. You might be able to use a regional cert if you switch to a REGIONAL as @deleugpn suggests but I haven't tested that myself.

@whereisaaron thanks for the feedback. I think it will be really valuable to add this example to the examples folder. Reopening this issue.

@praneetap can we close this issue now with #1144 merged?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

angustohrallegrinski picture angustohrallegrinski  路  3Comments

patrickli picture patrickli  路  4Comments

polovi picture polovi  路  3Comments

willdady picture willdady  路  3Comments

feinstein picture feinstein  路  3Comments