Is there a way to get an on-demand price list for EC2 on .NET?

0

Using the C# SDK, I can only find spot prices and not on-demand prices. Can this be done?

combii
已提问 1 年前712 查看次数
2 回答
1

Have you tried using the pricing API. Link in .NET SDK

AWS
专家
Peter_G
已回答 1 年前
profile picture
专家
iwasa
已审核 1 年前
  • Thank you for the response Peter. I tried looking into it with the following code, but I am getting a timeout when calling the GetProducts method. Added filters to make the request smaller as I thought the retrieval of information was too large and might be causing it to time out, but didn't seem to help.

    What is the cause of this?

            // Create a request to get the price for the specified instance type
    
            string instanceType = "t3.medium";
            var request = new GetProductsRequest
            {
                ServiceCode = "AmazonEC2",
                Filters = new List<Amazon.Pricing.Model.Filter>
                {
                    new()
                    {
                        Field = "instanceType",
                        Type = FilterType.TERM_MATCH,
                        Value = instanceType
                    },
                    new()
                    {
                        Field = "location",
                        Type = FilterType.TERM_MATCH,
                        Value = "EU (Ireland)"
                    }
                },
                FormatVersion = "aws_v1"
            };
    
            // Make the request to get the price
            var response = await pricingClient.GetProductsAsync(request);
    
  • Not exactly sure whats wrong with code (I am not .NET dev). Using AWS cli I got a response in < 2 secs using the same filters as you used

  • Could you please send me the cli command you used?

0

As mentioned in the answer by @Peter_G, you can use the Pricing API which is also available in the .NET SDK.

Make sure you configure the client to use a correct endpoint for calling the service.

As mentioned in the documentation:

The AWS Price List Query API provides the following two endpoints:

So you have to ensure your client is configured for either us-east-1 or ap-south-1 as region. Here is an example:

using Amazon.Pricing;
using Amazon.Pricing.Model;
using Amazon.Runtime.CredentialManagement;

var credChain = new CredentialProfileStoreChain();
credChain.TryGetAWSCredentials("your_profile_name", out var awsCredentials);

var client = new AmazonPricingClient(awsCredentials, Amazon.RegionEndpoint.USEast1);
var response = await client.GetProductsAsync(new GetProductsRequest
{
    ServiceCode = "AmazonEC2"
});

response.PriceList.ForEach(s => Console.WriteLine(s));
profile pictureAWS
已回答 1 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则