简体   繁体   中英

CloudFormation - How can I reference a serverless usage plan?

Problem:

I want to associate an existing API key to a newly created "Usage Plan" which is created via AWS SAM as below:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: MyAPI
      OpenApiVersion: '2.0'
      EndpointConfiguration:
        Type: REGIONAL
      StageName: prod
      MethodSettings:
        - ResourcePath: "/*"
          HttpMethod: "*"
          ThrottlingBurstLimit: 1
          ThrottlingRateLimit: 1
      Domain:
        DomainName: api.mywebsite.com
        CertificateArn: 'arn:aws:acm:eu-north-1:101010101010:certificate/88888888-4444-3333-2222-1111111111'
        EndpointConfiguration: REGIONAL
        Route53:
          HostedZoneId: 'OMMITTD82737373'
        BasePath:
          - /
      Auth:
        UsagePlan:
          CreateUsagePlan: PER_API
          Description: Usage plan for this API
          Quota:
            Limit: 1000
            Period: MONTH
          Throttle:
            BurstLimit: 1
            RateLimit: 1
          Tags:
            - Key: Name
              Value: MyUsagePlan
  usagePlanKey:
    Type: 'AWS::ApiGateway::UsagePlanKey'
    Properties:
      KeyId: 2672762828
      KeyType: API_KEY
      UsagePlanId: ????????????????????

Is it possible to reference the UsagePlanId here? I tried: !Ref UsagePlan but no success... Any ideas on how to do it?

Thanks

As far as I know, there is no way to reference the UsagePlan created as part of your Api.

However, you can create UsagePlan outside of ApiGatewayApi as a separate resource, and associate it with your ApiGatewayApi . Then you can easily reference it in your UsagePlanKey :

usagePlan:
  Type: 'AWS::ApiGateway::UsagePlan'
  Properties:
    ApiStages:
      - ApiId: !Ref ApiGatewayApi
        Stage: prod
    ... (rest of the properties omitted)

usagePlanKey:
  Type: 'AWS::ApiGateway::UsagePlanKey'
  Properties:
    KeyId: 2672762828
    KeyType: API_KEY
    UsagePlanId: !Ref usagePlan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM