简体   繁体   English

cdk api 网关 route53 lambda 自定义域名不工作

[英]cdk api gateway route53 lambda custom domain name not working

Similar questions has been made but none of them were able to help me fix the issue that I'm facing.已经提出了类似的问题,但没有一个能够帮助我解决我面临的问题。 What I'm trying to do is to connect my api-gateway/lamnda function with a custom domain name and for some reason when calling the api/domain is not returning what I expected.我想要做的是将我的 api-gateway/lamnda function 与自定义域名连接起来,并且由于某种原因在调用 api/domain 时没有返回我预期的结果。

cdk version: 1.53.0

    const lambdaFunction = new lambda.Function(this, 'LambdaApi', {
      functionName: 'lambda-api',
      handler: 'lambda.handler',
      runtime: lambda.Runtime.NODEJS_12_X,
      code: new lambda.AssetCode(join(process.cwd(), '../api/dist')),
      memorySize: 128,
      timeout: cdk.Duration.seconds(5),
    })

    const zone = route53.HostedZone.fromLookup(scope, 'Zone', {
     'example.com',
     privateZone: false,
    })

    const certificate = certificatemanager.Certificate.fromCertificateArn(
     this,
     'Certificate',
     CERT_ARN,
    )

    const api = new apigateway.LambdaRestApi(this, 'LambdaApiGateway', {
      handler: lambdaFunction,
      proxy: true,
      endpointTypes: [apigateway.EndpointType.EDGE],
      defaultCorsPreflightOptions: {
        allowOrigins: apigateway.Cors.ALL_ORIGINS,
      },
      options: {
        restApiName: 'gateway-api',
        domainName: {
          domainName: 'api.example.com',
          certificate,
        },
        deployOptions: {
          stageName: 'prod',
          metricsEnabled: true,
          loggingLevel: apigateway.MethodLoggingLevel.INFO,
          dataTraceEnabled: true,
        },
      },
    })

    new route53.ARecord(this, 'CustomDomainAliasRecord', {
      zone,
      recordName: 'api',
      target: route53.RecordTarget.fromAlias(new targets.ApiGateway(api)),
    })

The deployment process works fine, a ARecord is created on route53 that is pointing to the api-gateway domain name, the api mappings is created as well pointing to prod as specified on stageName but when calling the domain name it doesn't work but when calling the api-gateway endpoint it does.部署过程工作正常,在指向 api-gateway 域名的 route53 上创建了一个 ARecord,创建了 api 映射并指向stageName上指定的prod但是在调用域名时它不起作用但是当调用它所做的 api-gateway 端点。

api.example.com/ping returns healthy api.example.com/ping返回healthy

{id}.execute-api.us-east-1.amazonaws.com/prod/ping returns the current date {id}.execute-api.us-east-1.amazonaws.com/prod/ping返回当前日期

Been researching but I'm not able to find out why the api.example.com/ping is not working一直在研究,但我无法找出api.example.com/ping无法正常工作的原因

For the most part we've done what you are doing there, but after the zone and certificate creation we've got something like this:在大多数情况下,我们已经完成了您在那里所做的事情,但是在区域和证书创建之后,我们得到了这样的东西:

const customDomain = new DomainName(this, 'customDomain', {
    domainName: 'api.example.com',
    certificate: certificate,
    endpointType: EndpointType.REGIONAL // yours may be Edge here
})

We also use basePathMapping so we don't have to use "dev|stg|prod" on the end of the domain.我们还使用 basePathMapping,因此我们不必在域的末尾使用“dev|stg|prod”。

new BasePathMapping(this, 'CustomBasePathMapping', {
    domainName: customDomain,
    restApi: api // again yours may differ here
})

I fixed with cloudfront distribution, here is the code.我修复了云端分发,这是代码。

const api = new apigateway.LambdaRestApi(
  this,
  'lambda-api-gateway',
  {
    handler: lambdaFunction,
    proxy: true,
    endpointTypes: [apigateway.EndpointType.EDGE],
    defaultCorsPreflightOptions: {
      allowOrigins: apigateway.Cors.ALL_ORIGINS,
      allowMethods: apigateway.Cors.ALL_METHODS,
    },
    options: {
      restApiName: 'gateway-api',
      domainName: {
        domainName,
        certificate,
      },
      deployOptions: {
        stageName: props.stageName,
        metricsEnabled: true,
        loggingLevel: apigateway.MethodLoggingLevel.INFO,
        dataTraceEnabled: true,
      },
    },
  },
)

const distribution = new cloudfront.CloudFrontWebDistribution(
  this,
  'api-cloudfront-distribution',
  {
    defaultRootObject: '/',
    originConfigs: [
      {
        customOriginSource: {
          domainName: `${api.restApiId}.execute-api.${this.region}.${this.urlSuffix}`,
        },
        originPath: `/${props.stageName}`,
        behaviors: [
          {
            allowedMethods: cloudfront.CloudFrontAllowedMethods.ALL,
            isDefaultBehavior: true,
            forwardedValues: {
              cookies: {
                forward: 'all',
              },
              queryString: true,
            },
          },
        ],
      },
    ],
    enableIpV6: true,
    viewerCertificate: cloudfront.ViewerCertificate.fromAcmCertificate(
      certificate,
      {
        aliases: [domainName],
        securityPolicy: cloudfront.SecurityPolicyProtocol.TLS_V1,
        sslMethod: cloudfront.SSLMethod.SNI,
      },
    ),
  },
)

const zone = zoneFromLookUp(this, props.zoneDomainName)
const target = route53.RecordTarget.fromAlias(
  new targets.CloudFrontTarget(distribution),
)

new route53.ARecord(this, 'arecord-api', {
  zone,
  recordName: domainName,
  target,
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用 Java AWS API 更新 Route53 记录? - Is it possible using the Java AWS API to update a Route53 record? 使用 CDK v2 的 AWS AppSync 自定义域名 - Custom domain name with AWS AppSync using CDK v2 我可以将 cloudfront 与 route53 流量策略一起使用吗? - Can I use cloudfront with a route53 traffic policy? 别名为 Cloudfront 分布的 Route53 DNS 在某些地区解析为 Loopback 地址 - Route53 DNS Aliased to Cloudfront distribution is resolving to Loopback address at some geographies 使用 AWS CloudFormation json 创建堆栈时 AWS::Route53::RecordSet 中出现无效请求错误 - Invalid request error in AWS::Route53::RecordSet when creating stack with AWS CloudFormation json 在 aws route 53 中设置多个子域以暴露 Kubernetes - Set up multiple sub domain in aws route 53 to expose Kubernetes 如何创建在 Python CDK 中引用自身的 API 网关资源策略? - How to create API Gateway Resource Policy that references itself in the Python CDK? AWS CDK:如何在 API 网关中查找/获取集成的“IntegrationId” - AWS CDK: How to find/fetch 'IntegrationId' of Integrations in API Gateway AWS API Gateway Lambda 集成(非代理)- 如何发送自定义标头和二进制数据 - AWS API Gateway Lambda Integration (NOT Proxy) - How to Send Custom Headers and Binary Data AWS API Gateway 忽略从自定义授权方 Lambda 函数返回的身份验证策略 - AWS API Gateway ignores auth policy returned from the Custom Authorizer Lambda Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM