重用AWS :: ApiGateway :: ApiKey的cloudformation模板

问题描述

我有AWS SAM模板,其中一部分看起来像这样:

  # .......
  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
  #......

即使api键名不同,使用sam deploy --stack-name=stack-asam deploy --stack-name=stack-b将此模板部署为两个不同的应用程序(CloudFormation堆栈)也会失败,并显示以下错误

API密钥已经存在(服务:AmazonApiGateway;状态代码:409;错误代码:ConflictException;请求ID: 编辑代理: 空)

如何从此模板部署两个堆栈?

解决方法

不同的键值将解决您的问题。因此,如果您自己设置api键值,请确保它们是唯一的。不确定为什么这是不同阶段之间的约束。

,

您真的需要为钥匙提供价值吗?这是一个可选参数。您可以将其从模板中删除,AWS将在每次部署中为您生成一个唯一密钥。