public static string CreateFunctionIfNotExists(AmazonLambdaClient client, string functionName, string functionHandlerName, string code, string iamRoleArn)
{
AutoResetEvent ars = new AutoResetEvent(false);
Exception responseException = new Exception();
string functionArn = UtilityMethods.GetFunctionArnIfExists(client, functionName);
if (functionArn == null)
{
int retries = 3;
long codeSize = -1;
while (retries > 0)
{
client.CreateFunctionAsync(new CreateFunctionRequest
{
FunctionName = functionName,
Code = new FunctionCode
{
ZipFile = GetScriptStream(code)
},
Handler = functionHandlerName + ".handler",
Runtime = Runtime.Nodejs,
Description = "Feel free to delete this function. The tests will recreate it when needed.",
Role = iamRoleArn
}, (response) =>
{
responseException = response.Exception;
if (responseException == null)
{
functionArn = response.Response.FunctionArn;
codeSize = response.Response.CodeSize;
}
ars.Set();
}, new AsyncOptions { ExecuteCallbackOnMainThread = false });
ars.WaitOne();
if (responseException == null)
{
break;
}
else
{
Utils.AssertTrue(responseException is InvalidParameterValueException);
// Need to wait longer for eventual consistency of role
retries--;
Thread.Sleep(TimeSpan.FromSeconds(10));
}
}
Assert.IsNotNull(functionArn);
Utils.AssertTrue(codeSize > 0);
}
return functionArn;
}