问题
So I have an AWS CloudFormation template with 3 different instance 'types' (Server, Agent, Relay)
I'm using AutoScaling to dynamically launch X number of instances of a type.
My problem is that I need all of the IPs of these servers from Outputs of the template, preferably sorted into sections.
i.e.
Servers: x.x.x.x y.y.y.y
Relays: z.z.z.z
Agents: a.a.a.a
How do I get just the instance Ids from the Outputs? (I can get the IPs from the IDs)
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"
}
}
回答1:
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.
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.
回答2:
On bash and using the AWS CLI utility you could 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 '[^\"]\\+'
That will output the instance IDs of all the machines in the 'mygroup' auto scaling group, one per line.
来源:https://stackoverflow.com/questions/15349187/how-to-obtain-all-ec2-instance-ids-from-autoscaling