gpt4 book ai didi

amazon-web-services - 如何在 CDK 中为基于 Lambda 的 APIGateway 提供自定义域名?

转载 作者:行者123 更新时间:2023-12-04 08:10:31 29 4
gpt4 key购买 nike

对于这个问题,假设我已经有一个 example.org Route53 中的托管区域(我的实际区域当然不同)
使用以下 CDK 应用程序:

export class MyExampleStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const backend = new Function(this, 'backendLambda', {
code: new AssetCode("lambda/"),
handler: "index.handler",
runtime: Runtime.PYTHON_3_8
});

apiDomainName = 'api.test.example.org'
const api = new LambdaRestApi(this, 'api', {
handler: backend,
proxy: true,
deploy: true,
domainName: {
domainName: apiDomainName,
certificate: new Certificate(this, 'apiCertificate', {
domainName: apiDomainName
})
}
});

}
}
,当我运行 cdk deploy ,部分输出内容如下:
Outputs:
MyExampleStack.apiEndpoint0F54D2EA = https://<alphanumericId>.execute-api.us-east-1.amazonaws.com/prod/
,事实上,当我 curl那个 url,我看到了我期望从我的 Lambda 代码中得到的响应。我希望 curl学习 api.test.example.org给出相同的结果 - 但是,它给出了 curl: (6) Could not resolve host: api.test.example.org .
基于 this documentation , 我试过:
export class MyExampleStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const rootDomain = 'example.org'

const zone = HostedZone.fromLookup(this, 'baseZone', {
domainName: rootDomain
});

const backend = new Function(...);

const api = new LambdaRestApi(...);
new ARecord(this, 'apiDNS', {
zone: zone,
recordName: 'api.test',
target: RecordTarget.fromAlias(new ApiGateway(api))
});

}
}
这确实给出了一个 Route53 条目:
$ aws route53 list-hosted-zones
{
"HostedZones": [
{
"Id": "/hostedzone/ZO3B2N6W70PDD",
"Name": "example.org.",
"CallerReference": "598D71AB-4A98-EC5A-A170-D51CB243A2EA",
"Config": {
"PrivateZone": false
},
"ResourceRecordSetCount": 8
}
]
}
$ aws route53 list-resource-record-sets --hosted-zone-id /hostedzone/ZO3B2N6W70PDD --query 'ResourceRecordSets[?Name==`api.test.example.org.`]'
[
{
"Name": "api.test.example.org.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "Z1UJRXOUMOOFQ9",
"DNSName": "<alphanumericId2>.execute-api.us-east-1.amazonaws.com.",
"EvaluateTargetHealth": false
}
}
]
但这仍然不起作用:
  • curl api.test.example.org仍然给出“无法解析主机”
  • curl <alphanumericId2>.execute-api.us-east-1.amazonaws.comcurl: (7) Failed to connect to <alphanumericId2>.execute-api.us-east-1.amazonaws.com port 80: Connection refused
  • curl https://<alphanumericId2>..execute-api.us-east-1.amazonaws.com{"message":"Forbidden"}
  • curl https://<alphanumericId>.[...] (即 cdk deploy 的输出)仍然给出来自 Lambda
  • 的预期响应

    如何在 Route53 中定义自定义名称以路由到我的 Lambda 支持的 APIGateway API?

    最佳答案

    带有 Route53 A Record 的总体代码 LambdaRestApi 将创建

  • 指向特定阶段的自定义域 prodapi.test.example.org域到阶段“产品”(示例)
  • Route 53 api.test.example.org 的记录指向 Api Gateway 托管区域。

  • 这是两种有效的组合
  • https://api.test.example.org将直接指向舞台产品。
  • CDK输出https://abcdef1234.execute-api.us-east-1.amazonaws.com/prod/将在附加阶段时工作。

  • 这些是一些不起作用的组合
  • 您使用 http://
  • 进行的另外两个测试
  • 没有协议(protocol),默认为 http,将不起作用,因为我们的 api 网关默认提供 TLS 1.0 (ssl-https) 并且没有 http 监听器。
  • 您使用 https://进行的另一次尝试最后没有舞台名称,将返回 403 forbidden ,因为缺少艺名。

  • 这是完整的 CDK 代码。
    import * as cdk from "@aws-cdk/core";
    import * as apigw from "@aws-cdk/aws-apigateway";
    import * as acm from "@aws-cdk/aws-certificatemanager";
    import * as route53 from "@aws-cdk/aws-route53";
    import * as route53Targets from "@aws-cdk/aws-route53-targets";
    import * as lambda from "@aws-cdk/aws-lambda";

    export class HelloCdkStack extends cdk.Stack {

    constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    this.buildLambdaApiGateway();
    }

    buildLambdaApiGateway() {
    const rootDomain = "example.org";

    const zone = route53.HostedZone.fromLookup(this, "baseZone", {
    domainName: rootDomain,
    });

    const backend = new lambda.Function(this, "MyLayeredLambda", {
    code: new lambda.InlineCode("foo"),
    handler: "index.handler",
    runtime: lambda.Runtime.NODEJS_10_X,
    });

    const restApi = new apigw.LambdaRestApi(this, "myapi", {
    handler: backend,
    domainName: {
    domainName: `api-test.${rootDomain}`,
    certificate: acm.Certificate.fromCertificateArn(
    this,
    "my-cert",
    "arn:aws:acm:us-east-1:111112222333:certificate/abcd6805-1234-4159-ac38-761acdc700ef"
    ),
    endpointType: apigw.EndpointType.REGIONAL,
    },
    });

    new route53.ARecord(this, "apiDNS", {
    zone: zone,
    recordName: "api-test",
    target: route53.RecordTarget.fromAlias(
    new route53Targets.ApiGateway(restApi)
    ),
    });
    }
    }

    关于amazon-web-services - 如何在 CDK 中为基于 Lambda 的 APIGateway 提供自定义域名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65986897/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com