Question

I am trying to connect a powershell script to an Auto Scaling Group via the .NET Amazon API.

I have checked the documentation here, but I am struggling to get an object that contains the IP addresses of the instances belonging to the Auto Scaling Group.

I am not sure which class to use, or which class contains my object.

I am currently using:

$request = New-Object -TypeName Amazon.AutoScaling.Model.DescribeAutoScalingInstancesRequest

Has anyone come across the same situation? Which class/object contains the IP addresses of the instances running in the AutoScaling group?

Was it helpful?

Solution

Expanding on the answer above - you may want to include the name of the auto scaling group so you don't get every instance from every group. Also, if you're using VPC your instances may not have public IPs so you'll be after the private IPs like this

Get-ASAutoScalingInstance | ? {$_.AutoScalingGroupName -eq "web-autoscaler-group"} | select -ExpandProperty InstanceId | Get-EC2Instance | select -ExpandProperty RunningInstance | ft InstanceId, PrivateIpAddress

OTHER TIPS

Using the SDK approach you take the set of instance ids embedded in the response/result data returned from the DescribeAutoScalingInstances call and pass them to the DescribeInstances call for EC2 (using new-object again to get an EC2 client and request objects). This will net you a collection of Amazon.EC2.Model.Reservation objects (again inside the response/result data) from which the RunningInstance collection inside each reservation will get you the ip address(es) for the EC2 instance.

It is however much simpler to use the AWS Tools for Windows PowerShell like this:

Get-ASAutoScalingInstance | select -expandproperty InstanceId | Get-EC2Instance | select -expandproperty RunningInstance | ft InstanceId, IpAddress

Get-ASAutoScalingInstance maps to the request in your question; this yields the set of EC2 instances from which we extract the id of each instance with a select. We then request details for the instance using Get-EC2Instance; as noted above this yields an Amazon.EC2.Model.Reservation object, within with are the details of the instance (in the RunningInstance collection). We flatten this to pull out the instance id and associated ip address for the table.

The sample pipeline above assumes you've set credentials and region to use in the shell using Set-AWSCredentials and Set-DefaultAWSRegion.

The AWS Tools for Windows PowerShell are included with the download msi for the SDK and Visual Studio toolkit available here http://aws.amazon.com/net/.

Hope this helps.

For anyone looking to do this via aws-cli

replace my autoscale group

aws --output text --query "Reservations[*].Instances[*].PublicIpAddress" ec2 describe-instances --instance-ids `aws --output text --query "AutoScalingGroups[0].Instances[*].InstanceId" autoscaling describe-auto-scaling-groups --auto-scaling-group-names "my autoscale group"`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top