1. 8
    Change the properties of a lambda function deployed with AWS CDK
    1m 27s
⚠️ This lesson is retired and might contain outdated information.

Change the properties of a lambda function deployed with AWS CDK

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 6 months ago

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.

Instructor: [0:00] We have a Lambda function deployed to CDK and we can see over here that it has some basic settings such as the memory size, which was set to 128 MB, and also timeout which was set to three seconds by default. Of course, we can modify those default values. The cool thing about CDK is that we don't have to open up the documentation in order to do that.

[0:17] If I go over here and expand the list of all available properties for Lambda function and if I search for memory, we're going to see that in order to change the memory size available for Lambda function, we have to use the memory set property. It is a number and the default value is 128 MB. Let me do that and change it to 256.

[0:34] Next stop, the timeout. If I press T, it's going to show me the timeout. Here we can see a snippet of the documentation. This is the function execution time in seconds after which the Lambda is going to terminate the function.

[0:46] We can set it up to 15 minutes, but it is not such a good idea because what happens is that if your function is going to get stuck because of something, we'll end up paying for the 15 minutes of the Lambda function execution, and this can get rather expensive. I'm going to set the timeout to CDK duration of seconds and I'm going to set it to 10 seconds.

[1:06] We can open up the terminal and then run cdk diff to see what we are about to deploy. We can see that our function is going to stay exactly the same. The only thing that is going to change is the memory size and also the timeout. Let me clear the terminal and run cdk deploy.

[1:19] Once this is done, we can go back to AWS Console to see that both the memory size and also the timeout was updated according to the settings that we have configured over here.

Quang Le
Quang Le
~ 3 years ago

Not sure why there is a compilation error on timeout: cdk.Duration.seconds(10) as

  Types have separate declarations of a private property 'amount'.

26       timeout: cdk.Duration.seconds(10)
         ~~~~~~~

  node_modules/@aws-cdk/aws-lambda/lib/function.d.ts:67:14
    67     readonly timeout?: Duration;
                    ~~~~~~~
    The expected type comes from property 'timeout' which is declared here on type 'FunctionProps'

Does there anyone have any idea? Thanks

Tomasz Łakomy
Tomasz Łakomyinstructor
~ 3 years ago

Hey! Is your code open source? I'd like to take a look and help :)

Akash Manohar
Akash Manohar
~ 3 years ago

@quang I got the same error. Open package.json and look for the difference in versions for aws-cdk, aws-cdk/core and aws-cdk/aws-lambda. Change everything to the same verison.

For example: I had 1.222.0 for aws-cdk/aws-lambda but aws-cdk and aws-cdk/core were at 1.221.0. I changed everything by hand to 1.222.0 and that resolved the error.

Also for compatibility I started using the "cdk" binary within the node_modules dir of the project. So all my cdk commands are like this example:

$(npm bin)/cdk diff
Markdown supported.
Become a member to join the discussionEnroll Today