繁体   English   中英

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

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

已经提出了类似的问题,但没有一个能够帮助我解决我面临的问题。 我想要做的是将我的 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)),
    })

部署过程工作正常,在指向 api-gateway 域名的 route53 上创建了一个 ARecord,创建了 api 映射并指向stageName上指定的prod但是在调用域名时它不起作用但是当调用它所做的 api-gateway 端点。

api.example.com/ping返回healthy

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

一直在研究,但我无法找出api.example.com/ping无法正常工作的原因

在大多数情况下,我们已经完成了您在那里所做的事情,但是在区域和证书创建之后,我们得到了这样的东西:

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

我们还使用 basePathMapping,因此我们不必在域的末尾使用“dev|stg|prod”。

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

我修复了云端分发,这是代码。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM