예제 #1
0
func getInstancesWithTag(ec2Client *ec2.EC2, key string, value string) ([]*ec2.Instance, error) {
	var instances []*ec2.Instance

	err := ec2Client.DescribeInstancesPages(&ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			&ec2.Filter{
				Name:   aws.String("instance-state-name"),
				Values: []*string{aws.String("running")},
			},
			&ec2.Filter{
				Name:   aws.String(fmt.Sprintf("tag:%s", key)),
				Values: []*string{aws.String(value)},
			},
		},
	}, func(result *ec2.DescribeInstancesOutput, _ bool) bool {
		for _, reservation := range result.Reservations {
			instances = append(instances, reservation.Instances...)
		}
		return true // keep going
	})
	if err != nil {
		return nil, err
	}

	return instances, nil
}
예제 #2
0
func processInstances(svc *ec2.EC2, pageSize *int64, apply func([]*string)) {
	err := svc.DescribeInstancesPages(&ec2.DescribeInstancesInput{
		MaxResults: pageSize,
	},
		func(instancesOut *ec2.DescribeInstancesOutput, lastPage bool) bool {
			var instanceIds []*string
			for idx := range instancesOut.Reservations {
				for _, instance := range instancesOut.Reservations[idx].Instances {
					instanceIds = append(instanceIds, instance.InstanceId)
				}
			}

			apply(instanceIds)

			return !lastPage
		})

	kingpin.FatalIfError(err, "Could not retrieve EC2 instances")
}