Hey, i have this python script that i want to chec...
# python
e
Hey, i have this python script that i want to check if the security group existing or not, if its existing then import it if not create a new one, but i get this error
Copy code
error: Program failed with an unhandled exception:
    Traceback (most recent call last):
      File "/opt/homebrew/bin/pulumi-language-python-exec", line 111, in <module>
        loop.run_until_complete(coro)
      File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
        return future.result()
      File "/Users/afeefaz/Downloads/iac/pulumi/venv/lib/python3.10/site-packages/pulumi/runtime/stack.py", line 126, in run_in_stack
        await run_pulumi_func(lambda: Stack(func))
      File "/Users/afeefaz/Downloads/iac/pulumi/venv/lib/python3.10/site-packages/pulumi/runtime/stack.py", line 51, in run_pulumi_func
        await wait_for_rpcs()
      File "/Users/afeefaz/Downloads/iac/pulumi/venv/lib/python3.10/site-packages/pulumi/runtime/stack.py", line 73, in wait_for_rpcs
        await RPC_MANAGER.rpcs.pop()
      File "/Users/afeefaz/Downloads/iac/pulumi/venv/lib/python3.10/site-packages/pulumi/runtime/rpc_manager.py", line 68, in rpc_wrapper
        result = await rpc
      File "/Users/afeefaz/Downloads/iac/pulumi/venv/lib/python3.10/site-packages/pulumi/output.py", line 98, in is_value_known
        return await is_known and not contains_unknowns(await future)
      File "/Users/afeefaz/Downloads/iac/pulumi/venv/lib/python3.10/site-packages/pulumi/output.py", line 98, in is_value_known
        return await is_known and not contains_unknowns(await future)
      File "/Users/afeefaz/Downloads/iac/pulumi/venv/lib/python3.10/site-packages/pulumi/output.py", line 98, in is_value_known
        return await is_known and not contains_unknowns(await future)
      [Previous line repeated 10 more times]
      File "/Users/afeefaz/Downloads/iac/pulumi/venv/lib/python3.10/site-packages/pulumi/runtime/resource.py", line 561, in do_register
        req = resource_pb2.RegisterResourceRequest(
    TypeError: bad argument type for built-in operation
Here’s the python code :
Copy code
# check for SG existence
try:
    existing_sg = aws.ec2.get_security_group(name="sg_name", vpc_id=vpc_id)
except:
    print("Creating new SG")

    created_sg = aws.ec2.SecurityGroup("sg_name",
        name="sg_name",
        vpc_id=vpc_id,
        egress=[{
            'from_port': 0,
            'to_port': 0,
            'protocol': "-1",
            'cidr_blocks': ['0.0.0.0/0']
        }],
        ingress=[{
            'from_port': 0,
            'to_port': 0,
            'protocol': "-1",
            'cidr_blocks': ['0.0.0.0/0']
        }],
        tags={
            "Name":"sg_name"
        }
    )
    pulumi.export("sg_name", created_sg.id)
else:
    aws.ec2.SecurityGroup("sg_name",
        opts=pulumi.ResourceOptions(
            import_=[existing_sg.id]
            )
    )
b
you cannot do this with Pulumi, it’s an imperative action. you need to just define the security group and let pulumi create it, not do conditional logic
p
@billowy-army-68599 But what if I need to make a condition? for example before creating an EKS cluster I need to validate if it exists Just like VPC for example... How do I check if the VPC with the name "XPTO" exists?
a
If you really want to do this the best way is to make a call to boto3 directly. But I agree with jaxxstorm, it's not a regular pattern, you should just let Pulumi do it's job in a declarative way.