EC2 fails when importing cdk #31724
Labels
@aws-cdk/aws-ec2
Related to Amazon Elastic Compute Cloud
bug
This issue is a bug.
needs-reproduction
This issue needs reproduction.
p2
Describe the bug
When creating an EC2 in the AWS Management Console and importing it into the cdk using the cdk import command, the following error occurs and prevents importing
❌ ec2-1 failed: Error [ValidationError]: Template error: Mapping named 'MyInstancerestoreAmiMapA3AA9468' is not present in the 'Mappings' section of template.
at Request.extractError (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:46692)
at Request.callListeners (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:91385)
at Request.emit (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:90833)
at Request.emit (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:199229)
at Request.transition (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:192781)
at AcceptorStateMachine.runTo (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:157653)
at C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:157983
at Request.callListeners (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:91553) {
code: 'ValidationError',.
time: 2024-10-09T06:27:13.516Z,.
}
Template error: Mapping named 'MyInstancerestoreAmiMapA3AA9468' is not present in the 'Mappings' section of the template.
Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
When executing the cdk import command, you will be prompted for the EC2 instanceID, and if you enter it, the EC2 instance will be imported into the CDK code.
Current Behavior
The following error is output
❌ ec2-1 failed: error [ValidationError]: template error: the mapping named 'MyInstancerestoreAmiMapA3AA9468' does not exist in the 'Mappings' section of the template.
at Request.extractError (C:\Users\61-202111-3263-user\Desktop\ new folder\node_modules\aws-cdk\lib\index.js:373:46692)
at Request.callListeners (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:91385)
at Request.emit (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:90833)
at Request.emit (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:199229)
at Request.transition (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:192781)
at AcceptorStateMachine.runTo (C:◆Users61-202111-3263-user;Desktop;node_modules;aws-cdk;libindex.js:373:157653).
at C:\Users\61-202111-3263-user\Desktop\New folder\node_modules\aws-cdk\lib\index.js:373:157983
at Request.callListeners (C:\Users\61-202111-3263-user\Desktop\new folder\node_modules\aws-cdk\lib\index.js:373:91553) {
code: ValidationError',
time: 2024-10-09T06:27:13.516Z,.
}
Template error: the mapping named 'MyInstancerestoreAmiMapA3AA9468' does not exist in the 'Mappings' section of the template.
Reproduction Steps
I created the following typescript in the CDK lib folder and tried to import a service with a definition of “MyInstance_restor”
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { StackProps } from 'aws-cdk-lib'
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as route53 from 'aws-cdk-lib/aws-route53'
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch'
import * as elasticloadbalancingv2targets from 'aws-cdk-lib/aws-elasticloadbalancingv2-targets'
import { aws_elasticloadbalancingv2 as elbv2 } from 'aws-cdk-lib'
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import { aws_iam as iam } from 'aws-cdk-lib'
import { CfnMapping } from 'aws-cdk-lib'
import * as sqs from 'aws-cdk-lib/aws-sqs';
interface Props extends StackProps {
vpc: ec2.Vpc
}
export class EC2_1 extends cdk.Stack {
public testtg1: elbv2.ApplicationTargetGroup
constructor(scope: Construct, id: string, props: Props) {
super(scope, id, props);
)
const CloudWatchPolicy = iam.ManagedPolicy.fromManagedPolicyArn(
this,
'CloudWatchPolicy',
'arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy'
)
const rlJcWebap = new iam.Role(this, "webap", {
roleName: "webap", // ロール名
description: 'webap', //説明
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'), // サービス
path: '/', //パス(default:/)
managedPolicies: [SSMPolicy, CloudWatchPolicy], // マネージドポリシー
maxSessionDuration: cdk.Duration.hours(1), //ロールに設定する最大セッション期間(default:1時間)
// externalIds:, //ロールの引き渡しに関する外部IDの設定
// permissionsBoundary:, // 権限の境界設定
// inlinePolicies:, //インラインポリシー
})
const instanceProfileWebap = new iam.CfnInstanceProfile(this, 'instanceProfileWebap', {
roles: [rlJcWebap.roleName],
instanceProfileName: rlJcWebap.roleName,
})
const securityGroup = new ec2.SecurityGroup(this, 'MySecurityGroup', {
vpc: props.vpc,
description: 'Allow ssh access to ec2 instances',
securityGroupName: 'MySecurityGroupEC2'
});
});
cdk.Tags.of(instance).add("test", "tag1") // タグ
cdk.Tags.of(instance).add("names", "tag500000") // タグ
const jcWebap1Profile = instance.node.defaultChild as ec2.CfnInstance
instance.node.tryRemoveChild('InstanceProfile')
jcWebap1Profile.addDependency(instanceProfileWebap)
jcWebap1Profile.addPropertyOverride('IamInstanceProfile', instanceProfileWebap.ref)
const instance_restore = new ec2.Instance(this, 'MyInstance_restore', {
vpc: props.vpc,
instanceType: new ec2.InstanceType('t2.micro'), // インスタンスタイプ
machineImage: new ec2.GenericWindowsImage({
'ap-northeast-1': "ami-005dc502f025ac443"
}), // マシンイメージ
vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC }, // パブリックサブネットにデプロイ
securityGroup: securityGroup,
instanceName:"test1",
requireImdsv2:true,
role: rlJcWebap
});
cdk.Tags.of(instance_restore).add("test", "tag1") // タグ
cdk.Tags.of(instance_restore).add("names", "tag500000") // タグ
const jcWebapResotreProfile = instance_restore.node.defaultChild as ec2.CfnInstance
instance_restore.node.tryRemoveChild('InstanceProfile')
jcWebapResotreProfile.addDependency(instanceProfileWebap)
jcWebapResotreProfile.addPropertyOverride('IamInstanceProfile', instanceProfileWebap.ref)
const testtg1 = new elbv2.ApplicationTargetGroup(
this,
"test1-1",
{
targetGroupName: "tg1", // ターゲットグループ名
vpc: props.vpc, //VPC
protocol: elbv2.ApplicationProtocol.HTTP, // プロトコル
protocolVersion: elbv2.ApplicationProtocolVersion.HTTP1, // プロトコルのバージョン
port: 80, // ポート
healthCheck: {
enabled: true,
protocol: elbv2.Protocol.HTTP, // プロトコル
path: '/', // パス (default: /)
port: 'traffic-port', // ポート (default: traffic-port)
healthyThresholdCount: 5, //連続成功回数の閾値 (default:5)
unhealthyThresholdCount: 2, //連続ヘルスチェック失敗の回数(default: 2)
timeout: cdk.Duration.seconds(6), // タイムアウト時間
interval: cdk.Duration.seconds(30), // インターバル
healthyHttpCodes: '200,302', // HTTP成功時の応答コード
// healthyGrpcCodes:, // grpc成功時の応答コード
}, // ヘルスチェック
targetType: elbv2.TargetType.INSTANCE, // ターゲットのタイプ
targets: [new elasticloadbalancingv2targets.InstanceTarget(instance)], //ターゲット
deregistrationDelay: cdk.Duration.seconds(300), // ターゲットの解除後,実際に解除するまでの待機時間(default:300秒)
loadBalancingAlgorithmType: elbv2.TargetGroupLoadBalancingAlgorithmType.ROUND_ROBIN, // 負荷分散アルゴリズム(default: ROUND_ROBIN)
slowStart: cdk.Duration.seconds(30), // スロースタート設定(default: 30)
stickinessCookieDuration: cdk.Duration.days(1), //スティッキーセッションの有効期間 (default: 1日)
//stickinessCookieName:'', //スティッキーセッションのクッキー名
}
)
cdk.Tags.of(testtg1).add("Names", "test110") // タグ
cdk.Tags.of(testtg1).add("test", "tag1") // タグ
}
}
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
2.126.0
Framework Version
No response
Node.js Version
18.18.2
OS
WIndows
Language
TypeScript
Language Version
TypeScript 5.5.3
Other information
No response
The text was updated successfully, but these errors were encountered: