2

i was trying to use Lambda@Edge to handle A/B testing in my site. i wonder is there a way to let Lambada@Edge functions load an external config data from a url, eg. i created an api to return the traffic rates of A/B channels,i want control that config data outside, so that i can dynamically adjust the traffics flow to A or B channel and no need to modify the Lambda function. what i did now is

var versions = [];
var isLoadingVersionData = false;
const https = require('https');
function loadVersions() {
if (isLoadingVersionData)
    return null;

isLoadingVersionData=true;

https.get('https://example.com/getAbTestConfig', (res) => {
    res.on('data', (d) => {
        var parsedBody = JSON.parse(d);
        if (parsedBody.status)
            versions = parsedBody.data;

    });

    }).on('error', (e) => {
       console.log(e);
    });
}

//and load the function in handler
exports.handler = (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false;
    loadVersions();
}

i wonder this variable "versions" can be loaded correctly and shared in each later requests

do you have some more effectually solutions?

Kent Wood
  • 1,392
  • 2
  • 14
  • 30

2 Answers2

0

Why not maintain this data in S3 and use your Lambda@Edge to get the configuration from there? Further to reduce latency, you can front the S3 bucket containing traffic ratio with CloudFront and have your L@E make a call to CloudFront and get the desired value.

Mr.Ocean
  • 68
  • 1
  • 5
0

I was facing the same issue but not for A/B testing. I just created a json file in my lambda function to avoid delay of making http calls inside lambda functions. It works but the maintenance is not good, as every time when I need to change the Json file I need deploy the lambda function again.

While I was searching for it, I found same solution described for Mr.Ocean above, sounds like a good alternative to maintain the data in S3.

Lucas Santos
  • 2,991
  • 3
  • 19
  • 29