- [Narrator] In this video,
you'll see how you can build a Python event-driven
serverless application with Amazon CodeWhisperer.
With this machine learning powered service,
you can build applications faster
by leveraging code recommendations
and implementing API calls across popular AWS services.
This is the application that we're going to build
with the help of Amazon CodeWhisperer.
Users will have two different URLs
they can interact with to download
and recognize objects and images.
To achieve this implementation,
we'll work with four different Lambda functions.
We'll begin by implementing a Lambda function
that downloads an image using a provided URL,
and then uploads the image to Amazon S3.
For demonstration purposes,
we've already prepared an application
with the AWS Cloud Development Kit, or AWS CDK.
We're using Visual Studio Code as our IDE,
but we could also use an IDE by JetBrains.
To get started, let's navigate to our first Lambda function.
CodeWhisperer supports several programming languages,
including Java, JavaScript and Python.
In this example, we'll use Python to build our application.
Let's type a comment that says
write a function to download a file from HTTP get
and press enter.
CodeWhisperer guesses our intent
and suggests a function name.
We'll accept a suggestion by using our tab key.
Next, let's press enter to see CodeWhisperer's suggestions
for the body of the function.
We'll use our right arrow key to continue through
the suggestions until we find the one we wanna use.
We'll press tab to select this one.
Next, let's call the function
and pass it the image to recognize
and image file name variables.
Now we need to upload the image to Amazon S3.
To do this, we'll enter a comment.
We'll accept CodeWhisperer's suggestion
to name the function upload file.
We'll also accept the suggested method.
Now, let's call this function
and pass it the image file name at our S3 bucket.
We finished our first Lambda function.
All we had to do was type in our text comments,
and CodeWhisperer generated the code for us.
Now we'll implement our second Lambda function.
This function will read an item
from the Amazon Simple Queue Service, or SQS queue.
Use Amazon recognition to recognize
all the labels from the image,
store the results in Amazon DynamoDB,
and then publish the results
to Amazon Simple Notification Service, or SNS.
Let's start by entering receive message from SQS.
We'll use the method suggested by CodeWhisperer.
Next, we'll define the image event as receive message
and set MyQueueURL as the parameter.
Now let's enter detect labels from image with recognition.
We'll accept the suggested name,
which requires a bucket and key.
We'll also accept the suggested implementation
which will return all the labels that are in the image.
Next, let's define labels as detect labels
and pass the image bucket and key.
Now, let's enter save item to DynamoDB.
We'll accept the suggested name
and choose our preferred implementation.
In this case, CodeWhisperer took into consideration
our prior code that recognizes images from recognition
when creating a format and structure
for the items that will be saved.
Let's rename the method
and provide the image ID as a parameter.
We'll also rename a couple of the parameters.
Before calling this method,
we need to import the UUID module.
Let's call the method and pass it a random UUID
and the detected label.
Now we need to send the image to SNS.
When we press enter,
CodeWhisperer suggests adding code to send a message.
The suggestion looks good,
but instead of sending the image event,
we'll send the labels.
Now let's define the method.
We'll enter publish item to SNS.
CodeWhisperer makes assumptions about the message,
the subject and the message attributes.
The assumptions look good,
so we'll accept this suggestion.
Finally, let's add code to delete the message from SQS
so we don't process it twice.
We'll call delete message
and pass the appropriate parameters.
The second Lambda function is now complete.
Now we'll implement a third Lambda function.
This function will read the object
from the Amazon SQS queue that was published
by the previous Lambda function.
It will then convert the message from JSON to XML,
and send the message to an external email server.
Let's start by entering receive message from SQS.
We'll accept CodeWhisperer's suggestions.
Next, let's create a variable called SQS QueueURL
and assign a value to it.
Let's also create a variable called processed image
and set it to our receive message method,
passing the SQS QueueURL variable as a parameter.
When we press enter,
CodeWhisperer guesses what we wanna do next.
In this case, it's exactly right.
We want to convert JSON to XML.
Let's set up the method.
We'll continue through CodeWhisperer's suggestions
until we find a method we like.
Now we need to send the XML message
to our email server using HTTP post.
Again, CodeWhisperer guesses what we want to do.
Instead of accepting CodeWhisperer's suggestion,
we'll enter send XML object with HTTP post
to define the method first.
Now, let's call the method
and pass it the appropriate parameters.
Next, we'll define a method to delete the message from SQS.
Finally, let's call the method.
Now we'll implement our fourth Lambda function.
This function reads all the items
from the DynamoDB table and returns them as JSON objects.
Let's open the Lambda function.
Currently, all this function does
is scan items from DynamoDB.
We'll enter list all items from a DynamoDB table
to get suggestions from CodeWhisperer.
Let's choose the name scan table.
This method includes all the items we want.
We can return the items by simply calling the method
and passing the table as a parameter.
Based on our comments written in plain natural language,
CodeWhisperer generated code
that matches our patterns and style,
without requiring us to spend time researching methods.
Now, let's test this application
with this image of a pug to see what it recognizes.
We'll invoke the endpoint that we just deployed,
and then pass the URL for the image.
The image uploaded successfully.
Now we'll invoke the second API gateway endpoint
to see the results.
The application labeled our pug image tiger,
wildlife, animal, mammal, canine, pet, dog and so on.
Proving that our application with the code generated
by CodeWhisperer is up and running.
You've just seen how to build a Python
event-driven serverless app with Amazon CodeWhisperer.
You can learn more about this topic
and sign up for the CodeWhisperer preview
in the description and links for this video.
Thanks for watching, now it's your turn to try.