

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# カスタム計測を使用したコンテナ製品の AWS Marketplace Metering Service および との統合 AWS SDK for Java
<a name="java-integration-example-meterusage"></a>

AWS Marketplace コンテナ製品では、製品ごとに最大 24 の異なる料金ディメンションでカスタム計測を行うことができます。カスタム計測を有効にするには、コンテナ製品を AWS Marketplace Metering Service と統合してください。[https://docs.aws.amazon.com/marketplacemetering/latest/APIReference/API_MeterUsage.html](https://docs.aws.amazon.com/marketplacemetering/latest/APIReference/API_MeterUsage.html) API オペレーションを使用して、 AWS に請求するために独自の料金単位とその使用量に対するカスタム計測を定義できます。次の例では、 を使用して [AWS Marketplace Metering Service](https://docs.aws.amazon.com/marketplacemetering/latest/APIReference/Welcome.html) `MeterUsage`オペレーションと AWS SDK for Java 統合する実装の概要を示します。

詳細については、「[`MeterUsage` Java の例](#meterusage-java-example)」を参照してください。次の手順の多くは、言語にかかわらず適用されます。

**例: AWS Marketplace Metering Service の統合**

1. [AWS Marketplace 管理ポータル](https://aws.amazon.com/marketplace/management/tour) にサインインします。

1. **[アセット]** から **[コンテナ]** を選択して、新しいコンテナ製品の作成を開始します。製品を作成すると、製品とコンテナイメージを統合するための製品コードが生成されます。 AWS Identity and Access Management (IAM) アクセス許可の設定については、「」を参照してください[AWS Marketplace 計測と使用権限 API アクセス許可](iam-user-policy-for-aws-marketplace-actions.md)。

1.  公開 [AWS Java SDK](https://aws.amazon.com/sdk-for-java/) をダウンロードします。
**重要**  
 Amazon Elastic Kubernetes Service (Amazon EKS) から計測 API オペレーションを呼び出すには、[サポートされている AWS SDK を使用](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-minimum-sdk.html)し、Kubernetes 1.13 以降を実行している Amazon EKS クラスターで実行する必要があります。

1. 各ディメンションの使用状況について、1 時間に 1 回、タスクまたはポッドから `MeterUsage` オペレーションを呼び出します。API オペレーションは、`Dimension`、`Resource`、`Hour` の一意の組み合わせに対して 1 つの計測レコードを受け入れます。リソースは、Amazon Elastic Container Service (Amazon ECS) タスクまたは Amazon Elastic Container Service (Amazon ECS) ポッドのいずれかです。

   ```
   {
       "ProductCode" : "string", // (required)
       "UsageDimension" : "string", // (required)
       "UsageQuantity":  int, // (optional) Default is 0. Acceptable value from [0, 2147483647 (INT_MAX)]
       "Timestamp": Date, // (required) Timestamp in UTC. Value can be one hour in the past.
       "UsageAllocations": List<UsageAllocation> // (optional) UsageAllocations across 1 or more tags.
   }
   ```
**注記**  
への接続で一時的な問題が発生する可能性があります AWS Marketplace Metering Service。 AWS Marketplace 短期的な停止やネットワークの問題を回避するために、 では最大 30 分間の再試行をエクスポネンシャルバックオフで実装することを強くお勧めします。

1. `MeterUsage` 呼び出しを含むコンテナイメージを再構築し、コンテナにタグ付けして、Amazon Elastic Container Registry (Amazon ECR) など、Amazon ECS または Amazon EKS と互換性がある任意の Docker レジストリにプッシュします。Amazon ECR を使用している場合は、Amazon ECS タスクまたは Amazon EKS ポッドを起動するアカウントに、Amazon ECR リポジトリへのアクセス許可があることを確認します。それ以外の場合は、このオペレーションは失敗します。

1. 次のコード例で定義されているように、コンテナが `MeterUsage` を呼び出すアクセス許可を付与する [IAM](https://aws.amazon.com/iam/) ロールを作成します。Amazon ECS タスクまたは Amazon EKS ポッド定義の[タスクロール](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_role_arn)パラメータで、この AWS Identity and Access Management (IAM) ロールを指定する必要があります。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Action": [
                   "aws-marketplace:MeterUsage"
                   ],
                   "Effect": "Allow",
                   "Resource": "*"
           }
       ]
   }
   ```

------

1. と統合されたコンテナを参照 AWS Marketplace し、ステップ 6 で作成した IAM ロールを参照する Amazon ECS タスクまたは Amazon EKS ポッド定義を作成します。ログ記録を表示するには、タスク定義で AWS CloudTrail ログ記録を有効にします。

1. Amazon ECS または Amazon EKS クラスターを作成して、タスクまたはポッドを実行します。Amazon ECS クラスターの作成の詳細については、「*Amazon Elastic Container Service デベロッパーガイド*」の「[クラスターの作成](https://docs.aws.amazon.com/AmazonECS/latest/userguide/create_cluster.html)」を参照してください。Amazon EKS クラスターの作成 (Kubernetes バージョン 1.1.3.x 以降を使用) の詳細については、「[Amazon EKS クラスターの作成](https://docs.aws.amazon.com/eks/latest/userguide/create_cluster.html)」を参照してください。

1. Amazon ECS または Amazon EKS クラスターを設定し、us-east-1 AWS Region のステップ 8 で作成した Amazon ECS タスク定義または Amazon EKS ポッドを起動します。製品が本番稼働になる前のこのテストプロセス中にのみ、このリージョンを使用する必要があります。

1. 製品に対して発行されるディメンションごとに `MeterUsage` から有効な応答が返されると、コンテナ製品の作成を開始できます。ご質問がある場合は、[AWS Marketplace Seller Operations](https://aws.amazon.com/marketplace/management/contact-us/) チームまでお問い合わせください。

## `MeterUsage` Java の例
<a name="meterusage-java-example"></a>

次のコード例では、 AWS SDK for Java および AWS Marketplace Metering Service を使用して `MeterUsage`オペレーションを呼び出します。

次のコード例は、`UsageAllocations` なしで `MeterUsage` オペレーションを呼び出します。

```
import com.amazonaws.services.marketplacemetering.AWSMarketplaceMetering;
import com.amazonaws.services.marketplacemetering.AWSMarketplaceMeteringClientBuilder;
import com.amazonaws.services.marketplacemetering.model.MeterUsageRequest;
import com.amazonaws.services.marketplacemetering.model.MeterUsageResult;

import java.util.Date;

public class MeterUsage {
    private static final String PRODUCT_CODE = ".......";
    private final AWSMarketplaceMetering awsMarketplaceMetering;

    public MeterUsage() {
        awsMarketplaceMetering = AWSMarketplaceMeteringClientBuilder.standard().build();
    }

    /**
     * Submits metering record for a FCP Dimension. The API accepts 1 metering record per dimension
     * for a given buyer's resource for a given timestamp hour. Ex. If a buyer is running 10 tasks,
     * the API will accepts 1 call to MeterUsage in an hour for a given dimension for each running task.
     *
     * @param dimension - FCP dimension name provided during the publishing of the product.
     * @param quantity - FCP dimension consumption value for the hour.
     * @param timestamp - Timestamp, in UTC, for which the usage is being reported.
     *                  Timestamp cant be more than 1 hour in the past.
     *                  Make sure the timestamp value is not before the start of the software usage.
     */
    public void callMeterUsage(String dimension, int quantity, Date timestamp) {
        MeterUsageRequest meterUsageRequest = new MeterUsageRequest()
                .withProductCode(PRODUCT_CODE)
                .withUsageDimension(dimension)
                .withUsageQuantity(quantity)
                .withTimestamp(timestamp);
        MeterUsageResult meterUsageResult = awsMarketplaceMetering.meterUsage(meterUsageRequest);
    }
}
```

次のコード例は、`UsageAllocations` で `MeterUsage` オペレーションを呼び出します。

```
private static String callMeterUsageWithAllocationsByTag(AWSMarketplaceMetering marketplaceMetering) {
        // Tag Keys for the product
        String tagKey1 = "Key1";
        String tagKey2 = "Key2";
        String tagKey3 = "Key3";

        // 1st Usage Allocation bucket which has two Tags [{Key1, Key1Value1},{Key2, Key2Value1}]
        List<Tag> tagsForUsageAllocation1 = Arrays.asList(new Tag().withKey(tagKey1).withValue("Key1Value1"),
                new Tag().withKey(tagKey2).withValue("Key2Value1"));
        UsageAllocation usageAllocation1 = new UsageAllocation()
                .withTags(tagsForUsageAllocation1)
                .withAllocatedUsageQuantity(20);

        // 2nd Usage Allocation bucket which has two Tags [{Key1, Key1Value2},{Key2, Key2Value1}]
        List<Tag> tagsForUsageAllocation2 = Arrays.asList(new Tag().withKey(tagKey1).withValue("Key1Value2"),
                new Tag().withKey(tagKey2).withValue("Key2Value1"));
        UsageAllocation usageAllocation2 = new UsageAllocation()
                .withTags(tagsForUsageAllocation2)
                .withAllocatedUsageQuantity(20);

        // 3rd Usage Allocation bucket which has two Tags [{Key1, Key1Value2},{Key2, Key2Value2},{Key3, Key3Value1}]
        List<Tag> tagsForUsageAllocation3 = Arrays.asList(new Tag().withKey(tagKey1).withValue("Key1Value2"),
                new Tag().withKey(tagKey2).withValue("Key2Value2"),
                new Tag().withKey(tagKey3).withValue("Key3Value1"));
        UsageAllocation usageAllocation3 = new UsageAllocation()
                .withTags(tagsForUsageAllocation3)
                .withAllocatedUsageQuantity(15);

        // 4th Usage Allocation bucket with no tags
        UsageAllocation usageAllocation4 = new UsageAllocation()
                .withAllocatedUsageQuantity(15);

        List<UsageAllocation> usageAllocationList = Arrays.asList(usageAllocation1,
                usageAllocation2,
                usageAllocation3,
                usageAllocation4);

        MeterUsageRequest meterUsageRequest = new MeterUsageRequest()
                .withProductCode("TestProductCode")
                .withUsageDimension("Dimension1")
                .withTimestamp(new Date())
                //UsageQuantity value must match with sum of all AllocatedUsageQuantity
                .withUsageQuantity(70)
                .withUsageAllocations(usageAllocationList);

        MeterUsageResult meterUsageResult;
        try {
            meterUsageResult = marketplaceMetering.meterUsage(meterUsageRequest);
        } catch (Exception e) {
            // Log Error
            throw e;
        }

        return meterUsageResult.getMeteringRecordId();
    }
```