Every resource we deploy to AWS with CDK has some default values configured for us so that we don't have to configure everything ourselves. This comes in handy because it allows us to focus on building our infrastructure and solving our problems instead of configuring everything. With that being said - CDK allows us to configure various properties of our resources! In this quick lesson we're going to learn how to configure the memory size and timeout of an AWS Lambda function.
Lecturer: [0:00] We have an AWS Lambda function deployed in our stack and we can see that function over here.
[0:05] Let's go to configuration to see some of the defaults that were applied to our Lambda function. By default, every single Lambda function created by CDK gets 128 megabytes of memory and a timeout of three seconds. We can click on this info button over here to learn more about the different function settings.
[0:25] First of all, we might want to increase this memory if we are actually running something that is not hella worth in production. In essence, to increase the memory and CPU power allocated to a function, we can use the memory setting. Why? Because Lambda allocates CPU power in proportion to the amount of memory configured.
[0:42] We might also want to modify the timeout because three seconds might not be enough for certain use cases. Let's go back to our stack. Apart from entry and handler which are mandatory props, there's also all sorts of optional props that we can provide to our Lambda function. For instance, we can change the memory size. By default, it is set to 128 megabytes.
[1:03] I'm going to go ahead and change it to 256. We can also modify the timeout. By default, the timeout is set to three seconds. I'm going to make it a bit longer. Duration, seconds, and maybe 10 seconds. We can see that this duration is currently not defined, that is because we have to import it from aws-cdk-lib.
[1:22] Let me just add that duration. There you go. Now, our stack is ready to deploy. I'm going to open up the terminal. Let me just scroll this up a little bit. I'm going to run CDK diff to see what kind of changes are we about to deploy. We can see that we are not about to deploy a new function, instead, we are modifying the parameters of an existing function.
[1:42] We are going to modify the memory size to 256 megabytes and the timeout is going to be set to 10 seconds. Let me go ahead and deploy that. I'm going to run CDK deploy. After a successful deployment, we can go back to our Lambda function in AWS console. We can see that the timeout was increased as well as the memory size of this Lambda function.
[2:03] We were able to accomplish all of that by only adding two more lines of TypeScript code in our CDK application.