Skip to content

Commit 76734cf

Browse files
docs: updated README
1 parent dbd44fd commit 76734cf

File tree

1 file changed

+72
-32
lines changed

1 file changed

+72
-32
lines changed

README.md

+72-32
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) [![Build Status](https://travis-ci.org/horike37/serverless-step-functions.svg?branch=master)](https://travis-ci.org/horike37/serverless-step-functions) [![npm version](https://badge.fury.io/js/serverless-step-functions.svg)](https://badge.fury.io/js/serverless-step-functions) [![Coverage Status](https://coveralls.io/repos/github/horike37/serverless-step-functions/badge.svg?branch=master)](https://coveralls.io/github/horike37/serverless-step-functions?branch=master) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) [![serverless-step-functions Dev Token](https://badge.devtoken.rocks/serverless-step-functions)](https://devtoken.rocks/package/serverless-step-functions)
21
# Serverless Step Functions
2+
3+
[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) [![Build Status](https://travis-ci.org/horike37/serverless-step-functions.svg?branch=master)](https://travis-ci.org/horike37/serverless-step-functions) [![npm version](https://badge.fury.io/js/serverless-step-functions.svg)](https://badge.fury.io/js/serverless-step-functions) [![Coverage Status](https://coveralls.io/repos/github/horike37/serverless-step-functions/badge.svg?branch=master)](https://coveralls.io/github/horike37/serverless-step-functions?branch=master) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) [![serverless-step-functions Dev Token](https://badge.devtoken.rocks/serverless-step-functions)](https://devtoken.rocks/package/serverless-step-functions)
4+
35
This is the Serverless Framework plugin for AWS Step Functions.
46

57
## Install
8+
69
Run `npm install` in your Serverless project.
7-
```
8-
$ npm install --save-dev serverless-step-functions
9-
```
10+
11+
`$ npm install --save-dev serverless-step-functions`
1012

1113
Add the plugin to your serverless.yml file
14+
1215
```yml
1316
plugins:
1417
- serverless-step-functions
1518
```
1619
1720
## Setup
21+
1822
Specifies your statemachine definition using Amazon States Language in a `definition` statement in serverless.yml. You can use CloudFormation intrinsic functions such as `Ref` and `Fn::GetAtt` to reference Lambda functions, SNS topics, SQS queues and DynamoDB tables declared in the same `serverless.yml`.
1923

2024
Alternatively, you can also provide the raw ARN, or SQS queue URL, or DynamoDB table name as a string. If you need to construct the ARN by hand, then we recommend to use the [serverless-pseudo-parameters](https://www.npmjs.com/package/serverless-pseudo-parameters) plugin together to make your life easier.
@@ -47,7 +51,7 @@ stepFunctions:
4751
HelloWorld1:
4852
Type: Task
4953
Resource:
50-
Ref: HelloLambdaFunction
54+
Fn::GetAtt: [HelloLambdaFunction, Arn]
5155
End: true
5256
dependsOn: CustomIamRole
5357
tags:
@@ -71,7 +75,7 @@ stepFunctions:
7175
HelloWorld2:
7276
Type: Task
7377
Resource:
74-
Ref: HelloLambdaFunction
78+
Fn::GetAtt: [HelloLambdaFunction, Arn]
7579
End: true
7680
dependsOn:
7781
- DynamoDBTable
@@ -89,6 +93,7 @@ plugins:
8993
```
9094

9195
### Adding a custom name for a stateMachine
96+
9297
In case you need to interpolate a specific stage or service layer variable as the
9398
stateMachines name you can add a `name` property to your yaml.
9499

@@ -111,6 +116,7 @@ plugins:
111116
```
112117

113118
### Adding a custom logical id for a stateMachine
119+
114120
You can use a custom logical id that is only unique within the stack as opposed to the name that needs to be unique globally. This can make referencing the state machine easier/simpler because you don't have to duplicate the interpolation logic everywhere you reference the state machine.
115121

116122
```yml
@@ -135,6 +141,7 @@ plugins:
135141
You can then `Ref: SendMessageStateMachine` in various parts of CloudFormation or serverless.yml
136142

137143
#### Depending on another logical id
144+
138145
If your state machine depends on another resource defined in your `serverless.yml` then you can add a `dependsOn` field to the state machine `definition`. This would add the `DependsOn`clause to the generated CloudFormation template.
139146

140147
This `dependsOn` field can be either a string, or an array of strings.
@@ -152,6 +159,7 @@ stepFunctions:
152159
```
153160

154161
#### CloudWatch Alarms
162+
155163
It's common practice to want to monitor the health of your state machines and be alerted when something goes wrong. You can either:
156164

157165
* do this using the [serverless-plugin-aws-alerts](https://github.com/ACloudGuru/serverless-plugin-aws-alerts), which lets you configure custom CloudWatch Alarms against the various metrics that Step Functions publishes.
@@ -186,6 +194,7 @@ You can configure how the CloudWatch Alarms should treat missing data:
186194
For more information, please refer to the [official documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html#alarms-and-missing-data).
187195

188196
The generated CloudWatch alarms would have the following configurations:
197+
189198
```yaml
190199
namespace: 'AWS/States'
191200
metric: <ExecutionsTimeOut | ExecutionsFailed | ExecutionsAborted | ExecutionThrottled>
@@ -218,11 +227,12 @@ alarms:
218227
```
219228

220229
#### Current Gotcha
230+
221231
Please keep this gotcha in mind if you want to reference the `name` from the `resources` section. To generate Logical ID for CloudFormation, the plugin transforms the specified name in serverless.yml based on the following scheme.
222232

223-
- Transform a leading character into uppercase
224-
- Transform `-` into Dash
225-
- Transform `_` into Underscore
233+
* Transform a leading character into uppercase
234+
* Transform `-` into Dash
235+
* Transform `_` into Underscore
226236

227237
If you want to use variables system in name statement, you can't put the variables as a prefix like this:`${self:service}-${opt:stage}-myStateMachine` since the variables are transformed within Output section, as a result, the reference will be broken.
228238

@@ -243,11 +253,14 @@ resources:
243253
```
244254

245255
## Events
256+
246257
### API Gateway
258+
247259
To create HTTP endpoints as Event sources for your StepFunctions statemachine
248260

249261
#### Simple HTTP Endpoint
250-
This setup specifies that the hello statemachine should be run when someone accesses the API gateway at hello via a GET request.
262+
263+
This setup specifies that the hello state machine should be run when someone accesses the API gateway at hello via a GET request.
251264

252265
Here's an example:
253266

@@ -261,6 +274,7 @@ stepFunctions:
261274
method: GET
262275
definition:
263276
```
277+
264278
#### HTTP Endpoint with Extended Options
265279

266280
Here You can define an POST endpoint for the path posts/create.
@@ -293,6 +307,7 @@ provider:
293307
functions:
294308
...
295309
```
310+
296311
If your application has many nested paths, you might also want to break them out into smaller services.
297312

298313
However, Cloudformation will throw an error if we try to generate an existing path resource. To avoid that, we reference the resource ID:
@@ -444,10 +459,27 @@ stepFunctions:
444459
Ref: ApiGatewayAuthorizer # or hard-code Authorizer ID
445460
```
446461

462+
#### LAMBDA_PROXY request template
463+
464+
The plugin generates default body mapping templates for `application/json` and `application/x-www-form-urlencoded` content types. The default template would pass the request body as input to the state machine. If you need access to other contextual information about the HTTP request such as headers, path parameters, etc. then you can also use the `lambda_proxy` request template like this:
465+
466+
```yml
467+
stepFunctions:
468+
stateMachines:
469+
hello:
470+
events:
471+
- http:
472+
path: posts/create
473+
method: POST
474+
request:
475+
template: lambda_proxy
476+
```
477+
478+
This would generate the normal LAMBDA_PROXY template used for API Gateway integration with Lambda functions.
447479

448480
#### Customizing request body mapping templates
449481

450-
The plugin generates default body mapping templates for `application/json` and `application/x-www-form-urlencoded` content types. If you'd like to add more content types or customize the default ones, you can do so by including your custom [API Gateway request mapping template](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html) in `serverless.yml` like so:
482+
If you'd like to add content types or customize the default templates, you can do so by including your custom [API Gateway request mapping template](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html) in `serverless.yml` like so:
451483

452484
```yml
453485
stepFunctions:
@@ -472,13 +504,13 @@ stepFunctions:
472504
```
473505

474506
#### Send request to an API
507+
475508
You can input an value as json in request body, the value is passed as the input value of your statemachine
476509

477-
```
478-
$ curl -XPOST https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/posts/create -d '{"foo":"bar"}'
479-
```
510+
`$ curl -XPOST https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/posts/create -d '{"foo":"bar"}'`
480511

481512
#### Setting API keys for your Rest API
513+
482514
You can specify a list of API keys to be used by your service Rest API by adding an apiKeys array property to the provider object in serverless.yml. You'll also need to explicitly specify which endpoints are private and require one of the api keys to be included in the request by adding a private boolean property to the http event object you want to set as private. API Keys are created globally, so if you want to deploy your service to different stages make sure your API key contains a stage variable as defined below. When using API keys, you can optionally define usage plan quota and throttle, using usagePlan object.
483515

484516
Here's an example configuration for setting API keys for your service Rest API:
@@ -519,7 +551,7 @@ functions:
519551
HelloWorld1:
520552
Type: Task
521553
Resource:
522-
Ref: HelloLambdaFunction
554+
Fn::GetAtt: [HelloLambdaFunction, Arn]
523555
End: true
524556
525557
@@ -533,6 +565,7 @@ Please note that those are the API keys names, not the actual values. Once you d
533565
Clients connecting to this Rest API will then need to set any of these API keys values in the x-api-key header of their request. This is only necessary for functions where the private property is set to true.
534566

535567
### Schedule
568+
536569
The following config will attach a schedule event and causes the stateMachine `crawl` to be called every 2 hours. The configuration allows you to attach multiple schedules to the same stateMachine. You can either use the `rate` or `cron` syntax. Take a look at the [AWS schedule syntax documentation](http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html) for more details.
537570

538571
```yaml
@@ -592,9 +625,10 @@ events:
592625
- schedule:
593626
rate: rate(2 hours)
594627
role: arn:aws:iam::xxxxxxxx:role/yourRole
595-
628+
```
596629

597630
### CloudWatch Event
631+
598632
## Simple event definition
599633

600634
This will enable your Statemachine to be called by an EC2 event rule.
@@ -733,20 +767,20 @@ You can specify tags on each state machine. Additionally any global tags (specif
733767
## Command
734768

735769
### deploy
770+
736771
Run `sls deploy`, the defined Stepfunctions are deployed.
737772

738773
### invoke
739-
```
740-
$ sls invoke stepf --name <stepfunctionname> --data '{"foo":"bar"}'
741-
```
774+
775+
`$ sls invoke stepf --name <stepfunctionname> --data '{"foo":"bar"}'`
742776

743777
#### options
744778

745-
- --name or -n The name of the step function in your service that you want to invoke. Required.
746-
- --stage or -s The stage in your service you want to invoke your step function.
747-
- --region or -r The region in your stage that you want to invoke your step function.
748-
- --data or -d String data to be passed as an event to your step function.
749-
- --path or -p The path to a json file with input data to be passed to the invoked step function.
779+
* --name or -n The name of the step function in your service that you want to invoke. Required.
780+
* --stage or -s The stage in your service you want to invoke your step function.
781+
* --region or -r The region in your stage that you want to invoke your step function.
782+
* --data or -d String data to be passed as an event to your step function.
783+
* --path or -p The path to a json file with input data to be passed to the invoked step function.
750784

751785
## IAM Role
752786

@@ -784,7 +818,9 @@ resources:
784818
The short form of the intrinsic functions (i.e. `!Sub`, `!Ref`) is not supported at the moment.
785819

786820
## Tips
821+
787822
### How to specify the stateMachine ARN to environment variables
823+
788824
Here is serverless.yml sample to specify the stateMachine ARN to environment variables.
789825
This makes it possible to trigger your statemachine through Lambda events
790826

@@ -812,8 +848,11 @@ resources:
812848
plugins:
813849
- serverless-step-functions
814850
```
851+
815852
## Sample statemachines setting in serverless.yml
853+
816854
### Wait State
855+
817856
``` yaml
818857
functions:
819858
hello:
@@ -829,7 +868,7 @@ stepFunctions:
829868
FirstState:
830869
Type: Task
831870
Resource:
832-
Ref: HelloLambdaFunction
871+
Fn::GetAtt: [HelloLambdaFunction, Arn]
833872
Next: wait_using_seconds
834873
wait_using_seconds:
835874
Type: Wait
@@ -850,14 +889,15 @@ stepFunctions:
850889
FinalState:
851890
Type: Task
852891
Resource:
853-
Ref: HelloLambdaFunction
892+
Fn::GetAtt: [HelloLambdaFunction, Arn]
854893
End: true
855894
plugins:
856895
- serverless-step-functions
857896
- serverless-pseudo-parameters
858897
```
859898

860899
### Retry Failture
900+
861901
``` yaml
862902
functions:
863903
hello:
@@ -873,7 +913,7 @@ stepFunctions:
873913
HelloWorld:
874914
Type: Task
875915
Resource:
876-
Ref: HelloLambdaFunction
916+
Fn::GetAtt: [HelloLambdaFunction, Arn]
877917
Retry:
878918
- ErrorEquals:
879919
- HandledError
@@ -954,7 +994,7 @@ stepFunctions:
954994
HelloWorld:
955995
Type: Task
956996
Resource:
957-
Ref: HelloLambdaFunction
997+
Fn::GetAtt: [HelloLambdaFunction, Arn]
958998
Catch:
959999
- ErrorEquals: ["HandledError"]
9601000
Next: CustomErrorFallback
@@ -1003,7 +1043,7 @@ stepFunctions:
10031043
FirstState:
10041044
Type: Task
10051045
Resource:
1006-
Ref: Hello1LambdaFunction
1046+
Fn::GetAtt: [Hello1LambdaFunction, Arn]
10071047
Next: ChoiceState
10081048
ChoiceState:
10091049
Type: Choice
@@ -1018,20 +1058,20 @@ stepFunctions:
10181058
FirstMatchState:
10191059
Type: Task
10201060
Resource:
1021-
Ref: Hello2LambdaFunction
1061+
Fn::GetAtt: [Hello2LambdaFunction, Arn]
10221062
Next: NextState
10231063
SecondMatchState:
10241064
Type: Task
10251065
Resource:
1026-
Ref: Hello3LambdaFunction
1066+
Fn::GetAtt: [Hello3LambdaFunction, Arn]
10271067
Next: NextState
10281068
DefaultState:
10291069
Type: Fail
10301070
Cause: "No Matches!"
10311071
NextState:
10321072
Type: Task
10331073
Resource:
1034-
Ref: Hello4LambdaFunction
1074+
Fn::GetAtt: [Hello4LambdaFunction, Arn]
10351075
End: true
10361076
plugins:
10371077
- serverless-step-functions

0 commit comments

Comments
 (0)