Set up Amazon Web Services – Part 2

Safely Stored

Once the montage.jpg contact sheet has been created, the code from line 28 in Listing 2 copies the file from the temporary directory to a previously created S3 bucket on Amazon's cloud storage system. The boto3 Python module is available by default on Lambda servers and offers various tools for communication with related service offerings.

The put_object() method in line 32 stores the output file read from the virtual disk as a type image/jpeg object in the cloud storage. From there, the S3 web server discussed in the previous issue delivers it to interested users under the URL the API returned. For this, lines 36 to 39 compose a JSON response, which tells the web client which S3 URL refers to the montage results file. In line 41, all that remains is to delete the temporary directory created previously.

To give the Lambda script write permissions for the S3 bucket, the user needs to enable write access for the latter. Figure 5 shows that the S3 bucket grants write access to any designated AWS user. On the other side of the wall, files generated by the Lambda server in an S3 bucket must be globally readable for interested users. This is done using a bucket policy, the contents of which are shown in Listing 7. Each newly created file is thus readable for all; that is, the web server in the S3 bucket can deliver the file to any requesting web client.

Listing 7



Figure 5: The Lambda server needs access privileges for the S3 bucket.

Gateway to the World

Amazon helps you test Lambda functions; developers can either run uploaded scripts via the aws command-line utility or press the console's Test button in the browser. Ultimately, users need to be able to access functions via the web and Amazon's API Gateway. This service, which can also be configured in the console, creates a cloud server with a REST API, whose methods (as in the /vimo example shown) it redirects to user-defined Lambda functions, among other options.

AWS handles the connection between the web server and the application on the Lambda Service behind the scenes when the user stipulates the Lambda Function option as the Integration Type when creating the REST method (e.g., GET or POST), and defining the data center region and the name of the Lambda function further down (e.g., vimo; Figure 6).

Figure 6: Creating the REST method with GET.

In this case, the client will use the path /vimo to POST data in JSON format with named parameters like movie_url. If the web client, as shown in Figure 1, sets the Content-Type header of the request to application/json, then the API Gateway will expect JSON and decode it for consumers down the pipeline.

The Lambda function called later then receives the decoded value pairs from the JSON enchilada in the event function parameter as a Python dictionary. The client in Figure 1 provides the URL for the surveillance movie to be analyzed in JSON as movie_url , while the Lambda function in Listing 2 uses event['movie_url'] to access it.

The REST API does not go live until the user clicks on the Deploy API function in the context menu below API Actions (Figure 7) and selects a production environment (called "Stage"), for example Beta. In the browser, AWS then displays the URL of where to reach the newly configured web service.

Figure 7: Going live with the Deploy API function.

If you are installing in a production environment, the use of API tokens that manage access to the API is recommended. You can also slow down the access rate (e.g., to 1000 requests/second) to prevent an unexpected cost explosion if the link spreads like a wildfire.

While I was working on this article, I kept a watchful eye on the costs incurred, but they stayed within the "Free Tier" scope; it only cost me $0.01 to cover the bandwidth consumption for uploading the continuously updated and improved ZIP files with the test code and the libs.

Mike Schilli

Mike Schilli works as a software engineer in the San Francisco Bay Area. He can be contacted at Mike's homepage can be found at


  1. "Programming Snapshot – Amazon Web Services" by Mike Schilli. Linux Pro Magazine, issue 196, 2017, pg. 52.
  2. Poccia, Danilo. AWS Lambda in Action. Manning, 2017
  3. "Perl – Video Preview" by Mike Schilli. Linux Pro Magazine, issue 195, 2017, pg. 52.
  4. Listings for this article:

Buy this article as PDF

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

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl – Video Preview

    Rather than stare at boring surveillance videos, in which nothing happens 90 percent of the time, Mike Schilli tries the OpenCV image recognition software, which automatically extracts the most exciting action sequences.

  • Python 3

    What do Python 2.x programmers need to know about Python 3?

  • Out-of-Bounds Photos

    Including out-of-bounds effects in slide shows and presentations is bound to get the undivided attention of the audience. Gimp has simple tools to create these image effects.

  • Manage Amazon S3 with s3cmd
  • PSP Movies

    You can use Linux to load movies on your a Playstation Portable (PSP) gaming console.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95