CDK中如何给其他安全组添加安全组ID?
How do you add security group ID to other security group in CDK?
const securityGroup = new ec2.SecurityGroup(this, "Ec2SecurityGroup", {
vpc,
});
const securityGroupId = "sg-test";
securityGroup.addIngressRule(
// doesn't work
ec2.Peer.ipv4(securityGroupId),
// doesn't work
ec2.Peer.prefixList(securityGroupId),
ec2.Port.tcp(5432),
"SecurityGroup of Test"
);
我想添加一个安全组的ID,但是好像不可能...
从查看文档开始:
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.SecurityGroup.html
如您所见,您可以将 SecurityGroup
传递给 peer
属性。
要从其 ID 创建 SecurityGroup
,请使用 SecurityGroup.fromSecurityGroupId
:
const securityGroup = new ec2.SecurityGroup(this, "Ec2SecurityGroup", {
vpc,
});
const otherSecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(
this,
"OtherSecurityGroup",
"sg-test"
);
securityGroup.addIngressRule(
otherSecurityGroup,
ec2.Port.tcp(5432),
"SecurityGroup of Test"
);
const securityGroup = new ec2.SecurityGroup(this, "Ec2SecurityGroup", {
vpc,
});
const securityGroupId = "sg-test";
securityGroup.addIngressRule(
// doesn't work
ec2.Peer.ipv4(securityGroupId),
// doesn't work
ec2.Peer.prefixList(securityGroupId),
ec2.Port.tcp(5432),
"SecurityGroup of Test"
);
我想添加一个安全组的ID,但是好像不可能...
从查看文档开始:
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.SecurityGroup.html
如您所见,您可以将 SecurityGroup
传递给 peer
属性。
要从其 ID 创建 SecurityGroup
,请使用 SecurityGroup.fromSecurityGroupId
:
const securityGroup = new ec2.SecurityGroup(this, "Ec2SecurityGroup", {
vpc,
});
const otherSecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(
this,
"OtherSecurityGroup",
"sg-test"
);
securityGroup.addIngressRule(
otherSecurityGroup,
ec2.Port.tcp(5432),
"SecurityGroup of Test"
);