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

**URL:** <https://forum.serverless.com/t/creating-a-usage-plan-and-associating-it-with-a-stage-on-one-deploy/10792>\
**Category:** Serverless Framework\
**Tags:** aws\
**Created:** [March 13, 2020, 7:20pm UTC](https://forum.serverless.com/t/creating-a-usage-plan-and-associating-it-with-a-stage-on-one-deploy/10792 "2020-03-13T19:20:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LiaisonLarry](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/liaisonlarry/32/3914_2.png) [@LiaisonLarry](https://forum.serverless.com/u/LiaisonLarry)\
**Post date:** [March 13, 2020, 7:20pm UTC](https://forum.serverless.com/t/creating-a-usage-plan-and-associating-it-with-a-stage-on-one-deploy/10792/1 "2020-03-13T19:20:44Z")

</div>

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
Properties:
  UsagePlanName: ${self:service}-${self:provider.stage}
  Description: ${self:service} usageplan for ${self:provider.stage}
  ApiStages:
   - 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!!

---

<div class="post-metadata">

**Author:** ![garethmcc](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/garethmcc/32/2276_2.png) [@garethmcc](https://forum.serverless.com/u/garethmcc)\
**Post date:** [March 15, 2020, 8:58am UTC](https://forum.serverless.com/t/creating-a-usage-plan-and-associating-it-with-a-stage-on-one-deploy/10792/2 "2020-03-15T08:58:27Z")

</div>

You don’t need to use CloudFormation to specify usage plans. That is available as a part of the framework: [https://serverless.com/framework/docs/providers/aws/events/apigateway/#setting-api-keys-for-your-rest-api](https://serverless.com/framework/docs/providers/aws/events/apigateway/#setting-api-keys-for-your-rest-api)

---

<div class="post-metadata">

**Author:** ![dnafication](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/dnafication/32/4513_2.png) [@dnafication](https://forum.serverless.com/u/dnafication)\
**Post date:** [August 26, 2020, 4:35am UTC](https://forum.serverless.com/t/creating-a-usage-plan-and-associating-it-with-a-stage-on-one-deploy/10792/3 "2020-08-26T04:35:32Z")

</div>

@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

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

```

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