question

Will Strain avatar image
Will Strain asked

Getting Lambda to work

Ok so I have been working on a project for the last like two weeks and as far as I can tell Lambda can't communicate other than with the echo. I tried getting it to talk to DynamoDB, SNS, SQS and now trying to get it to just go to a simple URL (for a rest api so I can have it talk to my Arduino). I can't get it to do any of those. I really need it to just go to the URL it doesn't even have to get any data back. Has anyone gotten their lambda code to actually talk to anything other than their echo? If so how? I really want to get my code to work. Also if anyone has a good alternative to lambda I would also be interested (preferably free). Thank you PS: I know I am able to access the URL from outside of my home network, I was able to get my phone to get to it via cellular data. Also the lambda says the thing worked but on the other end nothing happened so it clearly didn't but no error to look at.
alexa skills kitsubmission testing certification
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
I've been running into problems with Lambda. From reading other things and experimental evidence, I think it forks off a new instance each time it is called, and kills all background threads as soon as it exits. My knock-knock joke example gets getting scrambled. Stars one joke, then starts a second one, then finishes the first one. Global variables are not being maintained. So it must round-robin or something against JVMs. I don't know if this is the root cause of your problems, or if it just keeps you in its own sandbox. I'm going to try to get it to talk to DynamoDB and a URL today, and I'll report back.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
I got a simple URL to work. What I've done is created a Lambda Function that only does a pass-through to an AWS web service. I like the statefullness of my AWS web service, but the SSL cert is a pain. So this way I get both (with a little latency). The first time I tried I got a big error stack about certifications. So I slipped in some code from EchoSim to turn off Certification Validation and it worked a charm. I've included it below. See if that works for you. I'll try DynamoDB next... [code] private void disableCertificateValidation() { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) { } } }; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (GeneralSecurityException e) { } // Now you can access an https URL without having the certificate in the truststore } [/code]
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Will Strain avatar image
Will Strain answered
My url isn't https, so I don't think that was the issue. Could you maybe give me the code for how you actually sent out the request? Possibly including how it calls up the function you made?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
Sure, here's a class that just passes through the entire request to a URL end point. I'll probably be adding it to the samples side (and to a new section of the book!) in a bit. It's handy if you want to deploy your skill as a HTTP endpoint, but you don't want to do all the messing with SSL certificates... [code] import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.security.GeneralSecurityException; import java.security.cert.X509Certificate; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import com.amazonaws.services.lambda.runtime.Context; public class PassThruHander { public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (;;) { int ch = inputStream.read(); if (ch == -1) break; baos.write(ch); } byte[] inbuf = baos.toByteArray(); disableCertificateValidation(); byte[] output = makeRequest(inbuf); outputStream.write(output); } private byte[] makeRequest(byte[] body) throws IOException { URL serviceURL = new URL(" https://echodevtestenv-65qiix3m3d.elasticbeanstalk.com/cc"); HttpURLConnection con = (HttpURLConnection)serviceURL.openConnection(); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); con.setRequestProperty("Accept", "application/json"); con.setRequestProperty("Accept-Charset", "utf-8"); con.setRequestProperty("Content-Length", String.valueOf(body.length)); OutputStream os = con.getOutputStream(); os.write(body); os.close(); InputStream is = con.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (;;) { int ch = is.read(); if (ch == -1) break; baos.write(ch); } is.close(); byte[] data = baos.toByteArray(); return data; } private void disableCertificateValidation() { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) { } } }; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (GeneralSecurityException e) { } // Now you can access an https URL without having the certificate in the truststore } } [/code]
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.