Reputation: 311
How to deploy StepFunciton with trigger by CloudWatch rule?
There is serverless-step-functions plugin for the serverless tool but it has not support CloudWatch events yet.
If it is possible to deploy pipeline like this via CloudFormation could you please provide some samples CF template and bash commands.
Upvotes: 2
Views: 1897
Reputation: 519
I was facing a similar issue but could not find anything in the serverless documentation that would make it work. I got it working by following the approach mentioned in the AWS Documentation - create a cloudTrail to catch events, add a cloudwatch event rule with the state machine as the target to trigger the state machine execution.
Could you mention what kind of trigger do you want to add? The serverless documentation says only http and scheduled events can be added to trigger the state machine. If you want to add any other event trigger, you must create a lambda function that executes the state machine and add the trigger on the lambda function.
I wanted to add a trigger on an s3 event. Here is what worked for me: I used the AWS CDK to provision all my infrastructure. Create an s3 bucket and a corresponding CloudTrail for the bucket to catch events in CDK. Following a code snippet for the same:
const mybucket= new s3.Bucket(this, 'myBucket', {
bucketName: "my-bucket",
});
// cloud trail to log PutObject events to the s3 bucket
const trail = new cloudtrail.Trail(this, 'myBucketEventTrail');
// Adds an event selector to the bucket foo
trail.addS3EventSelector(
[
{
bucket: myBucket,
objectPrefix: 'input',
},
],
{
includeManagementEvents: false,
readWriteType: ReadWriteType.WRITE_ONLY,
}
);
You can check AWS CDK here. Now, you can create a CloudWatch rule in your state machine definition in your serverless.yml to trigger the state machine on an s3 event. Here is the snippet:
stepFunctions:
stateMachines:
my-step-functions:
name: my-state-machine
events:
- cloudwatchEvent:
event:
source:
- "aws.s3"
detail-type:
- "AWS API Call via CloudTrail"
detail:
eventName:
- "PutObject"
requestParameters:
bucketName:
- "my-bucket"
definition:
Comment: "My test state machine"
StartAt:...
...
Now, deploy the two and test the trigger. The above approach worked for an s3 event. If you do not want to use CDK, you can use a CloudFormation template yourself and create a CloudTrail and a corresponding Cloudwatch event rule to trigger your state machine.
Here is a blog that describes how to create the CloudFormation template.
Hope that helps! Good luck!
Upvotes: 0
Reputation: 280
It sounds like you are asking about a Cloudformation template to build your state machine with Lambda.
AWS Step Functions has support for Cloudformation templates, Cloudwatch events triggers, and API Gateway (if you require it).
There is a good tutorial on the AWS documentation to follow to setup your state machine in Cloudformation. You can find that here: http://docs.aws.amazon.com/step-functions/latest/dg/tutorial-lambda-state-machine-cloudformation.html
For bash commands, you can use the AWS CLI to create, delete, and deploy your stack. Here is the reference to the docs: http://docs.aws.amazon.com/cli/latest/reference/cloudformation/index.html.
An example of how to use the CLI with Cloudformation can be found here: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-using-cli.html.
For starting and stopping executions, the AWS Step Functions CLI documentation can be found here: http://docs.aws.amazon.com/cli/latest/reference/stepfunctions/index.html.
Hope this helps!
Upvotes: 1