Reusing cloudformation template for AWS::ApiGateway::ApiKey

664 views Asked by At

I have AWS SAM template, part of which looks like this:

  # .......
  InternalApiKey:
    Type: AWS::ApiGateway::ApiKey
    Properties:
      Enabled: true
      Name: !Sub internal_api_key_${Env}
      Value: !Ref InternalApiKeyValue
      StageKeys:
        - RestApiId: !Ref ServerlessRestApi
          StageName: Prod
  InternalUsagePlan:
    Type: AWS::ApiGateway::UsagePlan
    Properties:
      ApiStages:
        - ApiId: !Ref ServerlessRestApi
          Stage: Prod
  InternalUsagePlanKey:
    Type: AWS::ApiGateway::UsagePlanKey
    Properties:
      KeyId: !Ref InternalApiKey
      KeyType: API_KEY
      UsagePlanId: !Ref InternalUsagePlan
  #......

Deploying this template as two distinct application (CloudFormation stack) with sam deploy --stack-name=stack-a and sam deploy --stack-name=stack-b fails with the following error even when api key names are different:

API Key already exists (Service: AmazonApiGateway; Status Code: 409; Error Code: ConflictException; Request ID: redacted; Proxy: null)

How can I deploy two stacks from this template?

2

There are 2 answers

0
Abdul Moeez On BEST ANSWER

A different key value and your problem will go away. So if you are setting the api key value yourself ensure they are unique. Not sure why that's a constraint between different stages.

0
user2415876 On

Do you really have to provide a value for your key? It's an optional parameter. You can remove it from your template and AWS will generate a unique key for you in each deployment.