每次使用Boto3获取具有唯一过滤器的ec2实例的成本和使用情况

问题描述

UseCase -根据标签获取EC2实例的成本。 我们正在执行两个强制性标签(所有者和应用程序),并且需要基于这些标签获取EC2实例的成本。 我们希望它采用CSV格式的文档,该文档每个月都会上传到S3。

预期输出- Expected output image

现在,我正在使用以下代码。我正在获取所有所有者标记值,并要求每个所有者产生的费用并按应用程序将它们分组。

    tags_response = ce_client.get_tags(
        TimePeriod={
            'Start': start,'End': end
        },TagKey='owner'
        )
    
        
        for i in tags_response["Tags"]:
            response = ce_client.get_cost_and_usage(
                TimePeriod={
                    'Start': start,'End': end
                },Filter={
                    'Dimensions': {
                        'Key': 'SERVICE','Values': [
                            'Amazon Elastic Compute Cloud - Compute'
                            ],},'Tags': {
                        'Key': 'owner','Values': [
                            i,],}
                },Granularity='MONTHLY',Metrics=["Unblendedcost"],GroupBy=[{
                    'Type': 'TAG','Key': 'Application'
                }]
                )

但是Lambda抛出以下错误- "An error occurred (ValidationException) when calling the GetCostAndUsage operation: Expression has more than one roots. Only one root operator is allowed for each expression: And,Or,Not,Dimensions,Tags,CostCategories"

此外,我在想也许循环请求不是一个好主意。我可以遵循一种更好的方法/方法来获得所需的输出吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)