2.3 Interaction With Other Services
Since functions are short-lived, you will often need to interact with external services to perform time-consuming tasks. In this lesson, you'll learn how to interface your serverless function with external services.
2.3 Interaction With Other Services
Hi, and welcome back to Introduction to Serverless. In this lesson, we are going to integrate our functions with other services to enhance its usefulness. Let's have a little fun with it. I am going to create a lambda function that uses Amazon Polly to do some text-to-speech conversion and upload the resulting mp3 file to an Amazon S3 bucket. Amazon Polly isn't available in many regions. At the time of this recording, it is just Northern Virginia, Ohio, Oregon, and Ireland. So if you want to follow along, make sure you choose one of those. Let's write a function first on my computer. I'm going to upload it as a zip file when I'm done, since I need the async node module as well. You can install by either using npm install or yarn add. So first let's require the aws-sdk and the async module. I'm exporting the handler function as I did in the hello world example, because it's a good convention. The function has the event, context, and callback parameters. To tidy up the execution of it and not have callback within callback within callback, I'm using the async.waterfall function. It takes three parts. Two of them are functions, generateAudio and uploadAudioFile. The third part is needed to pass the initial parameters to generateAudio. It's a utility function of async called constant. Here we pass in a text, a voiceId, and a filename parameter, all received from the event. I'm also going to pass in a function that either handles an error or the result from the last function in a sequence. I'm simply going to pass in both parameters to the callback function, which handles everything correctly. The generateAudio function will take the three parameters and the next callback that gets called with an error and a result that will be passed into the next function in line. Here I'm using the AWS SDK to create a new instance for Polly. To configure it, we also need some params. First of all, there is the text to transform into spoken word. Then the Type, which is simply text, and finally, some formatting options. I'm using mp3 as an OutputFormat, as well as 22600 as the SampleRate. The VoiceId is the name of the voice that will translate the text. There are multiple voices available for different languages. So you just choose the voice and don't have to supply a language parameter. We are going to create a function to decide which voice to use in the next lesson. Now that that's done, we can use Polly.synthesizeSpeech function to create our audio. In the callback, I'm simply passing the error object as the first param as well as the filename and AudioStream of your real data. The second function is responsible for uploading the audio stream to S3 as a file. It will receive the filename that it should store it as, as well as the audio stream. Within the function, I'm defining a new S3 object and set some parameters. The Bucket name, the Key, or filename, as well as the access rights and the Body. To upload the data, I'm using s3.upload. And in the callback function, I'm calling next again. Before we can create the function, I want to create a custom role for the function that has access to S3 and Polly, since it isn't possible to do this with the template generator. From the initial dashboard, go to Services, and under Security, Identity & Compliance, to IAM. You will see a list of resources that are available in your account. Go to Roles, either on the left-hand side or in the list of resources, and then press Create role to create a new one. Select AWS Lambda in the list of Service Roles, because we want to use it for a function. Now in the Policy selector, I'm first going to add the LambdaBasicExecution role. And then search for Polly and choose AmazonPollyFullAccess, as well as AmazonS3FullAccess when searching for S3. In the next step we can give it a name. I call it TutsplusLambdaPollyS3 and then create the role. Going back to Lambda, we can create a new function. This time we are going to author it from scratch. I'll leave the trigger disabled for now, we are going to add that in the next lesson. I'm calling the function synthesize-and-upload and add a useful description. This time we are going to upload an archive instead of just inline editing the file, since we have a dependency that needs including. In the File Manager, go to the folder and create a ZIP file of all the contents. Back in the browser, upload this archive. In our case, index.handler is the correct target, and for the role I'm choosing select an existing role and select our newly created role for Lambda. Since text to speech and uploading in the same function can take more than three seconds, I'm also going to increase the limit to 30. After confirming that everything is correct, I can now create the function. Of course, because there isn't a trigger yet, we can't test it from outside Lambda. We also need to create a S3 bucket. Actually, it might have been better to wait with the upload until we're sure that the S3 bucket is available, or dynamically set it as an environment variable. Let's create a bucket now. Go to Services > Storage > S3 and select Create bucket to create a new bucket. You don't need to change any of the defaults since we are setting the access rights directly on the object itself. Okay, now that that's done, it's time to go back and test the lambda function. I'm going to set up the test event to contain all the important data. The VoiceId is going to be Joanna, which is a female English voice, and the filename is going to be something like, test_1, without a file extension. I'm pretty sure if the Amazon Polly Services would watch this course, it would say something like, Wow, this Envato Tuts+ course on Serverless is awesome! Let's try it. If everything worked, then you will get a green bar. If not, the logs will tell you the exact reason. In the response there is also link to the file. Let's open and listen to it. >> Wow, this Envato Tuts+ course on Serverless is awesome. >> Thanks, Joanna, I think so too. To recap, the AWS SDK is available in every lambda execution without the need to install it yourself. Don't forget to give your lambda execution role the appropriate permissions to access the service. In a next lesson, I'm going to give you a peek at function orchestration and how to build complex scenarios that require sequential or parallel execution of functions. See you there.