Set up Amazon Web Services

Hidden Lambda

If you want to run programs on Amazon's back-end servers instead of just serving static web pages, you can opt for the Lambda offering. It runs JavaScript, Python, and Java functions in isolated containers, triggered either via a web API or associated with events from other services.

For example, Amazon's dynamic database Dynamo can generate an event if a new data record arrives; this in turn triggers a Lambda function that performs further steps in the workflow. A cloud application defined this way thus does not consist of a flow that is orchestrated by the program logic but is formed by linking individual components and their events to create an overall architecture.

In the world of Lambda, the Python script in Listing 1 [5] provides a test function. On the web console, you need to press the Lambda option in the Computing section to do this. After showing an overview, the service prompts you to Select blueprint for the test function. For your tests, select the Blank function and skip the next page, Configure triggers; then, enter a name for the Lambda function on the following page (as shown in Figure 7; wellHelloThere in this case) and copy the code from Listing 1 to the text box shown below the Edit code inline drop-down.

Listing 1

greet.py

 

Figure 7: Preparation of the test script in Python as a Lambda function.

On the following page (Figure 8), the console has already entered the name of the handler function. Because a run-time environment might include multiple files with many functions, you need to specify both the filename and the function it contains here.

Figure 8: AWS assigns access rights as a role.

Leave the default Role for execution rights – that is, Create new role from template(s) – and specify a suitable name later (myBasicExecutionRole in this case). After confirming, Amazon installs the Lambda function in the cloud and lets the user test it (Figure 9). You can add some parameters in JSON format, which the script dynamically evaluates later.

Figure 9: The test console launching a Lambda function programmed in Python.

The aws command-line client also has access to the Lambda script. As the call in Figure 10 shows, the tool takes the name of the function previously defined in the Web UI (wellHelloThere; i.e., not the name of the Python function) and a JSON hash with input parameters as --payload; this remains empty in the test case. Later, any parameters provided by the web server end up in the event parameter of the Python function that then interprets them dynamically.

Figure 10: The command line can call the Lambda function.

Now you need to teach the Lambda script to accept the URL with a video as input, fetch it off the web, and run the motion analysis program [2] on it. This requires more than a simple Python script without dependencies; in fact, you need a run-time environment with the OpenCV library and a precompiled static binary installed. To discover how this works and how to package the results, feed them to the cloud, and define a web service that both triggers the procedure and returns the results as an image file, tune in to next month's column.

Mike Schilli

Mike Schilli works as a software engineer in the San Francisco Bay Area. He can be contacted at mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.

Infos

  1. AWS usage rates for free operation: https://aws.amazon.com/free
  2. "Video Preview" by Mike Schilli, Linux Pro Magazine, issue 195, February 2017, p. 52, http://www.linuxpromagazine.com/Issues/2017/195/Perl-Video-Preview
  3. Poccia, Danilo. AWS Lambda in Action, Manning, 2017
  4. Rady, Ben. Serverless Single Page Apps: Fast, Scalable and Available, The Pragmatic Bookshelf, 2016
  5. Listings: ftp://ftp.linux-magazine.com/pub/listings/magazine/195

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Programming Snapshot – Alexa

    Asking Alexa only for built-in functions like the weather report gets old quickly, and add-on skills from the skills store only go so far. With a few lines of code, Mike teaches this digital pet some new tricks.

  • Red Hat Provides Cloud Access Option

    The Red Hat distro extended its subscriptions with the choice of Linux running on the local server or from the Amazon cloud.

  • Data Security in AWS

    As a cloud market leader, Amazon Web Services has had to  put a great deal of thought into data security. Encryption options and key management play an important role.

  • E-Commerce and Angry Birds

    Thanks to the scalability of Amazon's web services, the Angry Birds shop and its databases do not fail even when visited by a teaming mass of bird lovers.

  • IBM Emerges in Amazon's Cloud

    Big Blue is now providing images for Amazon's cloud. A cloud software-as-service (SaaS) for SUSE Linux, among other products, is to follow.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News