Creating a usage plan and associating it with a stage on one deploy

Here’s a bit of yml where I’m trying to create a usage plan and associate it with a stage:

Type: "AWS::ApiGateway::UsagePlan"
DependsOn: CasApiGateway
  UsagePlanName: ${self:service}-${self:provider.stage}
  Description: ${self:service} usageplan for ${self:provider.stage}
   - ApiId:
       Ref: CasApiGateway
     Stage: ${self:provider.stage}

For deploying to a new stage, it doesn’t work. I get the following error:

An error occurred: DefaultServiceUsagePlan - API Stage not found: ylml49tsq4:dev (Service: AmazonApiGateway; Status Code: 404; Error Code: NotFoundException; Request ID: 990af856-f981-4097-935c-062e4d9201b7)

If I comment out the ApiStages section, it will deploy successfully, but there’s no API Stage associated. If I then uncomment the ApiStages section and redeploy it will work and have the API Stage associated. I’m sure I’m missing something here, but how do I get the ApiStages associated correctly on the first deploy?? Thanks!!

You don’t need to use CloudFormation to specify usage plans. That is available as a part of the framework:

@garethmcc, do you know how to use existing API Key within the serverless template? Using the apiKey in the serverless template with existing API key throws an error

Serverless Error ---------------------------------------
  An error occurred: ApiGatewayApiKey1 - XXXXXX already exists.

Looks like its trying to create it again and thus resulting in error.