4
votes

just like in the title. I can deploy stuff on AWS using only Cloud Formation. Now I try to secure my API Gateway with API Keys and looks like I need a Usage Plan for it. It doesn't seem to be covered by the documentation right here: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html

Have any of you had a similar problem and if yes, how did you solved it?

3

3 Answers

6
votes

AWS has today released the ability to create AWS::ApiGateway::UsagePlan using cloud formation templates

Now AWS::ApiGateway::UsagePlanKey can be created using CloudFormation.

This snippet demonstrates how you might use a UsagePlan and UsagePlanKey, along with an APIKey in a CloudFormation Template

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
3
votes

For anyone reading, this is now supported via the AWS::ApiGateway::UsagePlanKey (docs) resource type in CloudFormation. From that page:

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.

0
votes

AWS has provided the CloudFormation template for the API Keys creation with the UsagePlan and the UsagePlan Keys as well, so probably the CFT for the same would be defined as:

 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

I hope this will probably help you.