Creating a python lambda function

We are going to use the AWS Management Console to create Python Lambda functions using function blueprints. We will deploy Python functions from the blueprints and then test them.

Sign in to the AWS Management Console and navigate through the AWS Lambda dashboard:

As shown in the preceding screenshot, you can see a list of all the Lambda functions that have already been created throughout this book, most of them in Node.js and also some in Java 8. Let's create our first Lambda function using Python. Click on Create a Lambda function.

We can select the Lambda function from one of the available blueprints provided. Let's start with a simple Blank Function blueprint. Click on the Blank Function blueprint. Don't select the trigger and just click Next. We need to give our function a name. For now, let's call it PyFun. Select the Runtime as well as shown below:

It's now created as Python code. We have a very simple Lambda function handler, which takes two arguments: an event that we invoke our Lambda function with and the context that gives us runtime information on our Lambda function. Let's add another statement that logs the event that we invoke our Lambda function with. Instead of returning Hello from Lambda, return the classic Hello World. Scroll down a little bit further, as we need to configure the IAM role that we want to assign to our Lambda function. For now, we use the basic execution role, as shown here:

Scroll down further and click on Next. Review the configurations and click on Create Function. The following screenshot shows the successfully created function:

Let's test it with one of the test events. Click on Configure test events. We can select whichever of the test events we like. For this chapter, we select the key 3 event. It doesn't really matter; it just prints it out on the console. Scroll down and click Save and Test. This will execute or invoke our Lambda function, returning Hello World. We can take a look at this excerpt from the log output, which states the event you have selected.

Let's now go back to the Lambda dashboard, and let's create a more interesting Lambda function from one of the other blueprints. You can select the blueprints by the runtime. How about choosing the S3 get object type Python blueprint?

As you can see, a trigger has already been pre-selected. We can also select the Bucket that we want to use. It's Amazon S3 that triggers our Lambda function. So, you can select whichever bucket you want to use. You just need to make sure that a trigger for the Lambda functions on these object-created events doesn't already exist. So, if you are not sure, just create a new bucket. You could also restrict it to listen for only certain events, or for objects that start with a certain prefix or that end with a certain suffix, but, for now, let's leave that empty. What we need to do is click the enable trigger checkbox here, which gives Amazon S3 permission to invoke our Lambda function. Click Next.

Now we need to give our Lambda function name. For now, let's name it PyFunS3. Let's take a look at the code that has been generated for us using this blueprint:

As you can see here, we import couple of libraries and one of them is the boto library, which you don't need to bundle with your source code because it's already installed on the instance that executes this Lambda function. So, you don't need to download and install this boto dependency. You can just use it by importing it. Here, we create an S3 client using the boto client library:

So, boto is a library used to call Amazon Web Services and, here, we want to call S3 from within our Lambda function. This means our Lambda function is not only triggered by an S3 event, we also want to call the S3 API from within our Lambda function. When our Lambda function is invoked by an S3-object-created event, we are going to do two things:

We are going to read the bucket in which the object has been created. There could be multiple buckets that trigger the same Lambda function. So, we retrieve the bucket name and retrieve the key name of the object that has been created. Then, we use the boto S3 client to perform a get_object request on S3; we retrieve some more information about our object, such as the content type; and then we print out the content type. What we could possibly do here is also retrieve the object content to process it; for example, to create a thumbnail of a bigger picture or to transform a Microsoft Word document into a PDF, or something like that. So, if there's no error, then the content type will be printed out:

Scroll down further to Lambda function handler and role. Here, we need to create a new IAM role because we don't only need permission, and S3 not only needs permission to invoke the Lambda function, but the Lambda function also needs permission to retrieve information or to perform the get object request here on S3. So, we need to give that role a name; here, a policy template has been selected already, which is an object read-only permission, because we want to read the content type. Let's call it PyFunS3Role:

Scroll down and click Next.

Review the configuration and click on Create function. Once the function has been created, as shown in the following, let's test it with a synthetic event:

Click on Actions | Configure Test Event. And, instead of using the Hello World event in the sample event template, let's use an S3 event:

So, this S3 Put event should do the trick because it's an object-created event. Click on Save and Test.

You can see in the preceding screenshot that we get an access-denied error. Why is that? Actually, we get an error because our GetObject operation does not have permission to use the S3 boto client to perform that GetObject request. Instead of tweaking the synthetic event, we can also go to our S3 bucket and just invoke the trigger.

Go to the S3 bucket and upload a file. Once it has uploaded, take a look. Go back to the Lambda dashboard, and click on Monitoring and View logs in CloudWatch:

Here, we can see a log stream, and we can see the GetObject operation has failed because access is denied. But we can also see that manually invoking the S3-object-created event has actually worked, and you can see the content type is an image in the following format:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.233.62