amazon-web-services - 如何自动将自动扩展的 EC2 实例添加到安全组?

标签 amazon-web-services amazon-ec2 ec2-api-tools

我想设置我的 AWS 以自动扩展 EC2 实例 (Windows Server 2012)。问题是他们需要将他们的 IP 地址添加到安全组,以便他们可以与另一个 EC2 通信。

  • AWS 是否可以通过其自动缩放功能自动处理此问题? (我能找到的最接近的是为新实例分配 IAM 角色,但我不能将 IAM 角色添加到安全组,我只能添加 IP 地址。)
  • 我目前正在研究的方法是使用 AWS CLI(命令行)作为启动脚本。
    ec2-authorize mySecurityGroup -p 1433 -s xx.xx.xx.xx/32
    

  • 但是如何获取当前实例的公网IP呢?是否有 AWS CLI 命令来获取此信息?我宁愿不依赖像“curl echoip.com”这样的外部网站。我听说过 ec2-metadata,但我认为它不适用于 Windows,而且我不想使用其他第三方软件。

    最佳答案

    创建一个名为 web 的安全组。举个例子,假设该组的 id 是:sg-7aa91911

    创建一个名为 db 的安全组。
    将新规则添加到 1433 端口的 db 安全组,源为 sg-7aa91911

    创建 Autoscaling 启动配置并将 SecurityGroups 设置为 sg-7aa91911 和您需要的任何其他配置。

    使用该启动配置创建 Autoscaling 组。

    我编写了一个快速的 CloudFormation 模板来完成这项任务。您应该能够运行它,它将使用连接的安全组创建一个 Autoscaling 组。它还将创建一个空白实例,您可以在其中存储您的数据库。

    如果您不想使用 CloudFormation 模板,只需查看安全组的定义位置。它显示了如何连接 2 个安全组

    {
      "AWSTemplateFormatVersion" : "2010-09-09",
    
      "Description" : "test tempalte",
    
      "Parameters" : {
        "KeyName" : {
          "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
          "Type" : "String"
        }
      },
    
      "Mappings" : {
        "RegionMap" : {
          "us-east-1"      : { "AMI" : "ami-7f418316" },
          "us-west-1"      : { "AMI" : "ami-951945d0" },
          "us-west-2"      : { "AMI" : "ami-16fd7026" },
          "eu-west-1"      : { "AMI" : "ami-24506250" },
          "sa-east-1"      : { "AMI" : "ami-3e3be423" },
          "ap-southeast-1" : { "AMI" : "ami-74dda626" },
          "ap-northeast-1" : { "AMI" : "ami-dcfa4edd" }
        }
      },
    
      "Resources" : {
        "WebServerGroup" : {
          "Type" : "AWS::AutoScaling::AutoScalingGroup",
          "Properties" : {
            "AvailabilityZones" : { "Fn::GetAZs" : "" },
            "LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
            "MinSize" : "1",
            "MaxSize" : "10",
            "DesiredCapacity" : "1"
          }
        },
    
        "LaunchConfig" : {
          "Type" : "AWS::AutoScaling::LaunchConfiguration",
          "Properties" : {
            "InstanceType" : "m1.small",
            "KeyName" : { "Ref" : "KeyName" },
            "SecurityGroups" : [ {"Ref" : "websg"} ],
            "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]}
          }
        },
        "Ec2Instance" : {
          "Type" : "AWS::EC2::Instance",
          "Properties" : {
            "KeyName" : { "Ref" : "KeyName" },
            "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
            "UserData" : { "Fn::Base64" : "80" }
          }
        },
    
        "websg" : {
          "Type" : "AWS::EC2::SecurityGroup",
          "Properties" : {
            "GroupDescription" : "Enable SSH and access, 8080, and 80",
            "SecurityGroupIngress" : [
              {"IpProtocol" : "tcp", "FromPort" : "8080", "ToPort" : "8080", "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"}
            ]
          }
        },
        "dbsg" : {
          "Type" : "AWS::EC2::SecurityGroup",
          "Properties" : {
            "GroupDescription" : "Port opened only to security group",
            "SecurityGroupIngress" : [
              {"IpProtocol" : "tcp", "FromPort" : "1433", "ToPort" : "1433", "SourceSecurityGroupName" : {"Ref" : "websg"}
              }
            ]
          }
        }
      }
    }
    

    关于amazon-web-services - 如何自动将自动扩展的 EC2 实例添加到安全组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006875/

    相关文章:

    hadoop - 用于EMR的hi1.4xlarge SSD EC2实例

    ubuntu - 亚马逊网络服务和 EC2 和 Ubuntu 10.04

    amazon-web-services - 如何在 serverless.yml 默认值运算符中使用 `!GetAtt`?

    amazon-s3 - Zencoder 的 CloudFront 签名 URL

    ubuntu - 如何调试崩溃的 memcached?

    amazon-web-services - 使用 EC2 命令行工具选择免费层级 Amazon 系统镜像 (AMI)

    scripting - 如何测试无需实际创建ec2实例的脚本?

    amazon-web-services - 查询 EC2 实例类型属性

    php - 签名版本 4 签名过程在 PHP 中访问 API 网关端点

    amazon-ec2 - 如何通过一次登录管理多个 AWS 账户