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


Golang EC2.TerminateInstances方法代码示例

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


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

示例1: awsTerminateInstance

func awsTerminateInstance(conn *ec2.EC2, id string) error {
	log.Printf("[INFO] Terminating instance: %s", id)
	req := &ec2.TerminateInstancesInput{
		InstanceIds: []*string{aws.String(id)},
	}
	if _, err := conn.TerminateInstances(req); err != nil {
		return fmt.Errorf("Error terminating instance: %s", err)
	}

	log.Printf("[DEBUG] Waiting for instance (%s) to become terminated", id)

	stateConf := &resource.StateChangeConf{
		Pending:    []string{"pending", "running", "shutting-down", "stopped", "stopping"},
		Target:     []string{"terminated"},
		Refresh:    InstanceStateRefreshFunc(conn, id),
		Timeout:    10 * time.Minute,
		Delay:      10 * time.Second,
		MinTimeout: 3 * time.Second,
	}

	_, err := stateConf.WaitForState()
	if err != nil {
		return fmt.Errorf(
			"Error waiting for instance (%s) to terminate: %s", id, err)
	}

	return nil
}
开发者ID:chandy,项目名称:terraform,代码行数:28,代码来源:resource_aws_instance.go

示例2: termInstance

// This function will terminate a set of instances based on the ID.  It takes a
// slice of strings and calls the API to stop them all.  It will return nil if
// there are no problems.
func termInstance(instanceids []string, ec2client *ec2.EC2) error {
	// Create our struct to hold everything
	instanceReq := ec2.TerminateInstancesInput{
		InstanceIds: []*string{},
	}

	// Loop through our input array and add them to our struct, converting them to the string pointer required by the SDK
	for _, id := range instanceids {
		instanceReq.InstanceIds = append(instanceReq.InstanceIds, aws.String(id))
	}

	//Make the request to kill all the instances, returning an error if we got one.
	instanceResp, err := ec2client.TerminateInstances(&instanceReq)
	if err != nil {
		return err
	}

	// The number of instances we got back should be the same as how many we requested.
	if len(instanceResp.TerminatingInstances) != len(instanceids) {
		return errors.New("The total number of stopped instances did not match the request")
	}

	// Finally, let's loop through all of the responses and see they aren't all terminated.
	// We'll store each ID in a string so we can build a good error and use it to see later if we had any unterminated
	allterminated := ""

	// Loop through all the instances and check the state
	for _, instance := range instanceResp.TerminatingInstances {
		if *instance.CurrentState.Code != INSTANCE_STATE_TERMINATED && *instance.CurrentState.Code != INSTANCE_STATE_SHUTTING_DOWN {
			allterminated = allterminated + " " + *instance.InstanceId
		}
	}

	// If we found some that didn't terminate then return the rror
	if allterminated != "" {
		return errors.New("The following instances were not properly terminated: " + allterminated)
	}

	// Else let's return nil for success
	return nil
}
开发者ID:roo7break,项目名称:cracklord,代码行数:44,代码来源:aws-api.go


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