【发布时间】:2019-04-19 08:21:56
【问题描述】:
我有一个自定义域 URL (my-custom-domain.com),并且 REST API 支持查询和路径参数。
https://my-custom-domain.com/hello
https://my-custom-domain.com?firstparam=abc&secondparam=def
调用的 lambda 必须返回响应,并将一些路径/查询参数附加到 json 正文中的自定义域 URL。基本上是其他可以访问的资源。
示例: https://my-custom-domain.com/hellofromlambda1123
https://my-custom-domain.com?firstparam=abc&secondparam=yourblogpage&pagenumber=30
一个理想的用例是分页,我必须提供上一个和下一个链接。如何将自定义域 URL 传递给我的 lambda。 我正在研究节点 js 8
在传统的 JAVA 编程中,我们可以通过 HttpServletRequest.getRequestURL() 来实现。 获取自定义域 URL 的方法是什么。我为 DefaultCacheBehavior 启用了标头。 lambda 事件中的主机提供 API 网关 URL。有没有办法在 lambda 中获取自定义域的映射?
我的自定义域的 Cloud Formation 模板如下所示
AWSTemplateFormatVersion: '2010-09-09'
Description: Custom domain template
Parameters:
ServiceName:
Description: Name of the Service
Type: String
DeploymentEnv:
Default: dev
Description: The environment this stack is being deployed to.
Type: String
CertificateId:
Description: SSL Certificate Id
Type: String
DomainName:
Description: Name of the custom domain
Type: String
HostedZoneId:
Description: Id of the hosted zone
Type: String
Resources:
APIDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName:
Fn::ImportValue:
!Sub "InvokeURL-${DeploymentEnv}"
Id: !Sub 'Custom-Domain-${DeploymentEnv}'
CustomOriginConfig:
OriginProtocolPolicy: https-only
OriginSSLProtocols: [TLSv1.2]
Enabled: 'true'
DefaultCacheBehavior:
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
DefaultTTL: 0
TargetOriginId: !Sub 'Custom-Domain-${DeploymentEnv}'
ForwardedValues:
QueryString: 'true'
Cookies:
Forward: none
Headers:
- 'Accept'
- 'api-version'
- 'Authorization'
ViewerProtocolPolicy: https-only
Aliases:
- !Sub '${DomainName}'
ViewerCertificate:
AcmCertificateArn: !Sub '${CertificateId}'
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2018
APIDNSRecord:
Type: AWS::Route53::RecordSet
DependsOn: "APIDistribution"
Properties:
HostedZoneId: !Sub '${HostedZoneId}'
Comment: DNS name for the custom distribution.
Name: !Sub '${DomainName}'
Type: A
AliasTarget:
DNSName: !GetAtt APIDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
EvaluateTargetHealth: false
Outputs:
DomainName:
Value: !GetAtt APIDistribution.DomainName
【问题讨论】:
-
所以这里的问题似乎是您正在创建自己的 CloudFront 分配,并将其源域名设置为 API 网关端点——并且,使用此配置,可以预期 API网关实际上并不知道自定义域名。虽然这是一种有效的配置,有时也是一种理想的配置,但您这样做是否有特定原因,而不是在 API Gateway 本身中配置自定义域名?最佳解决方案可能取决于您的推理。
-
是的@Michael-sqlbot 让云前端知道的意图是,如果我们可能需要给定 API 网关的多个路由,我们将在自定义域本身中配置它。此外,我们希望使用模板文件来运行所有云形成以实现自动化。
标签: node.js aws-lambda aws-api-gateway amazon-cloudfront