我正在编写一个 python 代码来使用 Boto3SDK 与 AWS 通信,但我有一些错误,我是 python 的新手。
这是代码:
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
这就是我尝试使用它的方式:
from main import AutoScalingWrapper
u = AutoScalingWrapper("ASGName")
print(u.describe_group())
但它会导致错误:
line 4, in <module>
print(u.describe_group())
^^^^^^^^^^^^^^^^^^
TypeError: AutoScalingWrapper.describe_group() missing 1 required positional argument: 'group_name'
有人可以帮助我吗?
执行 python 代码以将 Boto3SDK 与 AWS 结合使用
您有 2 个问题。
First
AutoScalingWrapper
期望客户端作为参数,并且您正在传递一个字符串。我强烈建议输入提示来防止这种情况发生。
其次,您在
describe_group
中缺少群组名称,因此您需要类似的内容
from main import AutoScalingWrapper
import boto3
client = boto3.client('autoscaling')
u = AutoScalingWrapper(client)
print(u.describe_group("my-auto-scaling-group"))