def update_cfdistro()

in lambda/lambda_function.py [0:0]


def update_cfdistro(distroid, headervalue):
    
    client = boto3.client('cloudfront')
    diststatus = get_cfdistro(distroid)
    if 'Deployed' in diststatus['Distribution']['Status']:
        distconfig = get_cfdistro_config(distroid)
        headercount = 0
        #logger.info(distconfig)
        for k in distconfig['DistributionConfig']['Origins']['Items']:
            if k['CustomHeaders']['Quantity'] > 0:
                for h in k['CustomHeaders']['Items']:
                    if HeaderName in h['HeaderName']:
                        logger.info("Update custom header, %s for origin, %s." % (h['HeaderName'], k['Id']))
                        headercount = headercount + 1
                        h['HeaderValue'] = headervalue
                    
                    else:
                        logger.info("Ignore custom header, %s for origin, %s." % (h['HeaderName'], k['Id']))
                        pass

            else:
                logger.info("No custom headers found in origin, %s." % k['Id'])
                pass
        
        if headercount < 1:
            logger.error("No custom header, %s found in distribution Id, %s." % (HeaderName, distroid))
            raise ValueError("No custom header found in distribution Id, %s." % distroid)
        
        else:
            response = client.update_distribution(
                Id = distroid,
                IfMatch = distconfig['ResponseMetadata']['HTTPHeaders']['etag'],
                DistributionConfig = distconfig['DistributionConfig']
                )

            return response
                
    else:
        logger.error("Distribution Id, %s status is not Deployed." % distroid)
        raise ValueError("Distribution Id, %s status is not Deployed." % distroid)