Creating Usage Plan and Api Key Serverless 1.17

I am using serverless 1.17. I had a service previously deployed with 1.15, that created an api with a usage plan and an api key.

I removed the stack (sls remove). I checked to make sure ALL resources were gone. I redeployed (sls deploy). I received the following error:

An error occurred while provisioning your stack: EISearchServiceUsagePlanKey - Usage Plan wkw8wr cannot be added because API Key axnaxll39k cannot reference multiple Usage Plans with the same API Stage: atax6mq8o4:dev.

To me this implies that not all the resources were removed when I did an sls remove, even though no resources related to the service existed when I attempted to redeploy. Is there an issue with CloudFormation and UsagePlan? Is sls properly removing the Plan? I assume so since no UsagePlan or Api Key existed when I attempted to redeploy.

Any ideas?

Well, I figured it out.

As I mentioned, originally I was using severless 1.15.1, and I had upgraded to 1.17. While under 1.15, I created and configured usage plans in my serverless file. It appears that in 1.17, usage plans are created automatically if you are adding an api key. So, when I upgraded to 1.17, all of a sudden I am attempting to create another usage plan and adding the same api that was added to the usage plan serverless creates for me.

I must have missed the documentation regarding this change is 1.17. I assume I can configure the usage plan that serverless creates by the normal overriding process. If anyone knows of any gotcha’s let me know.

Same thing happened for me. I had issues with 1.22.0 whereby a new usage plan was being created for an existing API key. Had to npm uninstall -g serverless and reinstall to an older version e.g. npm install -g serverless@1.10.2 in order to deploy.

Can you/ how did you configure usage plan IDs manually @shawnmullen?

If you are using the latest version of Serverless and if you already have an api key assigned to a usage plan, you will need to delete the usage plan first and then re-deploy. Serverless will then create a new usage plan and assign the api key to it.

Assuming the following is in the provider section of the serverless.yml file:

apiKeys:
- UserServiceApiKey

I put the following in the resources section:

resources:
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: my-user-service-api

UserServiceUsagePlan:
  Type: AWS::ApiGateway::UsagePlan
  DependsOn: ApiGatewayApiKey1
  Properties:
    Description: Usage plan for the user service api.
    UsagePlanName: user-service-api-usage-plan
    ApiStages:
      - ApiId:
          Ref: ApiGatewayRestApi
        Stage: ${self:provider.stage}

UserServiceUsagePlanKey:
  Type: AWS::ApiGateway::UsagePlanKey
  DependsOn: UserServiceUsagePlan
  Properties :
    KeyId:
      Ref: ApiGatewayApiKey1
    KeyType: API_KEY
    UsagePlanId:
      Ref: UserServiceUsagePlan