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 snapshot.linux-magazine.com
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
bucket-policy.json
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).
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.
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 mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.
Infos
- "Programming Snapshot – Amazon Web Services" by Mike Schilli. Linux Pro Magazine, issue 196, 2017, pg. 52.
- Poccia, Danilo. AWS Lambda in Action. Manning, 2017
- "Perl – Video Preview" by Mike Schilli. Linux Pro Magazine, issue 195, 2017, pg. 52. http://www.linux-magazine.com/Issues/2017/195/Perl-Video-Preview
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/197
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.