Search code examples
amazon-web-servicesaws-lambdaamazon-route53aws-sam

AWS SAM API with custom domain


I migrated a Jersey API to Cloudformation (using AWS SAM) and it is working properly with amazonaws domain... accesible like https://xxxxx.execute-api.us-east-1.amazonaws.com/Stage/endpoint

Now I need to be able to access it with my own domain, like https://api.mydomain.com/endpoint.

I configured a Certificate in Certificate Manager (api.mydomain.com) and added the CNAME record to my route53 hosted zone. Then, I just added this section to my YML file:

  Api:
    EndpointConfiguration: REGIONAL
    Domain:
      DomainName: api.mydomain.com
      CertificateArn: arn:aws:acm:us-east-1:XXXXXXXX:certificate/bbbbbb-bbbb-bbbb-bbbb-bbbbbbbb

This is my YML with that section:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Serverless Jersey API - com.mydomain::mydomain
Globals:
  Function:
    Environment:
      Variables:
        •••
  Api:
    EndpointConfiguration: REGIONAL
    Domain:
      DomainName: api.mydomain.com
      CertificateArn: arn:aws:acm:us-east-1:XXXXXXXX:certificate/bbbbbb-bbbb-bbbb-bbbb-bbbbbbbb
  
Resources:
  MyServiceFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: com.mydomain.app.StreamLambdaHandler::handleRequest
      Runtime: java8
      CodeUri: .
      MemorySize: 512
      Policies: AWSLambdaBasicExecutionRole
      Timeout: 30
      Events:
        ProxyResource:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: any
      Policies:
        •••

Outputs:
  MyServiceApi:
    Description: URL for application
    Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Stage/endpoint'
    Export:
      Name: MyServiceApi

It is just not working with my custom domain. The requested URL can't be reached. Please help :(


Solution

  • Ok, finally I solved the problem with these steps

    1. Create a Certificate in Certificate Manager with the url needed: api.mydomain.com
    2. Add the created DomainName and CertificateArn in the yml file
    3. Go to API Gateway->Custom Domain Names and select the domain. Then, from configurations copy the API Gateway Domain Name
    4. Go to Route53, and add an A record with the domain api.mydomain.com with value of the API Gateway Domain Name