GetInstances

Use this data source to get IDs or IPs of Amazon EC2 instances to be referenced elsewhere, e.g. to allow easier migration from another management solution or to make it easier for an operator to connect through bastion host(s).

Note: It’s strongly discouraged to use this data source for querying ephemeral instances (e.g. managed via autoscaling group), as the output may change at any time and you’d need to re-run apply every time an instance comes up or dies.

Example Usage

using System.Collections.Generic;
using System.Threading.Tasks;
using Pulumi;
using Aws = Pulumi.Aws;

class MyStack : Stack
{
    public MyStack()
    {
        var dict = Output.Create(Initialize());
    }

    private async Task<IDictionary<string, Output<string>>> Initialize()
    {
        var testInstances = await Aws.Ec2.GetInstances.InvokeAsync(new Aws.Ec2.GetInstancesArgs
        {
            Filters = 
            {
                new Aws.Ec2.Inputs.GetInstancesFilterArgs
                {
                    Name = "instance.group-id",
                    Values = 
                    {
                        "sg-12345678",
                    },
                },
            },
            InstanceStateNames = 
            {
                "running",
                "stopped",
            },
            InstanceTags = 
            {
                { "Role", "HardWorker" },
            },
        });
        var testEip = new List<Aws.Ec2.Eip>();
        for (var rangeIndex = 0; rangeIndex < testInstances.Ids.Length; rangeIndex++)
        {
            var range = new { Value = rangeIndex };
            testEip.Add(new Aws.Ec2.Eip($"testEip-{range.Value}", new Aws.Ec2.EipArgs
            {
                Instance = testInstances.Ids[range.Value],
            }));
        }

        return new Dictionary<string, Output<string>>
        {
        };
    }

}

Coming soon!

import pulumi
import pulumi_aws as aws

test_instances = aws.ec2.get_instances(filters=[{
        "name": "instance.group-id",
        "values": ["sg-12345678"],
    }],
    instance_state_names=[
        "running",
        "stopped",
    ],
    instance_tags={
        "Role": "HardWorker",
    })
test_eip = []
for range in [{"value": i} for i in range(0, len(test_instances.ids))]:
    test_eip.append(aws.ec2.Eip(f"testEip-{range['value']}", instance=test_instances.ids[range["value"]]))
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const testInstances = pulumi.output(aws.ec2.getInstances({
    filters: [{
        name: "instance.group-id",
        values: ["sg-12345678"],
    }],
    instanceStateNames: [
        "running",
        "stopped",
    ],
    instanceTags: {
        Role: "HardWorker",
    },
}, { async: true }));
const testEip: aws.ec2.Eip[] = [];
for (let i = 0; i < testInstances.apply(testInstances => testInstances.ids.length); i++) {
    testEip.push(new aws.ec2.Eip(`test-${i}`, {
        instance: testInstances.apply(testInstances => testInstances.ids[i]),
    }));
}

Using GetInstances

function getInstances(args: GetInstancesArgs, opts?: InvokeOptions): Promise<GetInstancesResult>
function  get_instances(filters=None, instance_state_names=None, instance_tags=None, opts=None)
func GetInstances(ctx *Context, args *GetInstancesArgs, opts ...InvokeOption) (*GetInstancesResult, error)
public static class GetInstances {
    public static Task<GetInstancesResult> InvokeAsync(GetInstancesArgs args, InvokeOptions? opts = null)
}

The following arguments are supported:

Filters List<GetInstancesFilterArgs>

One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out [describe-instances in the AWS CLI reference][1].

InstanceStateNames List<string>

A 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.

InstanceTags Dictionary<string, string>

A map of tags, each pair of which must exactly match a pair on desired instances.

Filters []GetInstancesFilter

One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out [describe-instances in the AWS CLI reference][1].

InstanceStateNames []string

A 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.

InstanceTags map[string]string

A map of tags, each pair of which must exactly match a pair on desired instances.

filters GetInstancesFilter[]

One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out [describe-instances in the AWS CLI reference][1].

instanceStateNames string[]

A 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.

instanceTags {[key: string]: string}

A map of tags, each pair of which must exactly match a pair on desired instances.

filters List[GetInstancesFilter]

One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out [describe-instances in the AWS CLI reference][1].

instance_state_names List[str]

A 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.

instance_tags Dict[str, str]

A map of tags, each pair of which must exactly match a pair on desired instances.

GetInstances Result

The following output properties are available:

Id string

The provider-assigned unique ID for this managed resource.

Ids List<string>

IDs of instances found through the filter

InstanceTags Dictionary<string, string>
PrivateIps List<string>

Private IP addresses of instances found through the filter

PublicIps List<string>

Public IP addresses of instances found through the filter

Filters List<GetInstancesFilter>
InstanceStateNames List<string>
Id string

The provider-assigned unique ID for this managed resource.

Ids []string

IDs of instances found through the filter

InstanceTags map[string]string
PrivateIps []string

Private IP addresses of instances found through the filter

PublicIps []string

Public IP addresses of instances found through the filter

Filters []GetInstancesFilter
InstanceStateNames []string
id string

The provider-assigned unique ID for this managed resource.

ids string[]

IDs of instances found through the filter

instanceTags {[key: string]: string}
privateIps string[]

Private IP addresses of instances found through the filter

publicIps string[]

Public IP addresses of instances found through the filter

filters GetInstancesFilter[]
instanceStateNames string[]
id str

The provider-assigned unique ID for this managed resource.

ids List[str]

IDs of instances found through the filter

instance_tags Dict[str, str]
private_ips List[str]

Private IP addresses of instances found through the filter

public_ips List[str]

Public IP addresses of instances found through the filter

filters List[GetInstancesFilter]
instance_state_names List[str]

Supporting Types

GetInstancesFilter

See the input and output API doc for this type.

See the input and output API doc for this type.

See the input and output API doc for this type.

Name string
Values List<string>
Name string
Values []string
name string
values string[]
name str
values List[str]

Package Details

Repository
https://github.com/pulumi/pulumi-aws
License
Apache-2.0
Notes
This Pulumi package is based on the aws Terraform Provider.