Search code examples
c#aws-lambdaalexa-skills-kitsystem.net.httpwebrequest

Lambda Function using c# cannot invoke external HTTPS APIs


I am trying to invoke External APIs from AWS lambda function written in c#. The Lamda function is deployed in No VPC mode. I am calling this function from Alexa skill. The code works fine for an http request, but its not working for https.

The below code works when I use http://www.google.com.

But, if I replace http with https, then I get the error in the cloud watch saying:

"Process exited before completing request."

Even the log written in catch is not getting logged in cloud watch.

public class Function
{
    public const string INVOCATION_NAME = "bingo";

    public async Task<SkillResponse> FunctionHandler(SkillRequest input, ILambdaContext context)
    {
        var requestType = input.GetRequestType();
        if (requestType == typeof(IntentRequest))
        {
            string response = "";
            IntentRequest request = input.Request as IntentRequest;
            response += $"About {request.Intent.Slots["carmodel"].Value}";
            
            try
            {
                using (var httpClient = new HttpClient())
                {
                    Console.WriteLine("Trying to access internet"); 
                    //var resp=httpClient.GetAsync("http://www.google.com").Result // this works perfect!                   
                    var resp = httpClient.GetAsync("https://www.google.com").Result; // this throws error
                    Console.WriteLine("Call was successful");
                }                
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception from main function " + ex.Message);
                Console.WriteLine(ex.InnerException.Message);
                Console.WriteLine(ex.StackTrace);

            }            
            return MakeSkillResponse(response, true);
        }
        else
        {
            return MakeSkillResponse(
                    $"I don't know how to handle this intent. Please say something like Alexa, ask {INVOCATION_NAME} about Tesla.",
                    true);
        }
    }

    private SkillResponse MakeSkillResponse(string outputSpeech, bool shouldEndSession,
        string repromptText = "Just say, tell me about car models to learn more. To exit, say, exit.")
    {
        var response = new ResponseBody
        {
            ShouldEndSession = shouldEndSession,
            OutputSpeech = new PlainTextOutputSpeech { Text = outputSpeech }
        };

        if (repromptText != null)
        {
            response.Reprompt = new Reprompt() { OutputSpeech = new PlainTextOutputSpeech() { Text = repromptText } };
        }

        var skillResponse = new SkillResponse
        {
            Response = response,
            Version = "1.0"
        };
        return skillResponse;
    }
}

Solution

  • The issue was resolved by updating the library version.

    System.Net.Http v4.3.4 was not completely compatible with dotnet core v1.

    So outbound http calls were working but not https calls. Changing the version of System.net.http resolved the issue.