How can I access a local API using Amazon Alexa

Matt Clark picture Matt Clark · May 19, 2017 · Viewed 9k times · Source

I intend to build a set of skills for Amazon Alexa that will integrate with a custom software suite that runs on a RaspberryPi in my home.

I am struggling to figure out how I can make the Echo / Dot itself make an API call to the raspberry pi directly - without going through the internet, as the target device will have nothing more then an intranet connection - it will be able to receive commands from devices on the local network, but is not accessible via the world.

From what I have read, the typical workflow is as follows

Echo -> Alexa Service -> Lambda

Where a Lambda function will return a blob of data to the Smart Home device; using this return value

Is it possible, and how can I make the Alexa device itself make an API request to a device on the local network, after receiving a response from lambda?

Answer

Alex Q picture Alex Q · May 24, 2017

I have the same problem and my solution is to use SQS as the message bus so that my RaspberryPi doesn't need to be accessible from the internet.

Echo <-> Alexa Service <-> Lambda -> SQS -> RaspberryPi
                             A                 |
                             +------ SQS <-----+

This works fine as long as:

  • you enable long polling (20sec) of SQS on the RaspberryPi and set the max messages per request to 1
  • you don't have concurrent messages going back and forth between Alexa and the RaspberryPi

This give the benefit of:

  • with a max message size of 1 the SQS request will return as soon as one message is available in the queue, even before the long poll timeout is met
  • with only 1 long polling at a time to SQS for the entire month this fit under the SQS free tier of 1 million requests
  • no special firewall permission for accessing your RaspberryPi from the internet, so the RaspberryPi's connection from the lambda always "just works"
  • more secure than exposing your RaspberryPi to the internet since there are no open ports exposed for malicious programs to attack