You can write some code to calculate that, there is no direct API available to calculate by instance id.
What you can do is combine various API calls:
- You can use DescribeServices
, you get all the
attributes
of the all the services or if you want to have for one particular you can provide the name. Boto3 call describe_services
Returns the metadata for one service or a list of the metadata for all services
response = client.describe_services(
FormatVersion='aws_v1',
MaxResults=1,
ServiceCode='AmazonEC2',
)
Above call gives you the necessary attributes you want to create filters for the next step
{
'FormatVersion': 'aws_v1',
'NextToken': 'abcdefg123',
'Services': [
{
'AttributeNames': [
'volumeType',
'maxIopsvolume',
'instanceCapacity10xlarge',
'locationType',
'operation',
],
'ServiceCode': 'AmazonEC2',
},
],
'ResponseMetadata': {
'...': '...',
},
}
- Then you need to use GetAttributeValues to determine the possible values of the attributes. Boto3 call get_attribute_values
response = client.get_attribute_values(
AttributeName='volumeType',
MaxResults=2,
ServiceCode='AmazonEC2',
)
print(response)
{
'AttributeValues': [
{
'Value': 'Throughput Optimized HDD',
},
{
'Value': 'Provisioned IOPS',
},
],
'NextToken': '',
'ResponseMetadata': {
'...': '...',
},
}
- And finally depending on the attributes collected in the earlier step you can build a filter for get_producs
FLT = '[{{"Field": "tenancy", "Value": "shared", "Type": "TERM_MATCH"}},'\
'{{"Field": "operatingSystem", "Value": "{o}", "Type": "TERM_MATCH"}},'\
'{{"Field": "preInstalledSw", "Value": "NA", "Type": "TERM_MATCH"}},'\
'{{"Field": "instanceType", "Value": "{t}", "Type": "TERM_MATCH"}},'\
'{{"Field": "location", "Value": "{r}", "Type": "TERM_MATCH"}},'\
'{{"Field": "capacitystatus", "Value": "Used", "Type": "TERM_MATCH"}}]'
data = client.get_products(ServiceCode='AmazonEC2', Filters=json.loads(f))
- Once you have the price of the instance depending on your instance start time, you just have to do simple multiplication to get the total cost.
Use boto3 to price for given EC2 instance type
get ec2 pricing programmatically?