def getSpotPrice()

in resource-selector-lambda/handler.py [0:0]


    def getSpotPrice(self):
        spotPriceValue = []
        if not self.instanceType:
            logger.error("Error: InstanceType argument missing")
            self.errorMessage = "InstanceType argument missing"
            self.setOutput(spotPriceValue)
            self.error = 'failed'
            return None

        if not self.valid_instance_type(self.instanceType):
            logger.error("Error: Invalid Instance Type specified")
            self.errorMessage = "Invalid Instance Type specified"
            self.setOutput(spotPriceValue)
            self.error = 'failed'
            return None

        #Check if instanceOS has been provided as an argument
        if not self.instanceOS:
            logger.error("Error: IntanceOS argument missing")
            self.errorMessage = "IntanceOS argument missing"
            self.setOutput(spotPriceValue)
            self.error = 'failed'
            return None

        # Validate the value of instance OS
        if self.instanceOS not in ("Linux", "Windows", "RHEL"):
            logger.error("Error: Invalid InstanceOS value")
            self.errorMessage = "Invalid InstanceOS value"
            self.setOutput(spotPriceValue)
            self.error = 'failed'
            return None

        # setup pricing connection
        spot = boto3.client('pricing', region_name=self.region)

        # get name of current region
        region_filter = REGION_MAP.get(self.region)

        if not region_filter:
            logger.error("Error: Unsupported region")
            self.errorMessage = "Unsupported region"
            self.setOutput(spotPriceValue)
            self.error = 'failed'
            return None

        # call for result with proper filters
        result = spot.get_products(
            ServiceCode='AmazonEC2', # specify the service name
            Filters=[
                    {
                        'Type'  : 'TERM_MATCH',
                        'Field' : 'instanceType',       # Filter on instance type
                        'Value' : self.instanceType
                    },
                    {
                        'Type'  : 'TERM_MATCH',
                        'Field' : 'location',           # Filter on the region
                        'Value' : region_filter
                    },
                    {
                        'Type'  : 'TERM_MATCH',
                        'Field' : 'operatingSystem',    # Filter on the operating system
                        'Value' : self.instanceOS
                    },
                    {
                        'Type'  : 'TERM_MATCH',
                        'Field' : 'tenancy',            # Filter on tenancy
                        'Value' : 'Shared'              # Value should always be 'Shared'
                    },
                    {
                        'Type'  : 'TERM_MATCH',
                        'Field' : 'preInstalledSw',     # Filter on the pre-installed software
                        'Value' : 'NA'                  # Value should always be 'NA'
                    },
                    {
                        'Type'  : 'TERM_MATCH',
                        'Field' : 'licenseModel',       # Filter on the license model
                        'Value' : 'No License required' # Value should always be 'No License required'
                    },
                    {
                        "Type": "TERM_MATCH",
                        "Field": "termType",
                        "Value": "OnDemand"
                    },
                    {
                        "Type": "TERM_MATCH",
                        "Field": "capacitystatus",
                        "Value": "UnusedCapacityReservation"
                    }
                ],
            )['PriceList'] # Take the PriceList portion of the response [what we need]

        result_object = json.loads(result[0])
        on_demand = result_object['terms']['OnDemand'] # Traverse down to get OnDemand Price
        on_demand = on_demand[list(on_demand.keys())[0]]['priceDimensions'] # Get the price Dimensions
        on_demand = float(on_demand[list(on_demand.keys())[0]]['pricePerUnit']['USD']) # Get the US Dollar Amount
        spotPrice = '{0:.3g}'.format(on_demand * 0.95)

        # return spot price
        if spotPrice:
            logger.info(f'Spot Price value determined: {spotPrice}')
            spotPriceValue.append(spotPrice)
        else:
            logger.error("Issue determining spot price.")

        # turn list into a comma separated string and place it in our response
        self.setOutput(spotPriceValue)