如何从AutoScaling获取所有EC2实例ID? [英] How to obtain all EC2 instance IDs from AutoScaling?

查看:62
本文介绍了如何从AutoScaling获取所有EC2实例ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个带有3种不同实例类型(服务器,代理,中继)的AWS CloudFormation模板

So I have an AWS CloudFormation template with 3 different instance 'types' (Server, Agent, Relay)

我正在使用AutoScaling动态启动X个类型的实例。

I'm using AutoScaling to dynamically launch X number of instances of a type.

我的问题是我需要模板输出中所有这些服务器的IP,最好将其分类。

My problem is that I need all of the IPs of these servers from Outputs of the template, preferably sorted into sections.

ie

服务器:
xxxx
yyyy

Servers: x.x.x.x y.y.y.y

中继:
zzzz

Relays: z.z.z.z

代理商:
aaaaa

Agents: a.a.a.a

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

How do I get just the instance Ids from the Outputs? (I can get the IPs from the IDs)

附加模板:

{
"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"
}
}


推荐答案

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

No you can't set the outputs to be the ips. The cloud formation is responsible for the autoscaling groups and the autoscaling launch configuration but it does not have control of the individual EC2 instances, so you cannot get information from them into the outputs.

您可以编写一些在启动时在EC2实例上运行的内容,以使用ip值在堆栈上设置标签。但这在实例终止时可能很难维护。

You could write something that runs on the EC2 instances at startup to set a tag on the stack with the ip values. But this could get difficult to maintain when instances terminate.

这篇关于如何从AutoScaling获取所有EC2实例ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆