Quick start aws-chalice
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
- Create the functions in app.py
- Config lambda fuction attributes
- Deploy chalice to create cloudwatch event which trigger lambda cron
- Check result
- Clean up
๐ Create new chalice project
โก $ chalice new-project lambda-cron
โก $ cd lambda-cron/
๐ Create the functions in 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!")
๐ 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
}
}
}
๐ 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
- 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 inconfig.json
eg."iam_role_arn": "arn:aws:iam::111111111111:role/lambda-cron-dev"
๐ Check result
- Lambda function with Event Bridge trigger
- Cloudwatch event rule
- Cloudwatch log group
๐ 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
Top comments (0)