Using an existing API key

When you specify an API in severless.yml, the framework generates the API key and assigns it to the functions. It generates the key even if one with the same name already exists in the environment.

Is there a way to specify an existing API key, rather than have the framework generate it? We really wish to keep generating the key separate from deployments.

Thanks for your time.

-mark

1 Like

Iā€™m in the same boat. Any ideas?

I was able to do this by manually creating the usage plan for my service using cloudformation syntax in the resources section and referring to my existing key by ID when specifying the UsagePlanKey. They key ID can be found in the AWS console. It is not an ARN, just a random alphanumeric ID.

resources:
  Resources:
    MyServiceUsagePlan:
      Type: "AWS::ApiGateway::UsagePlan"
      DependsOn: ApiGatewayRestApi
      Properties:
        UsagePlanName: myservice-dev
        ApiStages:
          - ApiId:
              Ref: ApiGatewayRestApi
            Stage: ${self:provider.stage}
    
    MyServiceUsagePlanKey:
      Type: "AWS::ApiGateway::UsagePlanKey"
      DependsOn: MyServiceUsagePlan
      Properties :
        KeyId: ThisIsWhereYouPutYourExistingKeyID
        KeyType: API_KEY
        UsagePlanId:
          Ref: MyServiceUsagePlan

Hi,
I am getting below exception while trying to use existing API Key:
An error occurred: ApiGatewayApiKey1 - ********* already exists in stack arn:aws:cloudformation:eu-central-1

Thanks

This is good but I feel like serverless should supply a more direct way of specifying API keys. My concern with our production APIs is that sometimes, for various reasons, we need to tear down a serverless stack and bring it back up again. Doing so invalidates the keys and this is very bad (need to distribute new keys).