Cloud_Platforms___Lambda

CLOUD PLATFORMS –AWS
PRACTICAL DEMOS

Demonstration 3: AWS Lambda This is a summary of the steps shown in class to use define a new
AWS Lambda function.
Creating the Lambda function
Go to the AWS Lambda console. In the left pane, click on Functions, and then on Create function .
Give the function a name
Under Runtime▽ select Python3.9 Click on Create function
That creates the function and brings us to the page where we can edit it.
Editing the function
If you scroll down, you’ll see an editor where you can enter python code.
Enter a simple code, such as a line that returns a string. For a more elaborate example, you can copy the contents of the file lambda_function.py provided in Moodle, and save by clicking CTRL+S.
To become effective, changes need to be deployed. Note the mes- sage “Changes not deployed” above. Click on Deploy .
Testing the function
Lambda functions can be configured to run periodically or in re- sponse to an event (the “trigger”), such as uploading a new file to a S3 bucket.
Github
Before configuring the trigger, we can simulate a call to our func- tion in the button Test▽ above.
The first time we click on Test▽ , it will ask us to configure a test event. We are not going to use this here but is a mandatory step1. So the first time the window “Configure Test Event” appears, just enter a name for the test event and click on Save .
Click again on Test . Now the function is executed. If your code contained a simple function, you should see the output here.
If you used the function provided in Moodle, at this point it is going to display an error message such as “module feedparser not found”. This is because feedparser is an external python package. For Lambda functions to be able to use external packages, we need to bring these packages to the environment. This is covered in the next section2 .
Installing the environment
Installing the environment means to include a zip file which contains the packages that the Lambda function needs.
In this section, we are going to see how to add a readily available zip file (use the zip file provided in Moodle). The zip file contains the module feedparser which is required by the code of the Lambda function. The Appendix describes how to do generate that file by yourself.
The process is a) to create a so-called “Layer”, and b) to associate the layer to the lambda function. Regarding step a:
1 In events that have parameters, this will allow to enter the parameters we want to simulate.
2 Similarly to when we do pip install, but it’s slightly different for Lambda
cloud platforms – aws practical demos 3
In the left pane of the AWS Lambda dashboard, under Additional resources▽, click on Layers.
Click on Create Layer .
In the next screen, enter a name for the layer.
Make sure “Upload from a zip file” is selected, and click on Upload .
Select the provided zip file.
In “Compatible runtimes”, select Python 3.93. Finish by clicking on Create .
Now, to associate the layer to the lambda function:
Navigate back to your function. The top of the page should look like Fig. 1. Click on “Layers (0)”.
3 This is optional, but will make our lives easier in a later step.

Code Help
josé a. rodríguez-serrano, esade
In the following page, click on Add a layer .
In the following page, select “Custom layers”.
Expand the Choose▽ dropdown, and the name of the layer you just created should appear 4.
Under Version, select “1” and click on Add .
4 If you had not selected Python in step 6 of the previous list, the layer would not appear here.
Figure 1: Caption
Now if you scroll down to see the code and click on Test , the function should run properly.
Appendix: Putting your dependencies into a zip file
This part is optional, but you might want to practice it if you use lambda functions in your final project.
If you use a lambda function that imports an external module (such as feedparser in the example above, or requests that we used in the previous demonstrations), you need to include these packages in the environment.
The problem is that AWS does not offer a way to do the pip install in the Lambda environment.
The process is as follows:
1. Open a terminal in your laptop. From a folder of your choice,
create a new folder called python.
2. Use pip to install the desired packages, but adding the option
-t python. This specifies that the packages should be installed in that specific folder. For instance, for the package feedparser used above, that would read
$ pip install -t python feedparser

程序代写 CS代考 加QQ: 749389476
Optional step. Inside the python folder, remove the folders that end with dist-info (this will just save space but is not strictly necessary).
Compress the directory python into a zip file. You can do it with the utilities available in your operating system. If you are working in Mac or Linux you can also do it from the command line:
$ zip -r python.zip python
Now that’s the zip file that you have to upload, as shown in the previous step.
Advanced note: packages with pre-compiled code
The procedure above should work for many python packages. How- ever, a number of packages, such as numpy or pandas, contain pre- compiled code5. Because AWS Lambda uses a Linux operating sys- tem, Lambda needs the Linux-specific version of the package. But if you have a laptop with a non-Linux operating system, it is likely that the python package that you install is not compatible.
Therefore, if following the above procedure does not work, there are two workarounds.
Using Cloud Shell
5 they call functions that depend on the operating system
6 CloudShell has no additional cost but the usual pricing policies of AWS apply (e.g. when you download this file, it is counted as outbound transfer).
7 Type “CloudShell” in the AWS ser- vices search box
1. 2. 3. 4.
Open a Cloud Shell6 in AWS7
Follow the above steps to create the zip file
Click on Actions▽ , then on Download file.
Specify the path of the zip file and it will be downloaded
This zip file can now be used as shown in the previous steps.
cloud platforms – aws practical demos 5
Create a Linux-specific package by using a specific pip command This can be achieved, even if you don’t have a Linux laptop, as described in the AWS Knowledge Center.