您可以使用 Cloud Formation 创建使用计划吗?

Can you create Usage Plan with Cloud Formation?

就像标题一样。我可以仅使用 Cloud Formation 在 AWS 上部署东西。现在我尝试使用 API 密钥保护我的 API 网关,看起来我需要一个使用计划。此处的文档似乎并未涵盖它:http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html

你们有没有遇到过类似的问题,如果有,你们是怎么解决的?

A​​WS today released the ability to create AWS::ApiGateway::UsagePlan 使用云形成模板

现在 AWS::ApiGateway::UsagePlanKey 可以使用 CloudFormation 创建。

此代码段演示了如何在 CloudFormation 模板中使用 UsagePlan 和 UsagePlanKey 以及 APIKey

UsagePlan:
  Type: 'AWS::ApiGateway::UsagePlan'
  Properties:
    ApiStages:
      - ApiId: !Ref MyRestApi
        Stage: !Ref Prod
    Description: Customer ABCs usage plan
    Quota:
      Limit: 5000
      Period: MONTH
    Throttle:
      BurstLimit: 200
      RateLimit: 100
    UsagePlanName: Plan_ABC

ApiKey:
  Type: 'AWS::ApiGateway::ApiKey'
  Properties:
    Name: TestApiKey
    Description: CloudFormation API Key V1
    Enabled: 'true'

UsagePlanKey:
  Type: 'AWS::ApiGateway::UsagePlanKey'
  Properties:
    KeyId: !Ref ApiKey
    KeyType: API_KEY
    UsagePlanId: !Ref UsagePlan

对于任何阅读者,现在通过 CloudFormation 中的 AWS::ApiGateway::UsagePlanKey (docs) 资源类型支持这一点。来自该页面:

The AWS::ApiGateway::UsagePlanKey resource associates an Amazon API Gateway API key with an API Gateway usage plan. This association determines which users the usage plan is applied to.

AWS 已经为 API 使用 UsagePlan 和 UsagePlan Keys 的密钥创建提供了 CloudFormation 模板,因此可能将相同的 CFT 定义为:

 ApiKey:
    Type: 'AWS::ApiGateway::ApiKey'
    DependsOn:
      - ApiGatewayDeployment      
    Properties:
      Name: !Sub "you keyName-Apikeys"
      Description: Api Keys Description
      Enabled: 'true'
      StageKeys:
        - RestApiId: !Ref ApiGatewayRestApi
          StageName: !Sub "your stageName"
  
 usagePlan:
    Type: 'AWS::ApiGateway::UsagePlan'
    DependsOn:
      - ApiGatewayDeployment
    Properties:
      ApiStages:
        - ApiId: !Ref ApiGatewayRestApi
          Stage:  !Sub "your stageName"
      Description: your description of usage plan
      Quota:
        Limit: 50000
        Period: DAY
      Throttle:
        BurstLimit: 200
        RateLimit: 100
      UsagePlanName: !Sub "define your name of UsagePlan"

 usagePlanKey:
    Type: 'AWS::ApiGateway::UsagePlanKey'
    DependsOn:
      - ApiGatewayDeployment
    Properties:
      KeyId: !Ref ApiKey
      KeyType: API_KEY
      UsagePlanId: !Ref usagePlan

希望这对您有所帮助。