# AWS Lambda + OpenAI API = Insane Serverless Power (Full Guide)

## Метаданные

- **Канал:** Cloud Guru
- **YouTube:** https://www.youtube.com/watch?v=P6gs8VskA7Y
- **Дата:** 05.05.2026
- **Длительность:** 16:18
- **Просмотры:** 94
- **Источник:** https://ekstraktznaniy.ru/video/50027

## Описание

🔥 Watch me deploy a LIVE GPT-powered API running on AWS Lambda — no servers, no infrastructure, just pure serverless execution. ⚡ By the end, you'll have your own production-ready OpenAI API endpoint responding in real-time.

🔥 DevOps Career Boost
🚀 Linux Foundation Certifications – 30% OFF
💥 Get 30% OFF Linux Foundation Courses & Certifications
COUPON CODE: CLOUDGURU

☸️ Top Kubernetes Certifications (HIGH DEMAND)

👉 Enroll here for CKAD: https://training.linuxfoundation.org/certification/certified-kubernetes-application-developer-ckad/?source=aw&sv1=affiliate&sv_campaign_id=2797056&awc=85919_1773067559_bfa5e6ace1397c69f68bfb64ff35fa1f

👉 Enroll here for CKA: https://training.linuxfoundation.org/certification/certified-kubernetes-administrator-cka/?source=aw&sv1=affiliate&sv_campaign_id=2797056&awc=85919_1773067536_a1f91345cf951dbbbecaad9281bf8434

👉 Enroll here for CKS: https://training.linuxfoundation.org/certification/certified-kubernetes-security-specialist/?source=aw&sv1=affiliat

## Транскрипт

### Introduction to the project and OpenAI GPT models. []

Unless you've been living under a bridge somewhere, you've probably heard of Chat GPT. It has become one of the most popular services on the internet by far. Now, if you've ever wondered how chat GPT works or you want to build your own chat interface like this, I'm here to show you how you can do that using the same technology that chat GPT uses. We're going to do this by completing a project on Skillmix. Now, if you go to the projects page on Skillmix, you'll be able to see a list of all the projects available. At the top here, there are three AI projects that you can complete. I'm going to go ahead and show you how to build this one. This is the simplest one that's available on our site. So, this project will is pretty straightforward. We're going to deploy a lambda function that queries Open AI for uh its natural language processing request and we're going to expose this Lambda to the internet using the function URL feature of Lambda. Now to

### Starting the interactive lab on Skillmix. [1:11]

get started, all you have to do is create an account at Skillmix and then you can open this page and open the interactive mode. When you're here, the first thing you want to do is click the start lab button. This is going to go provision an account and provision a development server. It takes one or two minutes, so I'm going to pause this and come back when everything's ready. Okay, great. Our lab environment is now ready. We can tell because all the credentials are here and our terminal is connected to the remote instance. I'm going to go ahead and first start out by adjusting these um windows a little bit to give my terminal a little bit more space. And you can do um as you please there as well. If you click on the hat icon, it'll pull up the build steps. And we first start off by kind of giving an intro into what Open AI is and what the GPT models are. You can read through that. Um then we start getting into the lab. And the first thing you'll want to do is

### Configuring the AWS CLI with credentials. [2:17]

configure the AWS CLI. So you type in AWS configure and it will prompt you for a couple things. These credentials are back on the lab page. So go grab the access key and then the secret key. Make sure you get all of it for region. Most labs are in US West 2 and you can leave the default output format. Okay, great. Let's get go back over to our instructions and we just completed this step. So we have our CLI configured.

### How to obtain and manage your OpenAI API key. [2:53]

configured. The next thing you'll want to do if you don't have it already is get your open AI API key. So you go over there to create an account and then you can go to this page to get your actual key. Um you know word of caution these keys are do need to be kept private and you can cycle them out. So even if you do this lab through Skillmix um you can use one key for that and then you can delete it afterwards just to ensure there's no leakage. Um, so go do that and come back if you haven't yet. Um, then we want to create

### Creating the main.py Python file. [3:26]

the Python file. Uh, there's a number of different ways to do it. You can do it through the CLI, but I'm just going to do it through the UI here. We're going to call it main. py and click create. And it will show up on the left hand side here. And you open the file. And then we're going to write the actual Python function. I'm going to copy and paste all this in and then we'll step through it.

### Writing the Lambda handler and configuring the OpenAI call. [3:51]

Okay. So the first thing we'll do is import the JSON and open AI libraries. Uh we do need to configure the Open AI API key. So this is where I will paste mine in and we'll end up deleting that from OpenAI afterwards. So don't try to use it. The next step is to create the Lambda handler. So this handler um is how AWS Lambda works. like we need to have a named function and we have to pass the event and context objects in. Uh the event has things like the um payload that we're going to pass in uh to have it evaluated by the function uh along with some other information and the context is information about the request itself. Okay. So I like to print the event because the it gives us a chance when we're looking through the cloudatch logs. If there is an issue, we can see what is in the actual event. Then I'm going to go ahead and set a couple local variables. The first one will be body and we're going to do a JSON. loads. So we're going to load the event body value. And in that body value, we're going to grab the prompt um section. So the prompt value and we'll use this prompt later on in our code. The next thing we do is we set a messages list and there's two items in the list. The first one is a dictionary that has um this role and it sets the role as system content as how we want open AI GPT to act. So right now we're saying you're a legendary fiction writer act like you normally do. If you're writing technical posts or you're doing something else, you'd want to set the system content to uh to be appropriate for what you're doing. And then we're going to set this next one. This next dictionary is going to be a um a role as user. So this is the user prompt coming in. And we'll just give it the prompt value that we set up here. So this function will accept the prompt and then it'll use that prompt to process a request to OpenAI. uh we do set some response text um an empty local variable right now and then we make the actual open AI call and so we're doing chat completion. We want to specify the GPT model 4. If you don't have access to GPT model 4, you may need to set it to 3. 5. You can find the model names in the um OpenAI documentation. Actually, I'll just show you right now. If you go to platform. opAI, Open AAI, go to the documentation, and there's a list of model models. So, if you don't have access to four, you would pull this one and set that right here. Okay, great. Next, we're going to set the messages to the object we created above, and we're going to set the temperature to zero for now. So once this call goes out to OpenAI, it's going to return a response and we're going to read the content of that response and then return it here. We're going to give it a status code of 200. So a successful response and we're going to give our response text. So once you've typed in that code, click save and then we can proceed. I do review all that code here. So if you're doing this by yourself, you can read through it again.

### Installing dependencies (zip, less, dialogue) and the OpenAI library. [7:22]

again. Now, let's deploy to Lambda. So, the first thing we're going to do is update our space here. Um, pull down the latest list of packages and then we're going to install a few things. Oops. So we'll install dialogue, we'll install zip and we will install less. Okay. Then we are going to you can ignore this error if you get it. Um you should be able to still proceed. Then we're going to install we're going to use pip which is a Python package manager and we're going to install the open AI library into the package directory. And if we CD into that package directory, we can see that there are a bunch of different um there's OpenAI package and then there's all the different dependencies that OpenAI requires.

### Creating the deployment package (ZIP file). [8:28]

requires. So we're going to create a zip file of that all those contents. And we need to do this because we need to upload that zip file along with our lambda code and um because it is a dependency. So you see here um we now have our zip file in the parent directory here or this is the main working directory. Okay. So we've done that. Um we went and we moved to the workspace folder and now we're going to add the main. p1 py file into the zip package. So now our zip package has our Python file as well as the open AI libraries. Okay. Next up, we're going to create a

### Setting up IAM policies and roles for the Lambda function. [9:16]

policy. json file. And this is a file that's going to be this policy will be used um or attached to our Lambda function. um I'm sorry our lambda role and it will let the lambda service interact with our function and and by proxy of our role here. Okay, so we created the policy file. Now we want to create that role itself. So we copy this and as we kind of review this, we're using the create roll um CLI. We're giving it a name and then we're pointing to the policy file that we just created and it will return back and give us details about the role that it just created and we can hit controll Z if we need to escape out of that. Okay, so we have our policy and we have our role. Now we need to attach the policy to the RO and that's what we do with this. Okay.

### Deploying the function to AWS Lambda using the CLI. [10:25]

Okay. And now we're going to create the actual function. Um, and I'll step through here what's going on. So, we're using the create function option. We're naming the function my open AAI function. We're uploading that package file that we created previously. And that's has our function code as well as all the additional packages. We're specifying the handler. So this is the Python file name main and this is the function in that file lambda handler. We're specing a runtime of Python 3. 10. I believe that's a good enough um version for OpenAI's library itself. We're setting a timeout for the Lambda function to 180 seconds. We kind of need some more time here because this function will call out to open AI's API and sometime that can take a little bit of time to respond. And then we're going to um point it to the role that we just created. And we do need to set our account ID here. And I'll show you how to do that. So let's just grab this text. Make sure we have it all. I'm going to minimize that and we'll paste that in. And then like I said, we need to grab our account ID. So go back to the lab tab and you can get your account ID here and we can execute that and this will confirm that we have created our function. There's the name and there's the AR. All righty. We're making good progress here. Next, we will

### Configuring the Lambda Function URL and setting permissions. [12:12]

I'm pretty sure it's configure the URL. Yeah. So, next we're going to go ahead and create the function URL config. And here we're going to point to the function name that we just created. And we're going to set the O type to none. You can AM, but then you need an AM user that actually calls the function. And we're just going to set it to none for now. And we're going to allow all origins here. So on the course side, we're just going to uh open it up to the internet basically. So let's go ahead and copy that in. And right now we do have u a URL that it provided. So this is the URL of our function. Oops. And you can see right now it's actually not allowing us to access it. It's saying forbidden and that's because we need to do one more thing. We need to give the lambda um some permissions and we're going to give it permission to be accessed by the open internet. Um the action has to be lambda invoke function URL. The principle is star. So the internet and then we're going to set it as an off type to none. Okay, so that worked. And if you go over and you're you open up this URL or your URL, it will be different in the browser and you click refresh, you should now get this error saying that there's a JSON parse error. Um, this means that the permission setting worked. And

### Testing the final API with a sample prompt. [13:54]

the last step is to um test our function. So, let's enter in the first part of this command and then we need to grab our URL and paste it in. So, you paste yours in and then hit backslash and hit enter. This will go to a new line and then add your content here. So, right now what we're doing is we're setting this prompt value and we're saying what movies are great to watch with family. And if you remember, we set in our um in our kind of uh ro our system setting here, we said you are legendary fiction writer. So we're going to have this question answered by a legendary fiction writer. And so let's go ahead and press enter and wait. Sometimes the API can take a minute. It's good time to get a coffee, maybe. Okay, here we go. So, here's our response. And it says, "As a legendary fiction writer, I would suggest a mix of classic and contemporary films, etc. " And so this is it. You just created your first Lambda function API that takes in user prompts, queries OpenAI, and returns a response.

### Conclusion and next steps. [15:37]

And that is all for this lab. You can go back to the lab tab and click stop lab. Um, if you don't, we've got an automated process that does that anyways, so no worries there. And I hope you enjoyed the video. Uh, please like and subscribe and share this if possible. Would really appreciate it. Thank you.
