当前位置: 首页>>代码示例>>Golang>>正文


Golang EC2.DescribeInstances方法代码示例

本文整理汇总了Golang中github.com/aws/aws-sdk-go/service/ec2.EC2.DescribeInstances方法的典型用法代码示例。如果您正苦于以下问题:Golang EC2.DescribeInstances方法的具体用法?Golang EC2.DescribeInstances怎么用?Golang EC2.DescribeInstances使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/aws/aws-sdk-go/service/ec2.EC2的用法示例。


在下文中一共展示了EC2.DescribeInstances方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: getInstanceByID

// Returns a single insance when given it's ID
func getInstanceByID(instanceid string, ec2client *ec2.EC2) (ec2.Instance, error) {
	instanceReq := ec2.DescribeInstancesInput{
		InstanceIds: []*string{
			aws.String(instanceid),
		},
	}

	instanceResp, err := ec2client.DescribeInstances(&instanceReq)
	if err != nil {
		return ec2.Instance{}, err
	}

	//We only requested one instance, so we should only get one
	if len(instanceResp.Reservations) != 1 {
		return ec2.Instance{}, errors.New("The total number of reservations did not match the request")
	}
	reservation := instanceResp.Reservations[0]

	// Now let's make sure we only got one instance in this reservation
	if len(reservation.Instances) != 1 {
		return ec2.Instance{}, errors.New("The total number of instances did not match the request for full instance data")
	}

	instance := reservation.Instances[0]
	return *instance, nil
}
开发者ID:roo7break,项目名称:cracklord,代码行数:27,代码来源:aws-api.go

示例2: findInstance

func findInstance(ec2HostConfig *config.Ec2HostConfig, svc *ec2.EC2) (*ec2.Instance, error) {
	region, name := ec2HostConfig.Region, ec2HostConfig.Name

	log.Printf("Searching for instance named %q in region %s", name, region)

	params := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			{
				Name: aws.String("tag:Name"),
				Values: []*string{
					aws.String(ec2HostConfig.Name),
				},
			},
		},
	}
	resp, err := svc.DescribeInstances(params)
	if err != nil {
		return nil, err
	}

	if len(resp.Reservations) < 1 {
		return nil, fmt.Errorf("no instance found in %s with name %s", region, name)
	}
	if len(resp.Reservations) > 1 || len(resp.Reservations[0].Instances) != 1 {
		return nil, fmt.Errorf("multiple instances found in %s with name %s", region, name)
	}
	return resp.Reservations[0].Instances[0], nil
}
开发者ID:djmitche,项目名称:proj,代码行数:28,代码来源:ec2.go

示例3: waitForDetachedNetwork

func waitForDetachedNetwork(svc *ec2.EC2, instanceid *string) error {
	iids := []*string{instanceid}
	dii := &ec2.DescribeInstancesInput{
		InstanceIds: iids,
	}

	dio, err := svc.DescribeInstances(dii)
	if err != nil {
		panic(err)
	}

	//fmt.Println(dio)
	count := 0
	for {
		dio, err = svc.DescribeInstances(dii)
		if err != nil {
			panic(err)
		}

		if len(dio.Reservations[0].Instances[0].NetworkInterfaces) == 0 {
			return nil
		}

		if count > 60 {
			break
		}

		time.Sleep(2 * time.Second)
		count++
	}

	return errors.New(fmt.Sprintf("Waited too long for EC2 to remove networking."))

}
开发者ID:jamesunger,项目名称:saws,代码行数:34,代码来源:saws.go

示例4: GetContainerInstances

// GetContainerInstances will return a slice of ECS Container Instances within a cluster
func GetContainerInstances(svc *ecs.ECS, ec2svc *ec2.EC2, cluster *string) (containerInstances []*ecs.ContainerInstance, instances []*ec2.Reservation, e error) {

	var ciArns []*string

	// List clusters
	reqParams := &ecs.ListContainerInstancesInput{
		Cluster:    cluster,
		MaxResults: aws.Int64(100),
		NextToken:  aws.String(""),
	}

	// Loop through tokens until no more results remain
	for {
		resp, err := svc.ListContainerInstances(reqParams)

		// Error check
		if err != nil {
			return nil, nil, fmt.Errorf("ecs.ListContainerInstances: %s", err.Error())
		}

		// Expand slice of container instances and append to our comprehensive list
		ciArns = append(ciArns, resp.ContainerInstanceArns...)

		// Cycle token
		if resp.NextToken != nil {
			reqParams.NextToken = resp.NextToken
		} else {
			// Kill loop ... out of response pages
			break
		}

	}

	// Describe the tasks that we just got back
	ciResponse, err := svc.DescribeContainerInstances(&ecs.DescribeContainerInstancesInput{
		Cluster:            cluster,
		ContainerInstances: ciArns,
	})

	if err != nil {
		return nil, nil, fmt.Errorf("ecs.DescribeContainerInstances: %s", err.Error())
	}

	var instanceIds []*string
	for _, k := range ciResponse.ContainerInstances {
		instanceIds = append(instanceIds, k.Ec2InstanceId)
	}

	// Create a map of container instances by ci arn...
	// Note: Will work for <= 1000 instances w/o having to use NextToken
	ec2Resp, err := ec2svc.DescribeInstances(&ec2.DescribeInstancesInput{
		InstanceIds: instanceIds,
	})

	if err != nil {
		return nil, nil, fmt.Errorf("ec2.DescribeInstances: %s", err.Error())
	}

	return ciResponse.ContainerInstances, ec2Resp.Reservations, nil
}
开发者ID:colinmutter,项目名称:go-ecs,代码行数:61,代码来源:ecs.go

示例5: getPrivateIPs

func getPrivateIPs(ec2region *ec2.EC2) []string {
	instances := []string{}
	filters, err := buildFilter(*tags)
	if err != nil {
		log.Println(err)
	}

	request := ec2.DescribeInstancesInput{Filters: filters}
	result, err := ec2region.DescribeInstances(&request)
	if err != nil {
		log.Println(err)
	}

	for _, reservation := range result.Reservations {
		for _, instance := range reservation.Instances {
			if instance.PrivateIpAddress != nil {
				if *debug {
					log.Printf("Adding %s to IP list\n", *instance.PrivateIpAddress)
				}
				instances = append(instances, *instance.PrivateIpAddress)
			}
		}
	}

	return instances
}
开发者ID:BashtonLtd,项目名称:varnish-purge-proxy,代码行数:26,代码来源:varnish-purge-proxy.go

示例6: GetEc2PrivateIpsFromInstanceIds

func GetEc2PrivateIpsFromInstanceIds(ec2_obj *ec2.EC2, instance_ids []string, debug bool) ([]string, error) {
	params := &ec2.DescribeInstancesInput{
		InstanceIds: aws.StringSlice(instance_ids),
	}
	resp, err := ec2_obj.DescribeInstances(params)

	if err != nil {
		return []string{}, fmt.Errorf("Cannot retrieve EC2 instance information: %s", FormatAwsError(err))
	}

	if len(resp.Reservations) <= 0 {
		return []string{}, fmt.Errorf("No EC2 instances found (Reservations.*) with specified Instance IDs filter: ", strings.Join(instance_ids, ", "))
	}
	if len(resp.Reservations[0].Instances) <= 0 {
		return []string{}, fmt.Errorf("No EC2 instances found (Reservations[0].* with specified Instance IDs filter: ", strings.Join(instance_ids, ", "))
	}

	var result []string
	for idx, _ := range resp.Reservations {
		for _, value := range resp.Reservations[idx].Instances {
			if *value.State.Name == "running" {
				result = append(result, *value.PrivateIpAddress)
			} else {
				if debug == true {
					fmt.Println(*value.InstanceId, "is not in a running state, excluded from results.")
				}
			}
		}
	}

	if len(result) == 0 {
		return []string{}, fmt.Errorf("No running EC2 instances found in result set, cannot proceed.")
	}
	return result, nil
}
开发者ID:CpuID,项目名称:ecs-discoverer,代码行数:35,代码来源:shared.go

示例7: InstanceStateRefreshFunc

// InstanceStateRefreshFunc returns a StateRefreshFunc that is used to watch
// an EC2 instance.
func InstanceStateRefreshFunc(conn *ec2.EC2, instanceId string) StateRefreshFunc {
	return func() (interface{}, string, error) {
		resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
			InstanceIDs: []*string{&instanceId},
		})
		if err != nil {
			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
				// Set this to nil as if we didn't find anything.
				resp = nil
			} else if isTransientNetworkError(err) {
				// Transient network error, treat it as if we didn't find anything
				resp = nil
			} else {
				log.Printf("Error on InstanceStateRefresh: %s", err)
				return nil, "", err
			}
		}

		if resp == nil || len(resp.Reservations) == 0 || len(resp.Reservations[0].Instances) == 0 {
			// Sometimes AWS just has consistency issues and doesn't see
			// our instance yet. Return an empty state.
			return nil, "", nil
		}

		i := resp.Reservations[0].Instances[0]
		return i, *i.State.Name, nil
	}
}
开发者ID:jamesleech,项目名称:packer,代码行数:30,代码来源:state.go

示例8: detectRouteTableID

func (be *AwsVpcBackend) detectRouteTableID(instanceID string, ec2c *ec2.EC2) (string, error) {
	instancesInput := &ec2.DescribeInstancesInput{
		InstanceIds: []*string{&instanceID},
	}

	resp, err := ec2c.DescribeInstances(instancesInput)
	if err != nil {
		return "", fmt.Errorf("error getting instance info: %v", err)
	}

	if len(resp.Reservations) == 0 {
		return "", fmt.Errorf("no reservations found")
	}

	if len(resp.Reservations[0].Instances) == 0 {
		return "", fmt.Errorf("no matching instance found with id: %v", instanceID)
	}

	subnetID := resp.Reservations[0].Instances[0].SubnetId
	vpcID := resp.Reservations[0].Instances[0].VpcId

	log.Info("Subnet-ID: ", *subnetID)
	log.Info("VPC-ID: ", *vpcID)

	filter := newFilter()
	filter.Add("association.subnet-id", *subnetID)

	routeTablesInput := &ec2.DescribeRouteTablesInput{
		Filters: filter,
	}

	res, err := ec2c.DescribeRouteTables(routeTablesInput)
	if err != nil {
		return "", fmt.Errorf("error describing routeTables for subnetID %s: %v", *subnetID, err)
	}

	if len(res.RouteTables) != 0 {
		return *res.RouteTables[0].RouteTableId, nil
	}

	filter = newFilter()
	filter.Add("association.main", "true")
	filter.Add("vpc-id", *vpcID)

	routeTablesInput = &ec2.DescribeRouteTablesInput{
		Filters: filter,
	}

	res, err = ec2c.DescribeRouteTables(routeTablesInput)
	if err != nil {
		log.Info("error describing route tables: ", err)
	}

	if len(res.RouteTables) == 0 {
		return "", fmt.Errorf("main route table not found")
	}

	return *res.RouteTables[0].RouteTableId, nil
}
开发者ID:luxas,项目名称:flannel,代码行数:59,代码来源:awsvpc.go

示例9: ec2connect

func ec2connect(svc *ec2.EC2) (*ec2.DescribeInstancesOutput, error) {
	logger.Info("Attempting to connect...")
	resp, err := svc.DescribeInstances(nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
开发者ID:lenfree,项目名称:awsClient,代码行数:8,代码来源:connect.go

示例10: getInstance

func (e *EC2) getInstance(id string, svc *ec2.EC2) (*ec2.Instance, error) {
	resp, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{
		InstanceIds: []*string{aws.String(id)},
	})
	if err != nil {
		return nil, err
	} else if len(resp.Reservations) != 1 || len(resp.Reservations[0].Instances) != 1 {
		return nil, fmt.Errorf("DescribeInstances did not return one reservation with one instance")
	} else {
		return resp.Reservations[0].Instances[0], nil
	}
}
开发者ID:LunaNode,项目名称:cloug,代码行数:12,代码来源:ec2.go

示例11: awsGetInstances

func awsGetInstances(ecc ec2.EC2) ([]*ec2.Instance, error) {
	instancelist := []*ec2.Instance{}
	resp, err := ecc.DescribeInstances(nil)
	if err != nil {
		return nil, fmt.Errorf("unable to describe EC2 Instances")
	}
	for _, reservation := range resp.Reservations {
		for _, instance := range reservation.Instances {
			instancelist = append(instancelist, instance)
		}
	}
	return instancelist, nil
}
开发者ID:nicollet,项目名称:bosun,代码行数:13,代码来源:aws.go

示例12: privateIps

func privateIps(ec2 *ec2pkg.EC2, ids []*string) []*string {
	output, err := ec2.DescribeInstances(&ec2pkg.DescribeInstancesInput{
		InstanceIds: ids,
	})
	log.Check(err)
	var ips []*string
	for _, r := range output.Reservations {
		for _, i := range r.Instances {
			if i.PrivateIpAddress != nil {
				ips = append(ips, i.PrivateIpAddress)
			}
		}
	}
	return ips
}
开发者ID:travisjeffery,项目名称:ecs-exec,代码行数:15,代码来源:exec.go

示例13: GetVolumeId

func GetVolumeId(i string, c *ec2.EC2) *string {

	opts := &ec2.DescribeInstancesInput{
		DryRun: aws.Bool(false),
		InstanceIds: []*string{
			aws.String(i),
		},
	}
	instanceStatus, err := c.DescribeInstances(opts)
	if err != nil {
		fmt.Println("Error Occured :", err.Error())
		os.Exit(1)
	}
	return instanceStatus.Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId
}
开发者ID:kumarsarath588,项目名称:Golang,代码行数:15,代码来源:main.go

示例14: DescribeInstance

// DescribeInstance gets an Instance object from AWS and returns it, stripping
// out Reservation data.
func DescribeInstance(c *ec2.EC2, i *string) (*ec2.Instance, error) {
	q := &ec2.DescribeInstancesInput{
		InstanceIds: []*string{i},
	}

	resp, err := c.DescribeInstances(q)
	if err != nil {
		return nil, err
	}

	if len(resp.Reservations[0].Instances) != 1 {
		return nil, errors.New("Found more than one instance. Bailing")
	}

	return resp.Reservations[0].Instances[0], nil
}
开发者ID:fluxrad,项目名称:ec2-tag-volumes,代码行数:18,代码来源:main.go

示例15: getVpcIDFromInstanceID

func getVpcIDFromInstanceID(svc *ec2.EC2, s string) {
	params := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			{
				Name:   aws.String("instance-id"),
				Values: []*string{aws.String(s)},
			},
		},
	}
	resp, err := svc.DescribeInstances(params)
	if err != nil {
		panic(err)
	}
	if resp.Reservations[0].Instances[0].VpcId != nil {
		fmt.Println(*resp.Reservations[0].Instances[0].VpcId)
	}
}
开发者ID:gamisan9999,项目名称:ec2vpc,代码行数:17,代码来源:main.go


注:本文中的github.com/aws/aws-sdk-go/service/ec2.EC2.DescribeInstances方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。