DEV Community

Cover image for Create Cron Jobs On AWS Lambda With Cloudwatch Event
πŸš€ Vu Dao πŸš€
πŸš€ Vu Dao πŸš€

Posted on β€’ Edited on

3 3 1 1 1

Create Cron Jobs On AWS Lambda With Cloudwatch Event

Quick start aws-chalice

Create Cron Jobs on AWS Lambda with cloudwatch event

A very simple example of creating lambda function with cloudwatch event using aws-chalice. It provides an optional of how to create lambda function beyond aws-cdk (eg. python lamdbda cron)

What’s In This Document

πŸš€ Create new chalice project

⚑ $ chalice new-project lambda-cron
⚑ $ cd lambda-cron/
Enter fullscreen mode Exit fullscreen mode

πŸš€ Create the functions in app.py

app,py

from datetime import datetime
from chalice import Chalice


app = Chalice(app_name='lambda-cron')
app.debug = True


#@app.schedule('cron(0 18 ? * MON-FRI *)')
@app.schedule('cron(* * ? * * *)')
def cron_tab(event):
    print(f"{datetime.now()}: I'm running!")
Enter fullscreen mode Exit fullscreen mode

πŸš€ Config lambda fuction attributes

  • Here is just config lambda timeout
⚑ $ cat .chalice/config.json 
{ 
  "version": "2.0",
  "app_name": "lambda-cron",
  "stages": {
    "dev": {
      "lambda_timeout": 300
    }
  } 
}
Enter fullscreen mode Exit fullscreen mode

πŸš€ Deploy chalice to create cloudwatch event which trigger lambda cron

⚑ $ chalice deploy
Creating deployment package.
Creating IAM role: lambda-cron-dev
Creating lambda function: lambda-cron-dev-cron_tab
Resources deployed:
  - Lambda ARN: arn:aws:lambda:ap-northeast-2:111111111111:function:lambda-cron-dev-cron_tab
Enter fullscreen mode Exit fullscreen mode
  • Note: Chalice auto create IAM role lambda-cron-dev base on the need of the function, we can disable by adding "manage_iam_role": false but you must provide the IAM ARN in config.json eg. "iam_role_arn": "arn:aws:iam::111111111111:role/lambda-cron-dev"

πŸš€ Check result

  • Lambda function with Event Bridge trigger

Alt Text

  • Cloudwatch event rule

Alt Text

  • Cloudwatch log group

Alt Text

πŸš€ Clean up

⚑ $ chalice delete
Deleting function: arn:aws:lambda:ap-northeast-2:111111111111:function:lambda-cron-dev-cron_tab
Deleting IAM role: lambda-cron-dev
Enter fullscreen mode Exit fullscreen mode

Visit wwww.cloudopz.co to get more

🌠 Blog · Web · Linkedin · Group · Page · Twitter 🌠

Top comments (0)

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more