Popeye
Popeye

Reputation: 437

AWS CDK deploy same resources and same stacks with different pipeline | Typescript

Pretty new to AWS CDK and am getting stuck with a certain issue.

I have a working CDK code pipeline deploying a bunch of resources to different environments, eventually making its way to prod. It uses master as its source branch and is currently has a prod deployment waiting to make it into production.

In order to allow devs to keep working I created a new pipeline which works on a new branch called dev but is meant to deploy the same resources to the same stacks but only in our dev environment.

Once the new dev pipeline runs I get the error

Resource handler returned message: "Usage Plan j4p4g2 cannot be added because API Key n8uyhik8h8 cannot reference multiple Usage Plans with the same API Stage: 9i1lnft358:v1 (Service: ApiGateway, Status Code: 409, Request ID: 54889a52-4fb8-4c90-93e5-31c8b1865335, Extended Request ID: null)" (RequestToken: 6fb61327-fa39-b967-8969-639daa658c72, HandlerErrorCode: AlreadyExists)

it seems that despite the same stack name and resources it is trying to add a new usage plan instead of accept the existing one

Second pipeline created like so

if (stackBuildTargetAcct === 'dev') {

  new PipelineStack(app, 'PipelineDev', {
    environment: 'dev',
    stackName: 'dev-build-pipeline',
  })

} else if (stackBuildTargetAcct === 'prod') {
  new PipelineStack(app, 'Pipeline', {
    environment: 'prod',
    stackName: 'master-build-pipeline',
  })
}

I figured that is the stack name is the same and the resources are the same why would it think it needs to create a new resource? I suspect its the 'Pipeline' vs 'PipelineDev' id but when I change them both to 'Pipeline', I can't deploy the new pipeline due to the following error

Pipeline/Pipeline/Pipeline/ArtifactsBucketEncryptionKeyAlias (PipelineArtifactsBucketEncryptionKeyAlias94A07392) alias/codepipeline-pipelinefb9defa0 already exists in stack arn:aws:cloudformation:ap-southeast-2:master-build-pipeline

Any help or advice appreciated

Upvotes: 3

Views: 3330

Answers (1)

Popeye
Popeye

Reputation: 437

I eventually figured out the solution and got a deeper understanding of the problem.

The problem is caused by a few things happening together:

  • The fact that there is often a create before delete policy in aws cloud formation, meaning new resources will be created before the old are removed
  • Logical id's in cloud formation when left to be generated by default will be different for different pipelines
  • Usage plans can't reference the same api key for the same api gateway

Also I'd like to note that in this repo we had explicit names for everything except the usage plan (usage plans don't have names) in the code thus far. The solution was to find the current logical id used for the usage plan and set it in the code like so:

 plan.addApiKey(importedKey, {overrideLogicalId: "<Logical id that you found is already deployed here>"})

I suspect similar errors can often be solved by giving a explicate name to a resource so its logical id wont be randomly generated or by overriding the logical id in a similar fashion as I needed to

Upvotes: 3

Related Questions