skip to Main Content

I’m working on a python code to use the Boto3SDK to speak with AWS but i have some error, i’m a newbie in python.

This is the code :

import logging
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)

class AutoScalingWrapper:
    def __init__(self, autoscaling_client):
        self.autoscaling_client = autoscaling_client

    def describe_group(self, group_name):
        try:
            response = self.autoscaling_client.describe_auto_scaling_groups(
                AutoScalingGroupNames=[group_name])
        except ClientError as err:
            logger.error(
                "Couldn't describe group %s. Here's why: %s: %s", group_name,
                err.response['Error']['Code'], err.response['Error']['Message'])
            raise
        else:
            groups = response.get('AutoScalingGroups', [])
            return groups[0] if len(groups) > 0 else None

This is how i try to use it :


from main import AutoScalingWrapper

u = AutoScalingWrapper("ASGName")
print(u.describe_group())

But it result an error :

line 4, in <module>
    print(u.describe_group())
          ^^^^^^^^^^^^^^^^^^
TypeError: AutoScalingWrapper.describe_group() missing 1 required positional argument: 'group_name'

Anyone can help me please ?

execute python code to use the Boto3SDK with AWS

2

Answers


  1. Chosen as BEST ANSWER

    i have tried to do what you said but it doesn't work. And i already did that in the past with your example, for exemple :

    import boto3
    client = boto3.client('autoscaling')
    clearesponse = client.describe_auto_scaling_groups(
        AutoScalingGroupNames=[
            'ASGName',
        ])
    
    for reservation in clearesponse ['AutoScalingGroups']:
            for instance in reservation['Instances']:
                print(instance['InstanceId'])
    

    That's works, but i don't want to do that, i want to use the AWS exemple code but it did't work, can you explain how to use it please ?

    This is what i'm speaking (with class and 2 def) : https://docs.aws.amazon.com/code-library/latest/ug/python_3_auto-scaling_code_examples.html (Get information about groups)

    I ask it because i don't found any information on the internet how to :(


  2. You have 2 problems.

    First AutoScalingWrapper is expecting a client as an argument, and you are passing a string. I strongly suggest type hints to prevent this.

    Second, you are missing the group name in describe_group, so you need something like

    from main import AutoScalingWrapper
    
    import boto3
    
    client = boto3.client('autoscaling')
    
    u = AutoScalingWrapper(client)
    print(u.describe_group("my-auto-scaling-group"))
    
    

    Check the docs for more details: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/autoscaling/client/describe_auto_scaling_groups.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search