github.com/pulumi/pulumi-aws/sdk/v6@v6.32.0/go/aws/ec2/getInstances.go (about)

     1  // Code generated by the Pulumi Terraform Bridge (tfgen) Tool DO NOT EDIT.
     2  // *** WARNING: Do not edit by hand unless you're certain you know what you are doing! ***
     3  
     4  package ec2
     5  
     6  import (
     7  	"context"
     8  	"reflect"
     9  
    10  	"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/internal"
    11  	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    12  )
    13  
    14  // Use this data source to get IDs or IPs of Amazon EC2 instances to be referenced elsewhere,
    15  // e.g., to allow easier migration from another management solution
    16  // or to make it easier for an operator to connect through bastion host(s).
    17  //
    18  // > **Note:** It's strongly discouraged to use this data source for querying ephemeral
    19  // instances (e.g., managed via autoscaling group), as the output may change at any time
    20  // and you'd need to re-run `apply` every time an instance comes up or dies.
    21  //
    22  // ## Example Usage
    23  //
    24  // <!--Start PulumiCodeChooser -->
    25  // ```go
    26  // package main
    27  //
    28  // import (
    29  //
    30  //	"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2"
    31  //	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
    32  //
    33  // )
    34  //
    35  //	func main() {
    36  //		pulumi.Run(func(ctx *pulumi.Context) error {
    37  //			test, err := ec2.GetInstances(ctx, &ec2.GetInstancesArgs{
    38  //				InstanceTags: map[string]interface{}{
    39  //					"Role": "HardWorker",
    40  //				},
    41  //				Filters: []ec2.GetInstancesFilter{
    42  //					{
    43  //						Name: "instance.group-id",
    44  //						Values: []string{
    45  //							"sg-12345678",
    46  //						},
    47  //					},
    48  //				},
    49  //				InstanceStateNames: []string{
    50  //					"running",
    51  //					"stopped",
    52  //				},
    53  //			}, nil)
    54  //			if err != nil {
    55  //				return err
    56  //			}
    57  //			var testEip []*ec2.Eip
    58  //			for index := 0; index < len(test.Ids); index++ {
    59  //				key0 := index
    60  //				val0 := index
    61  //				__res, err := ec2.NewEip(ctx, fmt.Sprintf("test-%v", key0), &ec2.EipArgs{
    62  //					Instance: test.Ids[val0],
    63  //				})
    64  //				if err != nil {
    65  //					return err
    66  //				}
    67  //				testEip = append(testEip, __res)
    68  //			}
    69  //			return nil
    70  //		})
    71  //	}
    72  //
    73  // ```
    74  // <!--End PulumiCodeChooser -->
    75  func GetInstances(ctx *pulumi.Context, args *GetInstancesArgs, opts ...pulumi.InvokeOption) (*GetInstancesResult, error) {
    76  	opts = internal.PkgInvokeDefaultOpts(opts)
    77  	var rv GetInstancesResult
    78  	err := ctx.Invoke("aws:ec2/getInstances:getInstances", args, &rv, opts...)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	return &rv, nil
    83  }
    84  
    85  // A collection of arguments for invoking getInstances.
    86  type GetInstancesArgs struct {
    87  	// One or more name/value pairs to use as filters. There are
    88  	// several valid keys, for a full reference, check out
    89  	// [describe-instances in the AWS CLI reference][1].
    90  	Filters []GetInstancesFilter `pulumi:"filters"`
    91  	// List of instance states that should be applicable to the desired instances. The permitted values are: `pending, running, shutting-down, stopped, stopping, terminated`. The default value is `running`.
    92  	InstanceStateNames []string `pulumi:"instanceStateNames"`
    93  	// Map of tags, each pair of which must
    94  	// exactly match a pair on desired instances.
    95  	InstanceTags map[string]string `pulumi:"instanceTags"`
    96  }
    97  
    98  // A collection of values returned by getInstances.
    99  type GetInstancesResult struct {
   100  	Filters []GetInstancesFilter `pulumi:"filters"`
   101  	// The provider-assigned unique ID for this managed resource.
   102  	Id string `pulumi:"id"`
   103  	// IDs of instances found through the filter
   104  	Ids                []string          `pulumi:"ids"`
   105  	InstanceStateNames []string          `pulumi:"instanceStateNames"`
   106  	InstanceTags       map[string]string `pulumi:"instanceTags"`
   107  	// IPv6 addresses of instances found through the filter
   108  	Ipv6Addresses []string `pulumi:"ipv6Addresses"`
   109  	// Private IP addresses of instances found through the filter
   110  	PrivateIps []string `pulumi:"privateIps"`
   111  	// Public IP addresses of instances found through the filter
   112  	PublicIps []string `pulumi:"publicIps"`
   113  }
   114  
   115  func GetInstancesOutput(ctx *pulumi.Context, args GetInstancesOutputArgs, opts ...pulumi.InvokeOption) GetInstancesResultOutput {
   116  	return pulumi.ToOutputWithContext(context.Background(), args).
   117  		ApplyT(func(v interface{}) (GetInstancesResult, error) {
   118  			args := v.(GetInstancesArgs)
   119  			r, err := GetInstances(ctx, &args, opts...)
   120  			var s GetInstancesResult
   121  			if r != nil {
   122  				s = *r
   123  			}
   124  			return s, err
   125  		}).(GetInstancesResultOutput)
   126  }
   127  
   128  // A collection of arguments for invoking getInstances.
   129  type GetInstancesOutputArgs struct {
   130  	// One or more name/value pairs to use as filters. There are
   131  	// several valid keys, for a full reference, check out
   132  	// [describe-instances in the AWS CLI reference][1].
   133  	Filters GetInstancesFilterArrayInput `pulumi:"filters"`
   134  	// List of instance states that should be applicable to the desired instances. The permitted values are: `pending, running, shutting-down, stopped, stopping, terminated`. The default value is `running`.
   135  	InstanceStateNames pulumi.StringArrayInput `pulumi:"instanceStateNames"`
   136  	// Map of tags, each pair of which must
   137  	// exactly match a pair on desired instances.
   138  	InstanceTags pulumi.StringMapInput `pulumi:"instanceTags"`
   139  }
   140  
   141  func (GetInstancesOutputArgs) ElementType() reflect.Type {
   142  	return reflect.TypeOf((*GetInstancesArgs)(nil)).Elem()
   143  }
   144  
   145  // A collection of values returned by getInstances.
   146  type GetInstancesResultOutput struct{ *pulumi.OutputState }
   147  
   148  func (GetInstancesResultOutput) ElementType() reflect.Type {
   149  	return reflect.TypeOf((*GetInstancesResult)(nil)).Elem()
   150  }
   151  
   152  func (o GetInstancesResultOutput) ToGetInstancesResultOutput() GetInstancesResultOutput {
   153  	return o
   154  }
   155  
   156  func (o GetInstancesResultOutput) ToGetInstancesResultOutputWithContext(ctx context.Context) GetInstancesResultOutput {
   157  	return o
   158  }
   159  
   160  func (o GetInstancesResultOutput) Filters() GetInstancesFilterArrayOutput {
   161  	return o.ApplyT(func(v GetInstancesResult) []GetInstancesFilter { return v.Filters }).(GetInstancesFilterArrayOutput)
   162  }
   163  
   164  // The provider-assigned unique ID for this managed resource.
   165  func (o GetInstancesResultOutput) Id() pulumi.StringOutput {
   166  	return o.ApplyT(func(v GetInstancesResult) string { return v.Id }).(pulumi.StringOutput)
   167  }
   168  
   169  // IDs of instances found through the filter
   170  func (o GetInstancesResultOutput) Ids() pulumi.StringArrayOutput {
   171  	return o.ApplyT(func(v GetInstancesResult) []string { return v.Ids }).(pulumi.StringArrayOutput)
   172  }
   173  
   174  func (o GetInstancesResultOutput) InstanceStateNames() pulumi.StringArrayOutput {
   175  	return o.ApplyT(func(v GetInstancesResult) []string { return v.InstanceStateNames }).(pulumi.StringArrayOutput)
   176  }
   177  
   178  func (o GetInstancesResultOutput) InstanceTags() pulumi.StringMapOutput {
   179  	return o.ApplyT(func(v GetInstancesResult) map[string]string { return v.InstanceTags }).(pulumi.StringMapOutput)
   180  }
   181  
   182  // IPv6 addresses of instances found through the filter
   183  func (o GetInstancesResultOutput) Ipv6Addresses() pulumi.StringArrayOutput {
   184  	return o.ApplyT(func(v GetInstancesResult) []string { return v.Ipv6Addresses }).(pulumi.StringArrayOutput)
   185  }
   186  
   187  // Private IP addresses of instances found through the filter
   188  func (o GetInstancesResultOutput) PrivateIps() pulumi.StringArrayOutput {
   189  	return o.ApplyT(func(v GetInstancesResult) []string { return v.PrivateIps }).(pulumi.StringArrayOutput)
   190  }
   191  
   192  // Public IP addresses of instances found through the filter
   193  func (o GetInstancesResultOutput) PublicIps() pulumi.StringArrayOutput {
   194  	return o.ApplyT(func(v GetInstancesResult) []string { return v.PublicIps }).(pulumi.StringArrayOutput)
   195  }
   196  
   197  func init() {
   198  	pulumi.RegisterOutputType(GetInstancesResultOutput{})
   199  }