跳至內容

aws lex : how much RecognizeText are requested ?

0

I am trying to calculate how much lex costs.

i found RecognizeText reqeusts that i request are saved in AWS.

i am gonna use it to calculate costs. ( 0.00075$ per RecognizeText )

what i am asking are two things.

  1. how can i get the number of how much RecognizeText i have requested ?

  2. is there other way to get how much costs for each bot ?

i am using AWS JavaScript SDK. just consider it for me.

thanks!

已提問 2 年前檢視次數 363 次
1 個回答
0

Here's a general outline of how you can achieve this using the AWS JavaScript SDK:

Enable CloudWatch Metrics: Ensure that CloudWatch metrics are enabled for your Amazon Lex bot. You can enable this option in the Amazon Lex console or via the AWS CLI. https://docs.aws.amazon.com/lex/latest/dg/gs-cli.html

Retrieve Metrics Data: Use the getMetricData method from the AWS CloudWatch SDK to retrieve metric data for your Amazon Lex bot. You'll need to specify the Namespace, MetricName, Dimensions, and StartTime and EndTime parameters to filter the metric data appropriately.

Calculate Total Cost: Once you have retrieved the number of RecognizeText requests, you can calculate the total cost by multiplying the number of requests by the cost per request (0.00075$ per RecognizeText).

example of how you can achieve this using the AWS JavaScript SDK

const AWS = require('aws-sdk');

// Configure AWS credentials
AWS.config.update({ region: 'your-region', accessKeyId: 'your-access-key', secretAccessKey: 'your-secret-key' });

// Create CloudWatch service object
const cloudwatch = new AWS.CloudWatch();

// Define parameters for GetMetricData API operation
const params = {
  MetricDataQueries: [
    {
      Id: 'm1',
      MetricStat: {
        Metric: {
          Namespace: 'AWS/Lex',
          MetricName: 'BilledTextSentCount',
          Dimensions: [
            {
              Name: 'BotName',
              Value: 'your-bot-name'
            },
            {
              Name: 'BotAlias',
              Value: 'your-bot-alias'
            }
          ]
        },
        Period: 3600, // One hour
        Stat: 'Sum'
      }
    }
  ],
  StartTime: new Date(Date.now() - 3600000), // One hour ago
  EndTime: new Date(),
  ScanBy: 'TimestampDescending'
};

// Retrieve metric data
cloudwatch.getMetricData(params, function(err, data) {
  if (err) {
    console.log('Error', err);
  } else {
    const metricData = data.MetricDataResults[0];
    const sum = metricData.Values[0];
    const totalRequests = sum.Value;
    const costPerRequest = 0.00075;
    const totalCost = totalRequests * costPerRequest;

    console.log('Total RecognizeText requests:', totalRequests);
    console.log('Total cost:', totalCost.toFixed(2), '$');
  }
});

Replace 'your-region', 'your-access-key', 'your-secret-key', 'your-bot-name', and 'your-bot-alias' with your actual AWS region, access key, secret key, bot name, and bot alias respectively.

專家
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。