Is there a way to loop through a list of resources...
# general
g
Is there a way to loop through a list of resources e.g. ec2 instances per id, in order to create another resource like an Cloudwatch alert/alarm?
g
Yes! If you already know the instance IDs, you can just use a for loop to do this. If you need to look up the instances (or other resources), there are
getX
functions that can help with this - e.g. https://www.pulumi.com/docs/reference/pkg/aws/ec2/getinstances/.
g
@gentle-diamond-70147 - thanks! where is documentation on how to implement the loop in? Can you send that
I already know the instances- I have a list of them
my list looks like
Copy code
test_instances = [i-xyz i-abc]
i just want to reference the each instance ID one at time
@gentle-diamond-70147 - can you provide an example?
g
You would do something like this, assuming you're using Python:
Copy code
test_instances = ["i-xyz", "i-abc"]

for instance_id in test_instances:
    instance_alarm = aws.cloudwatch.MetricAlarm(f"alarm-{instance_id}",
        alarm_description=f"Monitor for instance {instance_id}",
        # other inputs
    )
That's just normal python by the way, the for loop is not something specific to Pulumi, in case that's not clear.
g
awesome!
i will test this