It's time to start building our serverless backend!
In this quick lesson we're going to use the lambda.Function
construct imported from @aws-cdk/aws-lambda
in order to build and deploy a 'Hello World!' AWS Lambda function.
You can find out more about what kind of constructs are available in AWS CDK construct library here
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.
I had to bootstrap before running cdk deploy
. Once that completed I ran cdk deploy
and no errors. The exact error message I was getting was: TodoappStack failed: Error: This stack uses assets, so the toolkit stack must be deployed to the environment (Run "cdk bootstrap aws://unknown-account/unknown-region")
Thanks for this. It was really helpful to get started. One issue I had was using 3rd party node packages (I'm using FaunaDB) in my code. Somehow I missed the description of how to do this in a later lesson. After using my overcomplicated workaround, I found a much better way in lesson 22.
The link to the code seems to be broken - I get a 404
Link to code https://github.com/tlakomy/egghead-build-an-app-with-aws-cloud-development-kit/tree/create-a-lamba-function-with-cdk
Link to code https://github.com/tlakomy/egghead-build-an-app-with-aws-cloud-development-kit/tree/create-a-lamba-function-with-cdk
Thanks for this @Milan! I got the code link updated! 🙌
Hello Everyone that is facing problems with deploying their first Lambda Function! If you see this Error message: "failed: Error: This stack uses assets, so the toolkit stack must be deployed to the environment (Run "cdk bootstrap aws://unknown-account/unknown-region")" Just run "cdk bootstrap" and AWS will provide the necessary resources for your environment, details of the process can be found with this AWS docs link => https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html
I'm running this error: lib/todo-app-stack.ts:11:45 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'. Type 'TodoAppStack' is missing the following properties from type 'Construct': onValidate, onPrepare, onSynthesize
. I've already try to downgrade and upgrade the versions, but is not working. Does anybody knows how to help?
RE the TypeScript problem above: The lesson here uses v1 of the CDK. v2 is a little different. You just need to npm i aws-sdk-lib -D
. To pull in a particular construct, you can do this: import * as lambda from 'aws-cdk-lib/aws-lambda';
If you have the updated types, and the latest version (v2) of aws-cdk
and aws-cdk-lib
, you'll be good to go!
Hey Tomasz, I think you forgot to mention, that before running
cdk deploy
you also have to runnpm run build
to compile the code inlambda
folder. Otherwise, CDK will just upload just the TypeScript file and it's not going to work in AWS Lambda.