Hi there. I'm working with aws-python, and I've su...
# getting-started
s
Hi there. I'm working with aws-python, and I've successfully created an EC2 instance. I now have put a loop around that logic, ensured that each object has a unique name, but I only get the first instance to verify upon pulumi up. Guidance would be appreciated. TIA.
p
can you share some code sample?
s
Sure, thanks for looking. With some redaction which may break it, if taken literally ...
Copy code
def create_instance(index):
    instance_name = "%s-%02d"%(core.get("instance_name"), index)
    <http://pulumi.info|pulumi.info>("Instance name: %s"%instance_name)

    builder = aws.ec2.Instance(instance_name,
                               ami = host_ami.id,
                               ebs_optimized = True,
                               instance_type = aws.ec2.InstanceType.T3A_MEDIUM,
                               key_name = "steven.webster.keypair",
                               root_block_device = {
                                   "volume_type": "gp2",
                                   "volume_size": 200,
                                   "delete_on_termination": True
                               },
                               vpc_security_group_ids = [security_group.id],
                               subnet_id = core_subnet_id,
                               tags = {
                                   "Name": instance_name,
                               })
                               user_data = user_data)                                                                                                                                      

instance_quantity = core.get("instance_quantity")
for index in range(1, instance_quantity):
    <http://pulumi.info|pulumi.info>("Making %s instances"%instance_quantity)
    create_instance(index)
Output is
Copy code
Diagnostics:
  pulumi:pulumi:Stack (windows-builder-dev):
    Making 2 instances
    Instance name: core-gitlab-runner-docker-windows-01


Do you want to perform this update? no
p
that’s because your loop has only one iteration
you probably wanted to write:
Copy code
for index in range(0, instance_quantity)
s
yikes, thank you