Instructor: 0:00 Since we have an empty stack right now, it is a perfect time to start building our serverless backend. In order to do that, we are going to create our very first lambda function with CDK. First, open up the terminal and around npm install -- save awscdk/awslambda. We are also going to add types for the lambda function.
0:19 I'm going to also install @types/awslambda. We have to install the AWS Lambda package separately because the AWS CDK core package does not contain constructs for every single AWS service. Otherwise, it would be incredibly huge.
0:32 If you would like to see the list of all packages that you can install with AWS CDK, you can always take a look at official documentation. The link to this document is also in the description of this video. Over here, you can scroll down to see all the different packages that you can install in CDK apps.
0:47 For instance, if I scroll down over here, we are going to able to see AWS Lambda package. That is something that we just installed in our project and right now we are going to use it in order to create our very first lambda function with CDK.
0:57 First up, I'm going to create a new folder for our lambda function. I'm going to call it lambda. Also inside of this folder, I'm going to create a new file, which I'm going to call, hello.ts. This file is going to contain a body of us for lambda function.
1:08 Let me just do, exports.handler = async function, is going to take an event which is of type AWS lambda API Gateway event. We are going to call this function via the API Gateway. Let me paste in the body of this function.
1:23 This function is going to console the event that was provided to it. I'm going to use also the JSON.stringify to make sure that it is nicely formatted. Every single time this lambda function is going to be called, we are going to return a status code of 200.
1:34 Everything is fine. The counter type is going to be set to text plain and we're going to return a body of, "Hello, Egghead friends." Our lambda function is prepared. Let's go ahead and deploy it.
1:43 Go back to our start. First up, import star as lambda from AWS CDK, AWS lambda. Next up we're going to create an instance of AWS lambda function construct, const hello lambda = new lambda.function. As we can see over here, each construct takes three arguments.
2:03 The first step is a scope. A scope is a context in which the construct is created. In vast majority of cases it's going to be this, because we are creating this construct within the scope of the CDK stack.
2:14 Next step is the ID. An ID is an identifier for a construct which is unique within its scope. I'm going to set it as, hello lambda. The third argument are props. Props are a set of properties for each construct.
2:27 Since we are using TypeScript, our editor will help us understand what kind of props can we set for each construct. Also which one of those are mandatory because they are going to appear on the top of this list.
2:37 As we can see now our editor, in order to create a lambda function with CDK, we have to provide coat, handler, and runtime.
2:43 Let me start with coat. This property specifies where the lambda function is located. In our case is inside of this lambda director over here. I'm going to do lambda on that coat from asset lambda.
2:54 Next up, we have to provide the handler. A handler is a function that is going to be executed whenever this lamba function is triggered. If we take a quick look at our lambda function over here, we can see the defined name is held at TS, and the function itself is called handler. We have to set the handler property to hello.handler.
3:11 Next up, the runtime. We're going to run lambda runtime and here we can see the list of all possible runtimes for the lambda function. We are going to choose, "No addressed with X," because we want to be cutting it.
3:22 Now, everything is prepared so let me open up my terminal, and they're on the CDK DEV in order to understand what we are about to deploy.
3:29 We can see in the resources section over here that we are about to deploy a brand new lambda function, which is going to be called Hello Lambda and also an IRML, which was created for this function by CDK for us, and we didn't have to configure it.
3:41 Without being said, let me clear the terminal, and they're on CDK deploy in order to deploy to some of the functions to AWS. As usual, CDK is going to ask us whether we are OK with deploying those changes. I'm more than OK to deploy that so I'm going to say "Yes, enter," and now our stock is going to deploy.
3:56 Once this is done, we can see that our Hello Lambda has been successfully created in AWS.