cdklabs / awscdk-v1-stack-finder

Apache License 2.0
18 stars 5 forks source link

Version extracted from previous stack #267

Closed alexeiaguiar closed 1 year ago

alexeiaguiar commented 1 year ago

We received some emails telling us that we have tens of stacks that are still using CDK v1. When I ran awscdk-v1-stack-finder, it said that no stack was found. This happened for all of our accounts. While I was debugging the code, I saw that the variable body had information about a previous stack from the loop, instead of the current one. I'm not 100% sure, but I think this problem happens in this scenario:

  1. We deserialise a stack that has its template in JSON or YAML format. At this time, body is not null anymore.
  2. In the next loop iteration, we have a template in YAML format. The JSON parsing on line 147 will fail so, body will keep the value from the previous loop iteration.
    var body;
    var jsonErr;
    try {
      body = JSON.parse(getTemplateResponse.TemplateBody);
    } catch (err) { jsonErr = err; }

    if (!body) {
      try {
        body = YAML.parse(getTemplateResponse.TemplateBody);
      } catch (yamlErr) {
        console.error(`${region}: Failed to parse template for stack: ${stack.StackName}. \nJSON Parse Error: ${jsonErr} \nYAML Parse Error: ${yamlErr}`);
      }
    }

https://github.com/cdklabs/awscdk-v1-stack-finder/blob/main/src/index.ts#L144-L156

The fix I did in my local project might not be the right one, but I was able to see the stacks that are still using CDK v1. This was the fix I did on line 144:

    var body = null;
k-dahl commented 1 year ago

I ran into the same issue - received warning email about CDKv1 recommending to scan with this tool, but the output showed:

No AWS CDK V1 stacks found

... adding the var body = null; and re-running correctly pointed out the 5 CDK v1 stacks that we have.

rix0rrr commented 1 year ago

Thanks for the report! Just fixed this.