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
asked a year ago816 views
2 Answers
1

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

AWS
EXPERT
Peter_G
answered a year ago
profile picture
EXPERT
iwasa
reviewed a year ago
  • 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
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions