Skip to content

Instantly share code, notes, and snippets.

@twosdai
Last active June 19, 2022 01:03
Show Gist options
  • Save twosdai/d265708e590b5d9b2ac7a382fbd5b651 to your computer and use it in GitHub Desktop.
Save twosdai/d265708e590b5d9b2ac7a382fbd5b651 to your computer and use it in GitHub Desktop.
// Just run npm i @aws-sdk/client-pricing --save-dev
// I tested the below commands with Node Version: 14.19.2 on PopOS Linux
const {
PricingClient,
DescribeServicesCommand,
GetAttributeValuesCommand,
GetProductsCommand,
} = require('@aws-sdk/client-pricing');
const { promises } = require('fs');
/**
* Below are three functions, just comment them out when you only need the results from one.
* Specifically there are 3 different commands in the pricing API.
*
* DescribeServices
* GetAttributeValues
* GetProducts
*
* DescribeServices: gives you all the "Fields" which you can use as filters in GetProducts
*
* GetAttributeValues: gives you all the "Values" which can be used as filters for the specific Fields in GetProducts
*
* GetProducts enables us to request the AWS Billing / Offering API for specific offering information
*/
const run = async () => {
try {
const pricingClient = new PricingClient({ region: 'us-east-1' });
let next;
do {
const params = {
FormatVersion: 'aws_v1',
MaxResults: 1,
ServiceCode: 'AmazonEC2',
NextToken: next,
};
const command = new DescribeServicesCommand(params);
const { NextToken, Services } = await pricingClient.send(command);
next = NextToken;
console.log('Success', JSON.stringify(Services));
} while (next);
} catch (err) {
console.log('Error', err);
}
};
run();
const run2 = async () => {
try {
const pricingClient = new PricingClient({ region: 'us-east-1' });
let next;
do {
const params = {
FormatVersion: 'aws_v1',
ServiceCode: 'AmazonEC2',
AttributeName: 'marketoption',
NextToken: next,
};
const command = new GetAttributeValuesCommand(params);
const { NextToken, ...rest } = await pricingClient.send(command);
next = NextToken;
console.log('Success', JSON.stringify(rest));
} while (next);
} catch (err) {
console.log('Error', err);
}
};
run2();
const run3 = async () => {
try {
const pricingClient = new PricingClient({ region: 'us-east-1' });
let next;
let counter = 0;
do {
const params = {
Filters: [
{
Type: 'TERM_MATCH',
Field: 'ServiceCode',
Value: 'AmazonEC2',
},
{
Type: 'TERM_MATCH',
Field: 'regionCode',
Value: 'us-east-1',
},
{
Type: 'TERM_MATCH',
Field: 'instanceType',
Value: 't3.medium',
},
{
Type: 'TERM_MATCH',
Field: 'marketoption',
Value: 'OnDemand',
},
{
Type: 'TERM_MATCH',
Field: 'operatingSystem',
Value: 'Linux',
},
{
Type: 'TERM_MATCH',
Field: 'tenancy',
Value: 'Shared',
},
],
FormatVersion: 'aws_v1',
NextToken: null,
ServiceCode: 'AmazonEC2',
};
const command = new GetProductsCommand(params);
const { NextToken, ...rest } = await pricingClient.send(command);
const filteredResults = rest.PriceList.map((element) => {
const {
terms: { OnDemand },
} = JSON.parse(element);
const keys = Object.keys(OnDemand);
keys.forEach((key) => {
const res = OnDemand[key];
console.log(res.priceDimensions);
});
});
next = NextToken;
await promises.writeFile(`./temp${counter}.json`, JSON.stringify(rest));
counter++;
} while (next);
} catch (err) {
console.log('Error', err);
}
};
run3();
// This is just a reference for myself, its a save of the run1 command console log
const PricingClientResponseReference = {
$metadata: {
httpStatusCode: 200,
requestId: '7c0231e9-f107-430f-9cea-6fb4dccea337',
attempts: 1,
totalRetryDelay: 0,
},
FormatVersion: 'aws_v1',
NextToken:
'trqBNI42YvhF5nj9Sm4MdQ==:DkGS3RdOMWAU7aLtfc9Nur1DHI5ZTe3kNkjKdzaQBRM91387bqpPF9SeY1r9rymOGE0TrDYIy5X5Q1YShho6LtlNMV8uBS141YGd+4B/QG+GDjYSiMm4pBHIYIlvVQ1FFsAUyA9NiAzNB8gtjX0spzhDfW59jl88xMRfj0xN6Fm4hwlOaLjFRMyUDNfUeN74',
Services: [
{
AttributeNames: [
'instanceCapacityMetal',
'volumeType',
'maxIopsvolume',
'instance',
'classicnetworkingsupport',
'instanceCapacity10xlarge',
'fromRegionCode',
'locationType',
'toLocationType',
'instanceFamily',
'operatingSystem',
'toRegionCode',
'clockSpeed',
'LeaseContractLength',
'ecu',
'networkPerformance',
'instanceCapacity8xlarge',
'group',
'maxThroughputvolume',
'gpuMemory',
'ebsOptimized',
'vpcnetworkingsupport',
'maxVolumeSize',
'gpu',
'intelAvxAvailable',
'processorFeatures',
'instanceCapacity4xlarge',
'servicecode',
'groupDescription',
'elasticGraphicsType',
'volumeApiName',
'processorArchitecture',
'physicalCores',
'fromLocation',
'snapshotarchivefeetype',
'marketoption',
'availabilityzone',
'productFamily',
'fromLocationType',
'enhancedNetworkingSupported',
'intelTurboAvailable',
'memory',
'dedicatedEbsThroughput',
'vcpu',
'OfferingClass',
'instanceCapacityLarge',
'capacitystatus',
'termType',
'storage',
'toLocation',
'intelAvx2Available',
'storageMedia',
'regionCode',
'physicalProcessor',
'provisioned',
'servicename',
'PurchaseOption',
'instancesku',
'productType',
'instanceCapacity18xlarge',
'instanceType',
'tenancy',
'usagetype',
'normalizationSizeFactor',
'instanceCapacity16xlarge',
'instanceCapacity2xlarge',
'maxIopsBurstPerformance',
'instanceCapacity12xlarge',
'instanceCapacity32xlarge',
'instanceCapacityXlarge',
'licenseModel',
'currentGeneration',
'preInstalledSw',
'transferType',
'location',
'instanceCapacity24xlarge',
'instanceCapacity9xlarge',
'instanceCapacityMedium',
'operation',
'resourceType',
],
ServiceCode: 'AmazonEC2',
},
],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment