https://pulumi.com logo
#general
Title
# general
c

cold-rocket-35235

05/13/2020, 11:37 AM
Hello! đź‘‹ Just starting out / testing things, trying to look at existing resources programmatically. Slightly confused on how the getters should work, thanks for your help! Thread ->
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const attempt1 = async () => {
  let instances = await aws.ec2.getInstances({}, { async: true });
  // Property 'forEach' does not exist on type 'GetInstancesResult'.ts(2339)
  instances.forEach((instance) => {
    console.log(instance.instanceTags);
  });
};

const attempt2 = async () => {
  let instances = pulumi.output(aws.ec2.getInstances({}, { async: true }));
  // Property 'forEach' does not exist on type 'Output<UnwrappedObject<GetInstancesResult>>'.ts(2339)
  instances.forEach((instance) => {
    console.log(instance.instanceTags);
  });
};

const attempt3 = async () => {
  let instances = pulumi.output(aws.ec2.getInstances({}, { async: true }));
  instances.apply((instances) => {
    // Property 'forEach' does not exist on type 'UnwrappedObject<GetInstancesResult>'.ts(2339)
    instances.forEach((instance) => {
      console.log(instance.instanceTags);
    });
  });
};
f

faint-table-42725

05/13/2020, 5:28 PM
As the API doc you linked states, the function returns a type of
Promise<GetInstancesResult>
which is not an array you can iterate over. You’ll see that that interface has a bunch of fields on which you can interate.
So after you’ve awaited it, you still have a
GetInstancesResult
against which you’ll want to use one of its properties like
ids
or
instanceTags
to iterate
c

cold-rocket-35235

05/13/2020, 6:10 PM
Gotcha, thanks. Is it possible to run this code to query the existing infrastructure created with other tools, without (re-)creating or importing the infrastructure to pulumi? Just trying to run the code gets me a
Copy code
(node:28738) UnhandledPromiseRejectionWarning: Error: Program run without the Pulumi engine available; re-run using the `pulumi` CLI
and
pulumi preview
gets me
Copy code
Manage your Pulumi stacks by logging in.
Run `pulumi login --help` for alternative login options.
Enter your access token from <https://app.pulumi.com/account/tokens>
    or hit <ENTER> to log in using your browser                   :
I was wondering if there is a low-commitment way to check things out
f

faint-table-42725

05/13/2020, 6:53 PM
You can use a local file system backend if you don’t want to setup a free Pulumi account (see https://www.pulumi.com/docs/intro/concepts/state/#backends)
2 Views