2

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?

yskkin
  • 854
  • 7
  • 24
  • 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. – Abdul Moeez Sep 25 '20 at 08:04
  • 1
    @AbdulMoeez That is the reason! I'll accept this once you submit this as an answer. – yskkin Sep 25 '20 at 08:24

2 Answers2

2

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.

Marcin
  • 215,873
  • 14
  • 235
  • 294
Abdul Moeez
  • 1,331
  • 2
  • 13
  • 31
0

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.