【问题标题】:How to specify source security group Id in AWS CDK?如何在 AWS CDK 中指定源安全组 ID?
【发布时间】:2020-03-27 17:20:02
【问题描述】:

您好,我正在研究 AWS CDK。我正在编写安全组模板。我可以用 Cloud Formation 来写它。现在我在 AWS CDK 中编写它。我想得到任何包含源安全组的示例。下面是我之前写的成云模板。

Resources:
  MerchWebServicesSecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      Tags:
        - Key: "Name"
          Value: !Ref "AWS::StackName"
      GroupDescription: "EC2 Services Security Group"
      VpcId:
        Fn::ImportValue: "infra-vpc-base::VpcId"
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: "80"
          ToPort: "80"
          SourceSecurityGroupId: !Ref MerchWebServicesLoadBalancerSecurityGroup
        - IpProtocol: tcp
          FromPort: "443"
          ToPort: "443"
          SourceSecurityGroupId: !Ref MerchWebServicesLoadBalancerSecurityGroup
        - IpProtocol: tcp
          FromPort: 31000
          ToPort: 65535
          SourceSecurityGroupId: !Ref MerchWebServicesLoadBalancerSecurityGroup

  MerchWebServicesLoadBalancerSecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      Tags:
        -
          Key: "Name"
          Value: !Ref "AWS::StackName"
      GroupDescription: "MerchWebServices ALB Group"
      VpcId:
        Fn::ImportValue: "infra-vpc-base::VpcId"
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: '172.30.1.0/15'

在上面的模板中,我创建了 SG MerchWebServicesSecurityGroup,并将 SourceSecurityGroupId 指定为另一个 SG MerchWebServicesLoadBalancerSecurityGroup。

        #create SG MerchWebServicesLoadBalancerSecurityGroup
        mws_vpc_sg_alb  = ec2.SecurityGroup(self,"MerchWebServicesLoadBalancerSecurityGroup",
        description = "MerchWebServices ALB Group",
        security_group_name = "MerchWebServicesLoadBalancerSecurityGroup",
        vpc= vpc);

        mws_vpc_sg_alb.add_ingress_rule(peer = ec2.Peer.ipv4('172.30.0.0/15'), connection = ec2.Port.tcp(80));

        #create SG MerchWebServicesSecurityGroup
        mws_vpc_sg = ec2.SecurityGroup(self,"MerchWebServicesSecurityGroup",
        description="EC2 Services Security Group",
        security_group_name="MerchWebServicesSecurityGroup",
        vpc = vpc);
        mws_vpc_sg.add_ingress_rule(peer = ec2.Peer.ipv4(mws_vpc_sg_alb), connection = ec2.Port.tcp(80));

在上面的代码中,我正在尝试创建 SG MerchWebServicesSecurityGroup,下面我正在添加入口规则

mws_vpc_sg.add_ingress_rule(peer = ec2.Peer.ipv4(mws_vpc_sg_alb), connection = ec2.Port.tcp(80));

在这里,我想指定 SourceSecurityGroupId,而不是指定 Cidr 块。在 AWS CDK 中,我不确定如何使用 Ref 并包含 SourceSecurityGroupId。有人可以帮我完成这个吗?任何帮助,将不胜感激。谢谢

【问题讨论】:

    标签: python amazon-web-services amazon-cloudformation aws-cdk


    【解决方案1】:

    ec2.SecurityGroup实现IPeer接口,因此安全组本身可以作为对等体。

    mws_vpc_sg_alb.add_ingress_rule(
         peer=mws_vpc_sg_alb,
         connection=ec2.Port.tcp(80),
         description='ALB access'
    )
    

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2020-06-28
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      相关资源
      最近更新 更多