Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(cli) cdk import : resource-mapping options doesn't work #30318

Open
sudhirkumar04 opened this issue May 23, 2024 · 3 comments
Open

(cli) cdk import : resource-mapping options doesn't work #30318

sudhirkumar04 opened this issue May 23, 2024 · 3 comments
Labels
bug This issue is a bug. cli Issues related to the CDK CLI effort/medium Medium work item – several days of effort p2 package/tools Related to AWS CDK Tools or CLI

Comments

@sudhirkumar04
Copy link

sudhirkumar04 commented May 23, 2024

Describe the bug

cdk import --resource-mapping=resource-mapping.json doesn't work. This returns

Unrecognized resource identifiers in mapping file: ImportedKMSKey1, ImportedKMSKey2
No resources selected for import.

ImportedKMSKey1, ImportedKMSKey2 are resource names

--resource-mapping option also return same result when it used after --record-resource-mapping

cdk import --record-resource-mapping=resource-mapping.json // this command prompted for keyId twice
cdk import -resource-mapping=resource-mapping.json

This returns same as above

CoreAwsInfraStack
CoreAwsInfraStack/ImportedKMSKey1D83208A7/Resource: skipping
CoreAwsInfraStack/ImportedKMSKey2B3E62479/Resource: skipping
Unrecognized resource identifiers in mapping file: ImportedKMSKey1D83208A7, ImportedKMSKey2B3E62479
No resources selected for import.

Expected Behavior

cdk import --resource-mapping=resource-mapping.json should import the resources mentioned in the resource-mapping.json file

Current Behavior

cdk import --resource-mapping=resource-mapping.json doesn't work. This returns

CoreAwsInfraStack
CoreAwsInfraStack/ImportedKMSKey1/Resource: skipping
CoreAwsInfraStack/ImportedKMSKey2/Resource: skipping
Unrecognized resource identifiers in mapping file: ImportedKMSKey1, ImportedKMSKey2
No resources selected for import.

ImportedKMSKey1, ImportedKMSKey2 are resource names

Reproduction Steps

  1. Create a resource-mapping file, in this test, resource-mapping file is created inside lib/config/dev.json. E.g. below
{
  "ImportedKMSKey1": {
      "KeyId": "xxxx-ad04-42ae-9b36-xxxxxxx"
  },
  "ImportedKMSKey2": {
      "KeyId": "xxxxx-dca0-4d07-9fe7-xxxxxx"
  }
}
  1. Create file inside lib directory, this is for importing two kms keys.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Key } from 'aws-cdk-lib/aws-kms'
import {Tags} from 'aws-cdk-lib'
import * as fs from 'fs';

export class CoreAwsInfraStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    const config = JSON.parse(fs.readFileSync(`lib/config/dev.json`, 'utf8'));
    for (const [key] of Object.entries(config)) {
      new Key(this, key, {
      });
    }
  }
}
  1. Create a file inside bin directory
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { CoreAwsInfraStack } from '../lib/core-aws-infra-stack';

const app = new cdk.App();
new CoreAwsInfraStack(app, 'CoreAwsInfraStack', {
});
  1. AWS_DEFAULT_PROFILE is set
  2. Run cdk import --resource-mapping=lib/config/dev.json

Possible Solution

cdk import works but it prompts the kms key id which is a pain for rolling out for different accounts and environment. In order to avoid prompting keyId, resource-mapping should allow to map the resource.

Additional Information/Context

No response

CDK CLI Version

2.122

Framework Version

No response

Node.js Version

21.7.1

OS

mac

Language

TypeScript

Language Version

No response

Other information

No response

@sudhirkumar04 sudhirkumar04 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 23, 2024
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label May 23, 2024
@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels May 23, 2024
@khushail khushail self-assigned this May 23, 2024
@khushail
Copy link
Contributor

khushail commented May 24, 2024

@sudhirkumar04 , Thanks for reporting this.

I am able to repro the situation and resources are not getting imported however in my case, the error is a bit different which only says Skipping import. I found a somewhat similar past issue , there is a workaround suggested, not really sure if that works. You could check that out. Please feel free to share if that works!

I am currently investigating the root cause and looking into it. Will share my findings soon if there is an alternate on how this could be achieved.

@khushail khushail added p2 effort/medium Medium work item – several days of effort response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels May 24, 2024
@sudhirkumar04
Copy link
Author

sudhirkumar04 commented May 24, 2024

thanks for looking into it @khushail
Skipping import means its not importing the resource with the --resource-mapping options. The workaround you suggested has been checked before this issue raised. One of the solution suggested to use RemovalPolicy.DESTROY/RETAIN has also been checked.
In the issue you suggested not using --resource-mapping to map the resource. The main purpose of using --resource-mapping option is to import resources using ci/cd where the resources is taken from a file rather entering details for each resource manually in the terminal

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 24, 2024
@khushail
Copy link
Contributor

khushail commented May 28, 2024

@sudhirkumar04 , I understood your point in providing the resources through the file and I tried to repro the same scenario and faced similar error -

Screenshot 2024-05-28 at 1 33 02 PM

As per my investigation, this code leads to reading the data from the file -

: await resourceImporter.loadResourceIdentifiers(additions, options.resourceMappingFile);

which is invoking this module -

public async loadResourceIdentifiers(available: ImportableResource[], filename: string): Promise<ImportMap> {

Looks like, the file is not being read properly and error is produced by contents being read as Unknown -

if (unknown.length > 0) {

Since the direct option of passing the logical id exists through cdk import, marking this as P2.

@khushail khushail removed the investigating This issue is being investigated and/or work is in progress to resolve the issue. label May 28, 2024
@khushail khushail removed their assignment May 28, 2024
@pahud pahud added the cli Issues related to the CDK CLI label May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. cli Issues related to the CDK CLI effort/medium Medium work item – several days of effort p2 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

No branches or pull requests

3 participants