Running a Lambda on a schedule using AWS CDK



In this post, I will show the how to deploy a simple lambda that run on a schedule using AWS CDK.

The language type for this is typescript and for lambda the run time environment is python 2.7.

This example is based from the AWS examples of Typescript https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/lambda-cron/.

This is a stable version of example and I have updated my cdk version to 1.1.0 (build 1a11e96) at the point of posting this blog.


I am outlying the steps that I have done for this example.

Step 1.  Create directory structure of typescript project and scaffold the project.


Step 2.  The directory structure and building the project.



Step 3.   Installing the required packages (aws-events-targets,aws-events and aws-lambda) .




Step 4.  The code for the Lambda scheduler inisde the lib/lambdacron-stack.ts file



Step 5.
Compiling the code and I have got error that I do not have lambda-handler.py file. 


Step 6 . Created lambda-handler.py file and then synthesis the stack which generated the stack that it will create as below. Around 20 line of code has created 74 lines of cloud-formation compatible stack.





I have also run cdk doctor which gives information about cdk parameter it is using and version of cdk.

Step 7.   Once satisfied deploy using the cdk deploy and verifying the lambda created in the Lambda console.





Step 8. Verifying the stack creation in the cloud-formation console and the schedule that was created in the cloud-watch console. 





Step 9. Destroying the AWS stack via CDK destroy command and verifying that stack was deleted in the AWS cloud-formation console.


















Comments

Post a Comment

Popular Posts