aws / aws/aws-cdk

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

Open
#30,318 7 comments 0 reactions 0 assignees View on GitHub
bug cli effort/medium p2 package/tools
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### 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"
}
}
```

2. 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, {
});
}
}
}
```
3. 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', {
});
```
4. AWS_DEFAULT_PROFILE is set
5. 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_

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the `cdk import --resource-mapping=lib/config/dev.json` command, using the stack in `lib/core-aws-infra-stack` and the `bin` entry point. Trace the CLI's resource-mapping handling and verify that the named KMS resources are recognized and imported without prompting for their key IDs.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cli, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.