此错误是由于在使用AWS CDK创建EC2实例时,传递的'InstanceProps'类型中的'securityGroups'属性的类型与实际值的类型不匹配而引起的。可能是因为实际值是字符串,而'securityGroups'属性的类型是一个安全组ID或一个安全组列表。
要解决此问题,您可以使用以下代码示例中的方法之一:
方法1: 在创建EC2实例时,将'securityGroups'属性的类型更改为字符串。
const instance = new ec2.Instance(this, 'MyInstance', { instanceType: new ec2.InstanceType('t2.micro'), machineImage: new ec2.AmazonLinuxImage(), securityGroups: ['sg-1234567890'], });
方法2: 将实际值转换为正确的类型,以与InstanceProps中的'securityGroups'属性类型匹配。
const instance = new ec2.Instance(this, 'MyInstance', { instanceType: new ec2.InstanceType('t2.micro'), machineImage: new ec2.AmazonLinuxImage(), securityGroups: [ec2.SecurityGroup.fromSecurityGroupId(this, 'MySG', 'sg-1234567890')], });