简体   繁体   English

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"
);

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.

相关问题 使用CDK时如何将服务的安全组设置为默认安全组 - How do I set the the security group of a service to the default security group when using CDK 允许使用 AWS CDK 从一个安全组进入另一个安全组 - Allow ingress from one security group to another using AWS CDK CDK Fargate 部署正在向安全组添加“允许任何人”入站规则 - CDK Fargate deploy is adding a "allow anyone" inbound rule to security group 如何使用 CDK 将 AWS ECS (ApplicationLoadBalancedFargateService) 私有 IP 连接到 RDS 安全组 - How to Connect AWS ECS (ApplicationLoadBalancedFargateService) Private IP to RDS Security Group Using CDK 如何使用 VPC 默认安全组作为另一个安全组的对等点? - How to use a VPC default security group as a peer for another security group? AWS CDK - 不能在构造 ID 中使用令牌 - 如何根据其他构造的名称动态命名构造? - AWS CDK - Cannot use tokens in construct ID - How do I dynamically name constructs based off the names of other constructs? 如果有重复,我如何按日期分组并添加计数? - How do I group by date and add a count if there are duplicates? 如何使用 cdk 获取帐户 ID - How to get the account id with cdk 如何在 cdk 中将现有密钥与自动缩放组一起使用以启用 ssh 访问 - How can use an existing secret with an autoscaling group in cdk to enable ssh access Angular如何将表单组添加到TR组件? - Angular how to add a form group to a TR component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM