Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services cdk api网关路由53 lambda自定义域名不工作_Amazon Web Services_Aws Lambda_Aws Api Gateway_Amazon Route53_Aws Cdk - Fatal编程技术网

Amazon web services cdk api网关路由53 lambda自定义域名不工作

Amazon web services cdk api网关路由53 lambda自定义域名不工作,amazon-web-services,aws-lambda,aws-api-gateway,amazon-route53,aws-cdk,Amazon Web Services,Aws Lambda,Aws Api Gateway,Amazon Route53,Aws Cdk,有人提出过类似的问题,但没有一个能帮助我解决我面临的问题。 我试图做的是将我的api gateway/lamnda函数与自定义域名连接起来,由于某种原因,调用api/domain时没有返回我所期望的结果 cdk版本:1.53.0 const lambdaFunction = new lambda.Function(this, 'LambdaApi', { functionName: 'lambda-api', handler: 'lambda.handler',

有人提出过类似的问题,但没有一个能帮助我解决我面临的问题。 我试图做的是将我的api gateway/lamnda函数与自定义域名连接起来,由于某种原因,调用api/domain时没有返回我所期望的结果

cdk版本: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)),
    })
部署过程工作正常,在路由器53上创建了一条指向api网关域名的记录,并按照
stageName
中的指定创建了指向
prod
的api映射,但在调用域名时它不起作用,但在调用api网关端点时它起作用

api.example.com/ping
返回
health

{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 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
})

我修复了cloudfront发行版,下面是代码

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,
})

我修复了cloudfront发行版,下面是代码

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,
})