如何从AutoScaling获取所有EC2实例的ID?

6

我有一个AWS CloudFormation模板,其中包含3种不同的实例“类型”(服务器、代理和中继)。

我正在使用自动扩展来动态启动某一类型的X个实例。

我的问题是,我需要从模板的输出中获取所有这些服务器的IP地址,最好按部分排序。

即:

服务器: x.x.x.x y.y.y.y

中继: z.z.z.z

代理: a.a.a.a

如何只从输出中获取实例ID?(我可以从ID获取IP地址)

附带的模板:

{
"AWSTemplateFormatVersion" : "2010-09-09",

"Description" : "uDeploy Agent-Relay-Server",

"Parameters" : {
    "keyName" : {
        "Description" : "SSH key to enable access on the servers",
        "Type" : "String",
        "Default" : "nick-portal"
    },

    "ServerInstanceCount" : {
        "Description" : "Number of Servers to start",
        "Type" : "Number",
        "Default" : "1"
    },
    "RelayInstanceCount" : {
        "Description" : "Number of Agent Relays to start",
        "Type" : "Number",
        "Default" : "2"
    },
    "AgentInstanceCount" : {
        "Description" : "Number of Agents to start",
        "Type" : "Number",
        "Default" : "4"
    },

    "ServerAMI" : {
        "Description" : "",
        "Type" : "String",
        "Default" : "ami-7539b41c"
    },
    "RelayAMI" : {
        "Description" : "",
        "Type" : "String",
        "Default" : "ami-7539b41c"
    },
    "AgentAMI" : {
        "Description" : "",
        "Type" : "String",
        "Default" : "ami-7539b41c"
    },

    "ServerUserData" : {
        "Description" : "",
        "Type" : "String",
        "Default" : "#!/bin/bash"
    },
    "RelayUserData" : {
        "Description" : "",
        "Type" : "String",
        "Default" : "#!/bin/bash"
    },
    "AgentUserData" : {
        "Description" : "",
        "Type" : "String",
        "Default" : "#!/bin/bash"
    },
    "Zone" : {
        "Description" : "",
        "Type" : "String",
        "Default" : "us-east-1d"
    }
},

"Resources" : {
    "ServerLaunchConfig" : { 
        "Type" : "AWS::AutoScaling::LaunchConfiguration",
        "Properties" : {
            "KeyName" : { "Ref" : "keyName" },
            "ImageId" : { "Ref" : "ServerAMI" },
            "UserData" : { "Fn::Base64" : { "Ref" : "ServerUserData" } },
            "SecurityGroups" : [ { "Ref" : "ServerSecurityGroup" }, { "Ref" : "SshSecurityGroup" } ],
            "InstanceType" : "t1.micro"
        }
    },
    "RelayLaunchConfig" : { 
        "Type" : "AWS::AutoScaling::LaunchConfiguration",
        "Properties" : {
            "KeyName" : { "Ref" : "keyName" },
            "ImageId" : { "Ref" : "RelayAMI" },
            "UserData" : { "Fn::Base64" : { "Ref" : "RelayUserData" } },
            "SecurityGroups" : [ { "Ref" : "RelaySecurityGroup" }, { "Ref" : "SshSecurityGroup" } ],
            "InstanceType" : "t1.micro"
        }
    },
    "AgentLaunchConfig" : { 
        "Type" : "AWS::AutoScaling::LaunchConfiguration",
        "Properties" : {
            "KeyName" : { "Ref" : "keyName" },
            "ImageId" : { "Ref" : "AgentAMI" },
            "UserData" : { "Fn::Base64" : { "Ref" : "AgentUserData" } },
            "SecurityGroups" : [ { "Ref" : "AgentSecurityGroup" }, { "Ref" : "SshSecurityGroup" } ],
            "InstanceType" : "t1.micro"
        }
    },


    "ServerAutoScalingGroup" : {
        "Type" : "AWS::AutoScaling::AutoScalingGroup",
        "Properties" : {
            "AvailabilityZones" : [ { "Ref" : "Zone" } ],
            "LaunchConfigurationName" : { "Ref" : "ServerLaunchConfig" },
            "MinSize" : { "Ref" : "ServerInstanceCount" },
            "MaxSize" : { "Ref" : "ServerInstanceCount" }
        }
    },
    "RelayAutoScalingGroup" : {
        "Type" : "AWS::AutoScaling::AutoScalingGroup",
        "Properties" : {
            "AvailabilityZones" : [ { "Ref" : "Zone" } ],
            "LaunchConfigurationName" : { "Ref" : "RelayLaunchConfig" },
            "MinSize" : { "Ref" : "RelayInstanceCount" },
            "MaxSize" : { "Ref" : "RelayInstanceCount" }
        }
    },
    "AgentAutoScalingGroup" : {
        "Type" : "AWS::AutoScaling::AutoScalingGroup",
        "Properties" : {
            "AvailabilityZones" : [ { "Ref" : "Zone" } ],
            "LaunchConfigurationName" : { "Ref" : "AgentLaunchConfig" },
            "MinSize" : { "Ref" : "AgentInstanceCount" },
            "MaxSize" : { "Ref" : "AgentInstanceCount" }
        }
    },

    "RelaySecurityGroup" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : {
            "GroupDescription" : "Enable inbound 20080 and 7916 from Agents",
            "SecurityGroupIngress" : 
            [
                { 
                    "IpProtocol" : "tcp", 
                    "FromPort" : "20080", 
                    "ToPort" : "20080", 
                    "SourceSecurityGroupName" : { "Ref" : "AgentSecurityGroup" } 
                },
                { 
                    "IpProtocol" : "tcp", 
                    "FromPort" : "7916", 
                    "ToPort" : "7916", 
                    "SourceSecurityGroupName" : { "Ref" : "AgentSecurityGroup" } 
                }
            ]
        }
    },
    "ServerSecurityGroup" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : {
            "GroupDescription" : "Enable inbound 8080 all and 7918 from Relays",
            "SecurityGroupIngress" : [
                { 
                    "IpProtocol" : "tcp", 
                    "FromPort" : "7918", 
                    "ToPort" : "7918", 
                    "SourceSecurityGroupName" : { "Ref" : "RelaySecurityGroup" } 
                },
                { 
                    "IpProtocol" : "tcp", 
                    "FromPort" : "8080", 
                    "ToPort" : "8080", 
                    "CidrIp" : "0.0.0.0/0" 
                }
            ]
        }
    },
    "AgentSecurityGroup" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : {
            "GroupDescription" : "Enable no inbound",
            "SecurityGroupIngress" : []
        }
    },
    "SshSecurityGroup" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : {
            "GroupDescription" : "Enable SSH from all",
            "SecurityGroupIngress" : [
                {
                    "IpProtocol" : "tcp",
                    "FromPort" : "22",
                    "ToPort" : "22",
                    "CidrIp" : "0.0.0.0/0"
                }
            ]
        }
    }


},

"Outputs" : {
    "Ip"
}
}
2个回答

4

在bash中使用AWS CLI工具,您可以执行以下操作:

#!/bin/bash
AUTOSCALING_GROUP="mygroup"
aws ec2 describe-instances --filters \
  "Name=tag:aws:autoscaling:groupName,Values=$AUTOSCALING_GROUP" \
  "Name=instance-state-name,Values=running" | \
grep -o '\"i-[0-9a-f]\\+\"' | grep -o '[^\"]\\+'

这将输出“mygroup”自动缩放组中所有计算机的实例ID,每行一个。


3

不,你不能将输出设置为IP。云形成负责自动伸缩组和自动伸缩启动配置,但它无法控制单个EC2实例,因此您无法从中获取信息并输出。

您可以编写一些在启动时运行的内容,以在堆栈上设置标记以包含IP值。但是,当实例终止时,这可能会变得难以维护。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接