In this post, you learn how to use AWS CDK to create a cron job in AWS. A cron job is essentially a task which resources periodically, e.g. every minute, day, or even year. In this demo, I’ll show you how you can send emails as a cron job. But you can easily extend this by adding some lambda …. The components used for this application are, AWS Events which is the component where we specify how often the logic should run, AWS SNS where to send emails.
First thing’s first, let’s initialize an empty CDK typescript project: cdk init –language=typescript.
Then we go to package.json to declare the dependencies for our project. Add the following modules to the dependencies:
"@aws-cdk/core": *",
"@aws-cdk/aws-events": *",
"@aws-cdk/aws-events-targets": *",
"@aws-cdk/aws-sns-subscriptions": *",
"@aws-cdk/aws-sns": "*",
This is an optional step, but go ahead and run “npm install” in the root directory of your project. This will download the dependencies and give you intellisense which comes very handy. Then go to the lib folder and the typescript file for your application stack.
Add the following import statements to before class declaration:
import * as events from ‘@aws-cdk/aws-events’; import * as targets from ‘@aws-cdk/aws-events-targets’; import * as cdk from ‘@aws-cdk/core’; import * as sns from ‘@aws-cdk/aws-sns’; import * as subscriptions from ‘@aws-cdk/aws-sns-subscriptions’;
Have a look out the following code regarding the cron expression
const rule = new events.Rule(this, 'Rule', {
schedule: events.Schedule.expression('rate(1 minute)')
});
Of course, here I didn’t specify a cron expression, but only a rate. If you have a simple period to run a certain task, then you might be better of using the rate expression, as it is simpler. You can use minute, hours, days. If your logic is more complex, say every Monday to Friday at 10 am, then you will be better served with cron expressions:
const rule = new events.Rule(this, 'Rule', {
schedule: events.Schedule.expression('cron(0 10 ? * MON-FRI *)')
});
Then we add our sns topic and add Email subscription:
const topic = new sns.Topic(this, 'Topic', {
displayName: 'your topic'
});
topic.addSubscription(new subscriptions.EmailSubscription('emailaddress'));
Note that you could also use URL, SMS, Lambda, SQS subscriptions. Finally we hook up our rule with our sns target:
rule.addTarget(new targets.SnsTopic(topic));
Now we are ready to verify our cdk stack. First run ‘npm run build’ which compiles typescript to javascript. Then run cdk synth. This should give a cloud formation template. If it returns an error, go to previous steps and verify your work. Before you can deploy this application, you need to bootstrap your environment. To do that, run the following.
cdk bootstrap aws://youraccountID/yourregion
set CDK_NEW_BOOTSTRAP=1
cdk bootstrap aws://youraccountID/yourregion
Now you are ready to deploy your cdk app.
cdk deploy
Some more useful cdk commands:
cdk ls //lists the stacks in your application
cdk diff //shows the difference of your current deployed stack and your local stack
cdk doctor //*kind of* verifies your cdk stack for warnings
You could use such cron jobs for sending newsletters, verifying state of your application…
The source code on github: https://github.com/pedramha/Cron-Job-on-AWS-with-CDK