How to get all EC2 instance IDs from AutoScaling?

So, I have an AWS CloudFormation template with three different types of instances (Server, Agent, Relay)

I use AutoScaling to dynamically run X number of type instances.

My problem is that I need all the IP addresses of these servers from the outputs of the template, preferably sorted by sections.

i.e.

Servers: h.h.h.x yyyy

Relay: zzzz

Agent: aaaa

How to get only instance identifiers from outputs? (I can get IP addresses from identifiers)

Attached Template:

{ "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" } } 
+4
source share
2 answers

No, you cannot set the outputs as ips. Cloud formation is responsible for autoscaling groups and autoscaling startup configurations, but it does not control individual instances of EC2, so you cannot receive information from them on outputs.

You can write something that runs on EC2 instances at startup to set the tag on the stack with ip values. But it can be difficult to maintain when instances are completed.

+3
source

In bash and using the AWS CLI utility, you can do the following:

 #!/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 '[^\"]\\+' 

This will result in the issue of instance identifiers for all machines in the auto-scaling group 'mygroup', one per line.

+3
source

All Articles