#🔒 Creating an EC2 AWS Class

14 messages · Page 1 of 1 (latest)

stray trail
#

Hey, I have a personal project with a Python issue. I come from a Coding Background. My primary language has been Go.

I am trying to make this EC2 instance into its own class by moving its arguments into the init function. The arguments I'm trying to move are at the bottom if that makes sense, so the code at the bottom would be different. Just left it there to help with the question.

class ec2Args:
    def __init__(self, filters=[],owners=[],*args):
        
        self.filters = filters[aws.ec2.GetAmiFilterArgs(
            name = "",
            values=[],
        ),
        aws.ec2.GetAmiFilterArgs(
            name = "",
            values= [],
        ),
        ],
        self.owners = owners [""] 

ubuntu = aws.ec2.get_ami(most_recent=True,
    filters=[
        aws.ec2.GetAmiFilterArgs(
            name="name",
            values=["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"],
        ),
        aws.ec2.GetAmiFilterArgs(
            name="virtualization-type",
            values=["hvm"],
        ),
    ],
    owners=["099720109477"])

Have I done this correctly to be able to call the class and pass in the values needed? What's throwing me is the list and the essentially AWS tuple arguments.

Please don't ask why it's a work project; they want it done this way.

weak graniteBOT
#

@stray trail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

warped shard
#

seems like you're tackling this from the wrong side, the function get_ami is a simple call, and if you wanted to use a class for the arguments, you'd need to be able to serialize the class into a dict and use unpacking to form the keyword arguments like so (example):

ubuntu = aws.ec2.get_ami(**args.to_dict())

A more common approach would be to wrap the get_ami call into a class, so you pass your args into the ec2Args class, which will call the get_ami inside and return the ubuntu variable, but for such a simple case there's really no need

stray trail
#

Could I get an example? My brain is fried. I can't get my head round your second part sorry.

warped shard
#

sure, a wrapper class for the thing would be something like this:

class MyEC2Instance:
    def __init__(self, filters, owners):
        self.instance = aws.ec2.get_ami(
            most_recent=True,
            filters=filters,
            owners=owners,
        )


ec2 = MyEC2Instance(
    filters=[
        aws.ec2.GetAmiFilterArgs(
            name="name",
            values=["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"],
        ),
        aws.ec2.GetAmiFilterArgs(
            name="virtualization-type",
            values=["hvm"],
        ),
    ],
    owners=["099720109477"],
)
ubuntu = ec2.instance
#

this is fairly common in OOP and especially large code bases because then you can change the implementation of MyEC2Instance without changing the public API

#

it's basically the same to what people do with DB connections, web APIs and such

stray trail
#

ahhh

#

I get it

#

So in Go your not allowed to pass something to a function without it being like explecitly defined

#

So my brain looks at that code and goes no that wont work lol

#

So question:

How does Python know that your putting something in the Filters part?

So like you have: filters=filters

But to call filters you have to do: ```py
filters=[
aws.ec2.GetAmiFilterArgs(
name="name",
values=["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"],
),
aws.ec2.GetAmiFilterArgs(
name="virtualization-type",
values=["hvm"],
),
],


The structure looks different to the define so how does Python know or say thats okay?
weak graniteBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.