代码之家  ›  专栏  ›  技术社区  ›  Jeet

安全组和子网属于不同的网络

  •  14
  • Jeet  · 技术社区  · 6 年前

    我正在创建一个基本的AWS云信息模板,其中包含一个VPC、3个安全组和5个EC2实例我的安全组如下所示-

    {
      "WebApplicationServerSG": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
          "VpcId": {
            "Ref": "DevVpc"
          },
          "GroupDescription": "Enable HTTP, HTTPS and SSH access",
          "Tags": [
            {
              "Key": "Name",
              "Value": "WebApplicationServer Service Group"
            }
          ],
          "SecurityGroupIngress": [
            {
              "IpProtocol": "tcp",
              "FromPort": "443",
              "ToPort": "443",
              "CidrIp": "0.0.0.0/0"
            },
            {
              "IpProtocol": "tcp",
              "FromPort": "80",
              "ToPort": "80",
              "CidrIp": "0.0.0.0/0"
            },
            {
              "IpProtocol": "tcp",
              "FromPort": "22",
              "ToPort": "22",
              "CidrIp": "0.0.0.0/0"
            }
          ],
          "SecurityGroupEgress": [
            {
              "IpProtocol": "tcp",
              "FromPort": "443",
              "ToPort": "443",
              "CidrIp": "0.0.0.0/0"
            },
            {
              "IpProtocol": "tcp",
              "FromPort": "80",
              "ToPort": "80",
              "CidrIp": "0.0.0.0/0"
            },
            {
              "IpProtocol": "tcp",
              "FromPort": "22",
              "ToPort": "22",
              "CidrIp": "0.0.0.0/0"
            }
          ]
        },
        "Metadata": {
          "AWS::CloudFormation::Designer": {
            "id": "a7977f00-48d6-488f-9e23-9bcd0785d399"
          }
        }
      }
    }
    

    VPC如下所示-

    {
      "DevVpc": {
        "Type": "AWS::EC2::VPC",
        "Properties": {
          "CidrBlock": "172.31.0.0/16",
          "EnableDnsSupport": "false",
          "EnableDnsHostnames": "false",
          "InstanceTenancy": "dedicated",
          "Tags": [
            {
              "Key": "Name",
              "Value": "DevStackVpc"
            }
          ]
        }
      }
    }
    

    使用模板创建堆栈时出错-

    安全组sg-31f91b5a和子网ea0aa3a7属于 不同的网络。

    11:13:01 UTC+0550   CREATE_FAILED   AWS::EC2::Instance  WebApplicationServer    Security group sg-5147a53a and subnet subnet-ea0aa3a7 belong to different networks.
    

    这是一个 gist 对于完整的模板,任何帮助都将不胜感激。

    3 回复  |  直到 6 年前
        1
  •  13
  •   timothyclifford    5 年前

    如果有任何使用Terraform的人来到这里,我会收到一条类似的错误消息,结果如下:

    variable "name" {}
    
    locals {
      vpc_id    = "..."
      subnet_id = "..."
    }
    
    resource "aws_instance" "web" {
      ami                         = "ami-09def150731bdbcc2"
      instance_type               = "t3.micro"
      vpc_security_group_ids      = ["${aws_security_group.allow_http.id}"]
    
      user_data = <<-EOF
        #!/bin/bash
        sudo amazon-linux-extras install nginx1.12 -y
        sudo nginx
      EOF
    
      tags {
        Name = "${var.name}"
      }
    }
    
    resource "aws_security_group" "allow_http" {
      description = "Allow inbound HTTP traffic for ${var.name} instance"
      vpc_id      = "${local.vpc_id}"
    
      ingress {
        from_port   = 80
        to_port     = 80
        protocol    = "TCP"
        cidr_blocks = ["0.0.0.0/0"]
      }
    
      egress {
        from_port   = 0
        to_port     = 0
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
      }
    }
    

    我部署到的子网没有 auto assign public IPs 启用。因此,我更新了 aws_instance 包括 subnet_id associate_public_ip_address :

    resource "aws_instance" "web" {
      ami                         = "ami-09def150731bdbcc2"
      instance_type               = "t3.micro"
      subnet_id                   = "${local.subnet_id}"
      vpc_security_group_ids      = ["${aws_security_group.allow_http.id}"]
      associate_public_ip_address = true
    
      user_data = <<-EOF
        #!/bin/bash
        sudo amazon-linux-extras install nginx1.12 -y
        sudo nginx
      EOF
    
      tags {
        Name = "${var.name}"
      }
    }
    

    之后,一切顺利。

        2
  •  11
  •   Jeet    6 年前

    我通过注释中提供的指针解决了上述问题 subnet VPC , Security-Groups EC2 实例如下-

    首先要做的是 专有网络 第二个是 Subnet 这里你提到 VpcId 您之前创建的 您创建的第三个 security groups 这里你提到 VpcId 您之前也创建了。 第四,有一处房产 NetworkInterfaces 您提供的位置 SubnetId GroupSet 这是一个安全组ID数组,在这里定义安全组、专有网络和子网之间的关系,这就是解决问题的方法。

    下面是实际工作的示例模板-

    {
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "DevServerKeyPair": {
            "Description": "Name of an existing EC2 KeyPair to enable SSH access to the instance",
            "Type": "AWS::EC2::KeyPair::KeyName",
            "ConstraintDescription": "Must be the name of an existing EC2 KeyPair."
        }
    },
    "Resources": {
        "DevVpc": {
            "Type": "AWS::EC2::VPC",
            "Properties": {
                "CidrBlock": "172.31.0.0/16",
                "EnableDnsSupport": "false",
                "EnableDnsHostnames": "false",
                "InstanceTenancy": "dedicated",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "DevStackVpc"
                    }
                ]
            }
        },
        "DevSubnet": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "VpcId": {
                    "Ref": "DevVpc"
                },
                "CidrBlock": "172.31.0.0/16",
                "AvailabilityZone": {
                    "Fn::Select": [
                        0,
                        {
                            "Fn::GetAZs": ""
                        }
                    ]
                }
            }
        },
        "WebApplicationServerSG": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "VpcId": {
                    "Ref": "DevVpc"
                },
                "GroupDescription": "Enable HTTP, HTTPS and SSH access",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "WebApplicationServer Service Group"
                    }
                ],
                "SecurityGroupIngress": [
                    {
                        "IpProtocol": "tcp",
                        "FromPort": "443",
                        "ToPort": "443",
                        "CidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "FromPort": "80",
                        "ToPort": "80",
                        "CidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "FromPort": "22",
                        "ToPort": "22",
                        "CidrIp": "0.0.0.0/0"
                    }
                ],
                "SecurityGroupEgress": [
                    {
                        "IpProtocol": "tcp",
                        "FromPort": "443",
                        "ToPort": "443",
                        "CidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "FromPort": "80",
                        "ToPort": "80",
                        "CidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "FromPort": "22",
                        "ToPort": "22",
                        "CidrIp": "0.0.0.0/0"
                    }
                ]
            }
        },
        "WebApplicationServer": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "ImageId": "ami-f3e5aa9c",
                "InstanceType": "t2.micro",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "WebApplicationServer"
                    }
                ],
                "KeyName": {
                    "Ref": "DevServerKeyPair"
                },
                "NetworkInterfaces": [
                    {
                        "SubnetId": {"Ref": "DevSubnet"},
                        "AssociatePublicIpAddress": "true",
                        "DeviceIndex": "0",
                        "GroupSet": [{ "Ref" : "WebApplicationServerSG" }]
                    }
                ]
            }
        }
      }
    }
    

    希望这对研究类似问题的人有所帮助。

        3
  •  3
  •   Tkachenko Vlad    4 年前

    您尝试使用的安全组存在问题!使用模板创建时,它使用默认专有网络。 在创建安全组的CLoudFormation模板上,您需要识别您喜欢使用的VpcId(非默认),这将解决问题。或者,您可以使用(非默认)VPC手动创建新的安全组,然后运行新实例。