amazon-web-services - 如何将不同项目的多个基本路径映射添加到同一个AWS API网关中?

标签 amazon-web-services aws-api-gateway aws-cdk amazon-route53

我们针对不同的 API 有单独的 AWS CDK 项目。我们希望对同一 API 网关资源使用具有不同基本路径映射的相同子域。例如;假设我们有两个 API,分别是 tenantApiinvoiceApi 映射到 test.example.com/tenanttest.example.com/发票。通过创建一个 RestApi 并定义多个基本路径映射到它,可以从一个存储库实现这一点。但是,我无法找到一种方法来从不同的存储库实现此目的,因为我只需要为子域创建一个 ARecord。我的想法是在我们管理共享资源的存储库中创建 ARecord ,并从我们将使用相同 Api 网关的存储库导入该记录。

这是关于如何创建 Api 网关的简单 aws cdk 代码。正如您所看到的,我们必须将 RestApi 实例传递到 route53.RecordTarget.fromAlias 中,因此我不确定我们是否可以在创建 Api 网关之前创建 ARecord。

export class ApiGatewayStack extends Stack {
  constructor(scope: Construct, id: string, props: StackEnvProps) {
    super(scope, id, props);

    const tenantApi = new apigateway.RestApi(this, 'tenantApi', {
      domainName: {
        domainName: props.context['domainName'],
        certificate: acm.Certificate.fromCertificateArn(this, 'certificateArn', props.context['certificateArn']),
        basePath: 'tenant'
      },
      deploy: true,
      deployOptions: {
        stageName: 'prod',
      },
      defaultCorsPreflightOptions: {
        allowMethods: apigateway.Cors.ALL_METHODS,
        allowOrigins: apigateway.Cors.ALL_ORIGINS,
      }
    });

    const zone = route53.HostedZone.fromLookup(this, 'Zone', { domainName: 'example.com' });

    // create an alias for mapping
    new route53.ARecord(this, 'domainAliasRecord', {
      zone: zone,
      recordName: "test",
      target: route53.RecordTarget.fromAlias(new ApiGateway(tenantApi)),
    });

    const methodOptions: apigateway.MethodOptions = {
      methodResponses: [
        {
          statusCode: '200',
          responseParameters: {
            'method.response.header.Content-Type': true,
          },
        },
        {
          statusCode: '400',
          responseParameters: {
            'method.response.header.Content-Type': true,
          },
        },
      ],
    };

    const postPaymentsLambda = new NodejsFunction(this, 'postTenantLambda', {
      entry: './lambda/rest/tenant-api/post-tenant-api.ts',
      handler: 'handler',
      memorySize: 512,
      runtime: lambda.Runtime.NODEJS_14_X,
    });

    // tenant/v1
    const tenantV1 = tenantApi.root.addResource('v1');
    tenantV1.addMethod('POST', new apigateway.LambdaIntegration(postPaymentsLambda), methodOptions);

  }
}

非常感谢您的帮助。谢谢!

最佳答案

我必须首先创建一个domainName,然后创建一个ARecord,并以该domainName为目标,它可以从我想要附加的不同API导入.

// create domain name for api gateway
const domainName = new apigateway.DomainName(this, 'domainName', {
  domainName: `test.${props.domainName}`,
  certificate: acm.Certificate.fromCertificateArn(this, 'certificateArn', props.certificateArn),
  endpointType: apigateway.EndpointType.REGIONAL,
  securityPolicy: apigateway.SecurityPolicy.TLS_1_2,
});

const zone = route53.HostedZone.fromLookup(this, 'hostedZone', {
  domainName: props.context['domainName']
});

// create an alias for mapping
new route53.ARecord(this, 'apiGatewayDomainAliasRecord', {
  zone: zone,
  recordName: 'test',
  target: route53.RecordTarget.fromAlias(new r53target.ApiGatewayDomain(domainName)),
});

new CfnOutput(this, 'apiGatewayDomainNameAliasTarget', {
  value: domainName.domainNameAliasDomainName,
  description: 'domainNameAliasTarget attribute used when importing domain name',
  exportName: 'apiGatewayDomainNameAliasTarget'
});

稍后,我将导入此 domainName 以创建 BasePathMapping。导入domainName时会用到三个属性;

  • domainName:我们之前创建的域名。
  • domainNameAliasHostedZoneId:定义域的托管 ZoneId。
  • domainNameAliasTarget:AWS 文档没有明确说明它是什么。基本上,它是我们首先创建的 domainNamedomainNameAliasDomainName 值。
const tenantApi = new apigateway.RestApi(this, 'tenantApi', {
  deployOptions: {
    stageName: 'dev',
  },
  deploy: true,
  defaultCorsPreflightOptions: {
    allowMethods: apigateway.Cors.ALL_METHODS,
    allowOrigins: apigateway.Cors.ALL_ORIGINS,
  }
});

const domainName = apigateway.DomainName.fromDomainNameAttributes(this, 'domainName', {
  domainName: `test.${props.domainName}`,
  domainNameAliasHostedZoneId: props.hostedZoneId,
  domainNameAliasTarget: Fn.importValue(props.apiGatewayDomainNameAliasTarget),
});

const nodeBasePathMapping = new apigateway.BasePathMapping(this, 'nodeBasePathMapping', {
  basePath: 'node',
  domainName,
  restApi: tenantApi,
});

关于amazon-web-services - 如何将不同项目的多个基本路径映射添加到同一个AWS API网关中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73199148/

相关文章:

aws-sdk - 使用 cdk 部署 lambda 无法识别 sdk Nodejs v3

aws-cdk - AWS CDK 条件阶段部署

amazon-web-services - 使用域名 token 时如何为 AWS CDK 指定验证域

c - 从 C 代码调用 DynamoDB 低级 API 时出错

amazon-web-services - AWS Batch EC2 配置时间

amazon-web-services - 从 Swagger 错误导入 Amazon API Gateway - 不采用泛型

node.js - 如果每 30 秒超过 1 个请求(进程在完成请求之前退出),则 Lambda 无法保持/删除请求(通过 API 网关)

java - 如何在 AWS 上定义 spring profile

amazon-web-services - AWS 安全组导出上的协议(protocol) -1 是什么意思?

aws-cloudformation - 如何将 Route53 A 记录定位到 ApiGateway v2