Possible to Use SAM with multiple API Gateways in different template.yml with custom domains? #2703
-
I'm new to AWS SAM and really struggling with this. I'm trying to achieve the following. API Gateway hosting a custom domain (api.hostname.com) So it would look like this: Microservice Project 1 (separate template.yml) Microservice Project 2 (separate template.yml) The challenge that I run into is that in the first project, I create the custom domain in the first project and everything works correctly but the second project fails because the custom API domain has already been created by the first project. ApiGatewayApi: Is there anyone with a similar setup that might be able to help (or be able to share your template.yml)? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I think what I'm trying to do is to create my custom domain name on API Gateway outside of SAM (using CloudFormation), and then within my SAM app associate it to the pre-existing custom domain name (already defined in API Gateway) when I create an API Gateway instance using SAM. So instead of SAM creating the custom domain name, i'm trying to use SAM to associate it to the custom domain that was already defined. From the docs, I see a lot about creating custom domains using SAM but not sure how to associate it to a custom domain that already exists: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-domainname.html#aws-resource-apigateway-domainname--examples Any ideas how to do that? |
Beta Was this translation helpful? Give feedback.
-
I was able to achieve this; the key is the But let's assume you already have Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod If you want your microservice to also create the DNS record and associate it to your API Gateway, you can do this instead (but note, only do this in your top-level / root microservice...what would service the Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Domain:
DomainName: api.hostname.com
CertificateArn: <YOUR CERTIFICATE ARN>
Route53:
HostedZoneId: <YOUR HOSTED ZONE ID> Then create your lambda function and associate the Lambda's event to your API Gateway (using RestApiId): HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs18.x
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api
Properties:
Path: /
Method: get
RestApiId:
Ref: ApiGatewayApi Now here's the really important part. Create a new API Mapping associating the API Gateway to a path using the custom domain. This enables different paths to be routed to different API Gateways. Microservice01Mapping:
Type: 'AWS::ApiGateway::BasePathMapping'
Properties:
BasePath: 'path1'
DomainName: api.hostname.com
RestApiId: !Ref ApiGatewayApi
Stage: Prod This will result in an API that can be called at: You essentially want to take this same approach for your second microservice. The only real change you need to make is to change the In the end you can have two microservice projects, each with multiple lambdas, all using a single custom DNS name which looks like this: https://api.hostname.com/path1 Where Hope this helps. |
Beta Was this translation helpful? Give feedback.
I was able to achieve this; the key is the
AWS::ApiGateway::BasePathMapping
definition. You can either define your custom dns name (let's say api.hostname.com) manually using the Console or CloudFormation or it's better to define the DNS in your "root" SAM Lambda project because you can automatically associate your Route 53 DNS A record to API Gateway.But let's assume you already have
api.hostname.com
added as a custom domain in API Gateway. Set up two SAM projects, one for each of your microservices. Define an API Gateway in each:If you want your microservice to also create the DNS record a…