

From here, we'll build out our generatePDF function. Then we will create a key property, which is simply going to dynamically generate a unique name for a PDF.

Then we will create an instance of S3 off of the SDK.
PDFKIT S3 PDF
We'll go ahead and add in our dependencies, PDF kit and AWS SDK. With that defined, let's go to the top and build this out. Generate a PDF, save a PDF, and then generate a URI. Then finally, generate a URI to return within the response body that we can use to view the PDF in the browser. This will return a PDF, which we will then save to S3 via save PDF. We'll take this text, send it into the generatePDF() method. After that, we'll define the first step in our workflow, which is going to be generating the PDF. We'll want to continue on by parsing the body, and if it doesn't exist, turn it into a pending string. Next, let's change the handler name and pull the body property off of the event parameter. We're going to define the workflow inside of the handler and then build out the underlying functions that we're calling. Bear with me, because what I like to do next is approach this from a little bit of a different angle because I know where we're going and I think this will create a better narrative arc. Let's clean up this entire handler and delete everything in there.
PDFKIT S3 CODE
Once these are installed, we can get back into our code and start to build out this handler.
PDFKIT S3 INSTALL
We're going to hop back into the command line and install two dependencies, PDF kit and AWS SDK. With all of that infrastructure out of the way, let's move on to the JavaScript portion of this lesson. We want to do this so that once we've uploaded a PDF and gotten that URI back, we're able to click on it and see the PDF. I've also created a custom bucket policy, which essentially says that for anything we've put into this bucket, we can get that object and anybody is allowed to do that.
PDFKIT S3 FULL
I've opened this up so that it has full public access. What I've done in this case, and you won't want to do this for something in production or that has any security interest. Now that we've reviewed, let's jump into the console so we can take a closer look at the S3 bucket we're working with. We've generated a pretty standard handler with an API gateway that has some custom environment variables. We've defined our custom IAM role statements. Let's take a moment here to do a quick review of what we've done up to this point. Using the custom property that we set up, we're able to locate that via NARN. We also want to define the resource that we are providing these permissions for. To do this within IAM role statements, we're going to create and allow effect, and define it for two specific actions, S3 put object and S3 put object ACL, which is saying that we now have the ability to use the underlying ACL object to determine if we have access. Inside of our provider declaration here, we need to define some IAM role permissions, When creating interoperability between multiple AWS services, we need to make sure that everything has the right permissions to communicate with each other. This is looking pretty good so far, but we'll need to do one additional thing. Finally, we'll give this the value of NCAA football stats. We'll add in a custom property, and then underneath that, we're going to add in bucket. From here, let's jump up to the top and add in our custom variable.

We can find this out using AWS region, which pulls that from our local configuration when we set up serverless. You'll see that we're referencing custom bucket, which we'll define in just a moment. The first property that we're going to add is the bucket that we want to upload our PDF into. To do that, we simply add an environment property into our YAML. We're going to define two environment variables that we want to use inside of our function. We're going to introduce something we haven't talked about before. Set it up as a basic HTTP API gateway with a route of API PDF and then use a method of post. In the serverless YAML file, we'll change the function name from Hello to Generate. Let's close the left-hand side and get started. We're going to start with the serverless YAML file first, and work through that. We'll generate the project and then step right into the generated directory and open this up in VS code. To get started, let's create a project using our typical node.js template, and call it "Egghead PDF Generator."
PDFKIT S3 HOW TO
With the most important one being, how to integrate with additional AWS services from a Lambda function. In doing so, we will learn some new techniques along the way. We'll create a Serverless Function that generates a PDF and then saves it to S3.

Instructor: In this lesson, we are going to do something a little more advanced.
