[英]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"
);
I want to add an ID of security group but it seems like it's impossible...我想添加一个安全组的 ID,但似乎不可能...
Start by looking at the documentation:首先查看文档:
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.SecurityGroup.html https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.SecurityGroup.html
As yoou can see, you can pass a SecurityGroup
to the peer
attribute.如您所见,您可以将
SecurityGroup
传递给peer
属性。
To create a SecurityGroup
from its ID, use SecurityGroup.fromSecurityGroupId
:要从其 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"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.