本文整理匯總了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
}
示例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
}
示例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."))
}
示例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
}
示例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
}
示例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
}
示例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
}
}
示例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
}
示例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
}
示例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
}
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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)
}
}