可以使用AWS SAM的API Gateway来实现将两个域名映射到同一个函数的目的。在API Gateway上创建一个REST API,并添加两个域名作为API的自定义域名。然后在API Gateway上创建Lambda集成,将其关联到相应的Lambda函数。
以下是一个示例AWS SAM模板,它使用API Gateway将两个域名映射到同一个Lambda函数:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
ApiGatewayApi:
Type: 'AWS::Serverless::Api'
Properties:
StageName: Prod
DefinitionBody:
swagger: '2.0'
info:
version: '1.0.0'
title: My API
schemes:
- https
paths:
/myresource:
get:
x-amazon-apigateway-integration:
type: aws
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations
httpMethod: POST
credentials: !GetAtt ApiGatewayRole.Arn
responses: {}
produces:
- application/json
500:
description: 'Server Error'
ApiGatewayDeployment:
Type: 'AWS::Serverless::Api'
DependsOn: ApiGatewayApi
Properties:
StageName: Prod
ApiGatewayDomainName1:
Type: 'AWS::ApiGateway::DomainName'
Properties:
DomainName: 'example.com'
CertificateArn: 'arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
ApiGatewayDomainName2:
Type: 'AWS::ApiGateway::DomainName'
Properties:
DomainName: 'sub.example.com'
CertificateArn: 'arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'