

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在 AMI 产品中进行成本分配标记 AWS Marketplace
<a name="cost-allocation-tagging-ami-marketplace"></a>

AWS Marketplace 支持基于亚马逊系统映像 (AMI) 的软件产品的成本分配标记。新的和现有的亚马逊弹性计算云 (Amazon EC2) 实例标签会根据相应 AWS Marketplace 的 AMI 使用情况自动填充。您可以使用已激活的成本分配标签，通过成本和使用情况报告 AWS Cost Explorer、 AWS 预算或其他云支出分析工具来识别和跟踪 AMI 的使用情况。 AWS 

提供 AMI 的供应商还可能根据产品的特定信息，在计量基于 AMI 的产品时记录其他自定义标签。有关更多信息，请参阅[使用成本分配标记](cost-allocation-tagging.md)。

您可以使用标签来整理资源，并可以使用成本分配标签来细致地跟踪 AWS 成本。激活成本分配标签后， AWS 使用成本分配标签在成本分配报告中整理资源成本，以便您更轻松地对 AWS 成本进行分类和跟踪。

成本分配标记仅跟踪在账单与成本管理控制台中激活标签后的成本。只有 AWS 账户 所有者、 AWS Organizations 管理账户所有者和具有相应权限的用户才能访问账户的账单和成本管理控制台。无论您是否使用成本分配标记，您的账单金额都不会发生变化。是否使用成本分配标签不会对基于 AMI 的软件产品的功能产生影响。

## 跨多个实例跟踪一个 AMI 的成本分配标签
<a name="multi-instances-cost-allocation-tagging-marketplace"></a>

每个为订阅 AWS Marketplace AMI 而启动的 Amazon EC2 实例在 “ AWS 成本和使用情况” 报告中都有相应的 AWS Marketplace 软件使用量项目。您的 AWS Marketplace 使用量将始终反映应用于相应的 Amazon EC2 实例的特定标签。这使您可以根据在实例级别分配的不同标签值来区分 AWS Marketplace 使用成本。

您还可以使用 Cost Explorer 或 “成本和使用情况” 报告汇总基于标签的使用成本，使其等于账单中反映的 AMI 软件使用费。 AWS 

## 使用分配了成本的已标记实例查找预算
<a name="cost-allocation-tag-script-marketplace"></a>

如果您已经在账单与成本管理控制台中的多个 Amazon EC2 实例上按成本分配标签过滤了活动的预算，则可能很难找到所有这些预算。以下 Python 脚本返回预算列表，其中包含来自当前 AWS 区域中 AWS Marketplace 的 Amazon EC2 实例。

您可以使用此脚本来了解对预算的潜在影响，以及此更改可能导致超支的地方。请注意，账单金额不会发生变化，但会更准确地反映成本分配，这可能会影响预算。

```
#! /usr/bin/python

import boto3

session = boto3.Session()
b3account=boto3.client('sts').get_caller_identity()['Account']
print("using account {} in region {}".format(b3account,session.region_name))


def getBudgetFilters(filtertype):
    ''' 
    Returns budgets nested within the filter values [filter value][budeget name].
    The filtertype is the CostFilter Key such as Region, Service, TagKeyValue.
    '''    
    budget_client = session.client('budgets')
    budgets_paginator = budget_client.get_paginator('describe_budgets')
    budget_result = budgets_paginator.paginate(
        AccountId=b3account
    ).build_full_result()    
    returnval = {}
    if 'Budgets' in budget_result:
        for budget in budget_result['Budgets']:
            for cftype in budget['CostFilters']:
                if filtertype == cftype:                          
                    for cfval in budget['CostFilters'][cftype]:
                        if cfval in returnval:
                            if not budget['BudgetName'] in returnval[cfval]:
                                returnval[cfval].append(budget['BudgetName'])
                        else:
                            returnval[cfval] = [ budget['BudgetName'] ]
    return returnval

def getMarketplaceInstances():
    '''
    Get all the Amazon EC2 instances which originated with AWS Marketplace.        
    '''
    ec2_client = session.client('ec2')
    paginator = ec2_client.get_paginator('describe_instances')
    returnval = paginator.paginate(
        Filters=[{
            'Name': 'product-code.type',
            'Values': ['marketplace']
        }]
    ).build_full_result()
    return returnval


def getInstances():
    mp_instances = getMarketplaceInstances()
    budget_tags = getBudgetFilters("TagKeyValue")
    cost_instance_budgets = []
    for instance in [inst for resrv in mp_instances['Reservations'] for inst in resrv['Instances'] if 'Tags' in inst.keys()]:    
        for tag in instance['Tags']:                
            # combine the tag and value to get the budget filter string
            str_full = "user:{}${}".format(tag['Key'], tag['Value'])
            if str_full in budget_tags:
                for budget in budget_tags[str_full]:
                    if not budget in cost_instance_budgets:
                        cost_instance_budgets.append(budget)    
    print("\r\nBudgets containing tagged Marketplace EC2 instances:")
    print( '\r\n'.join([budgetname for budgetname in cost_instance_budgets]) )


if __name__ == "__main__":
    getInstances()
```

**示例输出**

```
Using account 123456789012 in region us-east-2

Budgets containing tagged Marketplace EC2 instances:
EC2 simple
MP-test-2
```

## 相关主题
<a name="cost-allocation-tagging-related-topics"></a>

有关更多信息，请参阅以下主题：
+ 《AWS Billing 用户指南》**中的[使用成本分配标签](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html)。
+ 《AWS Billing 用户指南》中的[激活 AWS 生成的成本分配标签](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/activate-built-in-tags.html)**。
+ 《Amazon EC2 用户指南》中的[标记 Amazon EC2 资源](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html)。