c# - 在 AWS Lambda serverless.template 文件中设置 EndpointConfiguration

标签 c# .net amazon-web-services aws-lambda aws-api-gateway

AWS API Gateway(相对)最近允许将端点配置设置为区域性而非边缘优化。我正在使用 .NET 创建无服务器 lambda 函数。

据我了解,我需要添加以下值来设置端点类型:

"EndpointConfiguration": { "Types" : [ "REGIONAL" ] }

serverless.template 是否接受这个键值对,我应该把它放在哪里?

编辑:我没有使用无服务器框架。我在 Visual Studio 2017 中使用 AWS 工具包

最佳答案

当您说您在 Visual Studio 2017 中使用 AWS 工具包时,我假设您正在使用 AWS SAM 并构建 CloudFormation 模板(正如您提到的 serverless.template 所推断的那样)。

目前 AWS SAM 不支持使用区域终端节点设置 API 网关。这是当前正在跟踪它的 GitHub issue


但是,如果您使用的是自定义域,则有一个合理的解决方法。

即使您的 API 网关是在 Endpoint 设置为 Edge Optimized 的情况下创建的,您也可以创建 Endpoint 设置为 Regional 的 AWS::ApiGateway::DomainName 资源。


下面是我如何在我的 AWS SAM 模板中实现这一点的示例(我使用 YAML 而不是 JSON,因为它更容易处理多行值)。

注意:AWS::ApiGateway::DomainName 目前不提供有关区域端点的信息,因此我包含了一个能够检索我们需要的信息的自定义资源。

  MyApiGateway:
    Type: 'AWS::Serverless::Api'
    Properties:
      StageName: 'prod'

  ApiCertificate:
    Type: 'AWS::CertificateManager::Certificate'
    Properties:
      DomainName: 'api.example.com'
      DomainValidationOptions:
      - DomainName: 'api.example.com'
        ValidationDomain: 'example.com'

  CustomResourceLambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Action: 'sts:AssumeRole'
          Effect: Allow
          Principal:
            Service: 'lambda.amazonaws.com'
      Path: /
      ManagedPolicyArns:
      - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
      Policies:
      - PolicyName: ApiGateway
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Action:
            - 'apigateway:*'
            Effect: Allow
            Resource: '*'

  DomainNameInfoCustomResourceFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: index.handler
      Role: !GetAtt CustomResourceLambdaExecutionRole.Arn
      Runtime: 'nodejs6.10'
      Timeout: 300
      Code:
        ZipFile: |
          const AWS = require('aws-sdk');
          const response = require('cfn-response');

          exports.handler = function(event, context) {
              const ApiGateway = new AWS.APIGateway();
              ApiGateway.getDomainName({
                  domainName: event.ResourceProperties.DomainName
              }, (err, data) => {
                  if (err != null) {
                      response.send(event, context, response.FAILED, undefined);
                  } else {
                      response.send(event, context, response.SUCCESS, {
                          DomainName: data.domainName,
                          RegionalDomainName: data.regionalDomainName,
                          RegionalHostedZoneId: data.regionalHostedZoneId,
                          DistributionDomainName: data.distributionDomainName,
                          DistributionHostedZoneId: data.distributionHostedZoneId
                      });
                  }
              });
          }

  ApiDomainName:
    Type: 'AWS::ApiGateway::DomainName'
    Properties:
      DomainName: 'api.example.com'
      EndpointConfiguration:
        Types:
        - REGIONAL
      RegionalCertificateArn: !Ref ApiCertificate

  ApiBasePathMapping:
    Type: 'AWS::ApiGateway::BasePathMapping'
    DependsOn: [MyApiGatewayprodStage, ApiDomainName]
    Properties:
      DomainName: 'api.example.com'
      RestApiId: !Ref MyApiGateway
      Stage: 'prod'

  ApiDomainNameInfo:
    Type: 'Custom::DomainNameInfo'
    DependsOn: [ApiDomainName, ApiBasePathMapping]
    Properties:
      ServiceToken: !GetAtt DomainNameInfoCustomResourceFunction.Arn
      DomainName: !Ref ApiDomainName

  ApiRecordSet:
    Type: 'AWS::Route53::RecordSet'
    DependsOn: [ApiDomainNameInfo]
    Properties:
      HostedZoneId: '0123456789' # ENTER YOUR DOMAINS HOSTED ZONE ID
      Name: 'api.example.com'
      ResourceRecords:
      - !GetAtt ApiDomainNameInfo.RegionalDomainName
      Type: CNAME
      TTL: 60

关于c# - 在 AWS Lambda serverless.template 文件中设置 EndpointConfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48326029/

相关文章:

c# - 由另一个对象决定的有限单例生命周期

c# - 从 Entity Framework 发送阿拉伯语单词时存储过程不返回结果

c# - MVC4 RouteConfig 参数返回 null

javascript - 正则表达式:重复模式 n 次,最终重复有变化

ios - 如何避免在 Cognito 联合身份中创建未使用的身份

python - 在 AWS Lambda 中访问 GET 参数

amazon-web-services - 在 AWS Lambda 中存储单个值的最佳实践

c# - ANTLR 访问者单元测试在一条规则上成功,但在另一条规则上失败

c# - 为什么这些 Type 对象不相等?

c# - ICorProfiler : Why do I get the wrong type token for a jitted function?