Skip to content

Commit e141a6b

Browse files
authored
feat: Add aws-ruby-step-functions-express example (#639)
1 parent 68a6a62 commit e141a6b

17 files changed

+16403
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ serverless install -u https://github.com/serverless/examples/tree/master/folder-
128128
| [Aws Ruby Step Functions Callback](https://github.com/serverless/examples/tree/master/aws-ruby-step-functions-with-callback) <br/> Ruby example that make usage of AWS Step Functions with callback pattern, AWS Lambda, DynamoDB, Amazon Comprehend, API Gateway, and Step Functions flows | ruby |
129129
| [Serverless Ruby Sqs Dynamodb](https://github.com/serverless/examples/tree/master/aws-ruby-sqs-with-dynamodb) <br/> A serverless ruby example that creates DynamoDB records with the usage of SQS, API Gateway, and AWS Lambda functions. | ruby |
130130
| [Aws Ruby Step Functions](https://github.com/serverless/examples/tree/master/aws-ruby-step-functions) <br/> Ruby example that make usage of AWS Step Functions with AWS Lambda, DynamoDB and Step Functions flows. | ruby |
131+
| [Aws Ruby Step Functions Express Workflow](https://github.com/serverless/examples/tree/master/aws-ruby-step-functions-express) <br/> Ruby example that make usage of AWS Step Functions Express Type with AWS Lambda, DynamoDB, Amazon SES, API Gateway, and Step Functions flows | ruby |
131132
| [Aws Rust Simple Http Endpoint](https://github.com/serverless/examples/tree/master/aws-rust-simple-http-endpoint) <br/> Example demonstrates how to setup a simple HTTP GET endpoint with rust | nodeJS |
132133
| [Azure Nodejs](https://github.com/serverless/examples/tree/master/azure-node-line-bot) <br/> Azure Functions sample for the Serverless framework | nodeJS |
133134
| [Azure Node Simple Http Endpoint](https://github.com/serverless/examples/tree/master/azure-node-simple-http-endpoint) <br/> An example of making http endpoints with the Azure Functions Serverless Framework plugin | nodeJS |
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'aws-sdk-ses'
+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<!--
2+
title: 'Ruby AWS Ruby Step Functions Express'
3+
description: 'Ruby example that make usage of AWS Step Functions Express Type with AWS Lambda, DynamoDB, Amazon SES, API Gateway, and Step Functions flows.'
4+
layout: Doc
5+
framework: v2
6+
platform: AWS
7+
language: Ruby
8+
authorLink: 'https://github.com/pigius'
9+
authorName: 'Daniel Aniszkiewicz'
10+
authorAvatar: 'https://avatars1.githubusercontent.com/u/8863200?s=200&v=4'
11+
-->
12+
# Serverless AWS Ruby Step Functions Express
13+
14+
![diagram](./images/express-workflow-draw.png)
15+
16+
17+
This is an example of using `AWS Step Functions` `Express` Workflow Type. It uses `AWS Lambda`, `DynamoDB`, `Amazon SES`, `API Gateway` and `flows` from `Step Functions`.
18+
19+
20+
## Diagram
21+
22+
![diagram](./images/stepfunctions_express_graph.png)
23+
24+
It's a simple workflow, it adds the user to the database (`DynamoDB`) and if the save is successful, we send an email notification (`AWS Lambda` + `Amazon SES`). We also have error handling.
25+
26+
![detailed-diagram](./images/express-diagram-visual.png)
27+
28+
## Setup
29+
30+
`npm install` to install all needed packages.
31+
32+
## Deployment
33+
34+
In order to deploy the service run:
35+
36+
```bash
37+
sls deploy
38+
```
39+
40+
for deploying with a specific `profile` (located in `~/.aws/credentials`) you can simply use the command:
41+
42+
```bash
43+
AWS_PROFILE=YOUR_PROFILE_NAME sls deploy
44+
```
45+
46+
for deploying to the specific stage, let's say `staging` do:
47+
48+
```bash
49+
sls deploy --stage staging
50+
```
51+
52+
The expected result should be similar to:
53+
54+
```bash
55+
Serverless: Running "serverless" installed locally (in service node_modules)
56+
Serverless: Packaging service...
57+
Serverless: Excluding development dependencies...
58+
Serverless: Clearing previous build ruby layer build
59+
[ '2.2' ]
60+
Serverless: Installing gem using local bundler
61+
Serverless: Zipping the gemfiles to sls-examples/examples/aws-ruby-step-functions-express/.serverless/ruby_layer/gemLayer.zip
62+
Serverless: Configuring Layer and GEM_PATH to the functions
63+
✓ State machine "myStateMachine" definition is valid
64+
Serverless: Uploading CloudFormation file to S3...
65+
Serverless: Uploading artifacts...
66+
Serverless: Uploading service aws-ruby-step-functions.zip file to S3 (971.85 KB)...
67+
Serverless: Uploading service gemLayer.zip file to S3 (553.86 KB)...
68+
Serverless: Validating template...
69+
Serverless: Updating Stack...
70+
Serverless: Checking Stack update progress...
71+
................................
72+
Serverless: Stack update finished...
73+
Service Information
74+
service: aws-ruby-step-functions
75+
stage: dev
76+
region: eu-west-1
77+
stack: aws-ruby-step-functions-dev
78+
resources: 16
79+
api keys:
80+
None
81+
endpoints:
82+
functions:
83+
send-email: aws-ruby-step-functions-dev-send-email
84+
layers:
85+
gem: arn:aws:lambda:your-region:XXXXXXXXXXX:layer:aws-ruby-step-functions-dev-ruby-bundle:2
86+
Serverless StepFunctions OutPuts
87+
endpoints:
88+
POST - https://XXXXXXXXXXX.execute-api.your-region.amazonaws.com/dev/employees/add
89+
```
90+
91+
92+
## Prerequistes
93+
94+
Make sure to validate `sender` and `recipient` emails within the `Amazon SES` (most likely for testing it, it will be one `email`):
95+
96+
![verify-email](./images/verify-email.png)
97+
98+
Later on within `serverless.yml` edit in `custom` section, both `sender` and `recipient`.
99+
100+
## Usage
101+
102+
There are two possible ways of invoking the example:
103+
104+
## Via Api Gateway
105+
106+
After the deployment, grab the POST endpoint for this service. You can make a API call either by cURL or some tools like Postman.
107+
108+
Use payload like:
109+
110+
```json
111+
{
112+
"id": "The name of the employee",
113+
"jobTitle": "the name of the employee's position"
114+
}
115+
```
116+
As a response you will get:
117+
118+
```json
119+
{
120+
"executionArn": "arn:aws:states:your-region:XXXXXXXXXXX:express:add-employee-and-send-email-state-machine-dev:e9fcce40-0642-48cb-bd9a-4d3d9cea7bcc:da5ee796-5944-4426-9ca2-410a5003ceee",
121+
"startDate": 1.62885382751E9
122+
}
123+
```
124+
125+
![dynamo](./images/postman-express-step-functions.png)
126+
127+
128+
After it, you can then check the Dynamo database to see if a record has been created, and check the email.
129+
130+
![dynamo](./images/dynamodb-results-express.png)
131+
132+
![email](./images/email-express-step-functions.png)
133+
134+
## Via AWS Dashboard
135+
136+
After the deployment, go to the AWS Dashboard, and enter Step Functions page. You will see a newly created state machine.
137+
138+
Open the state machine and click on `Start Execution`. You need to provide the input in the JSON schema.
139+
140+
Example:
141+
142+
``` JSON
143+
{
144+
"id": "The name of the employee",
145+
"jobTitle": "the name of the employee's position"
146+
}
147+
```
148+
149+
Later on, simply start the excecution.
150+
151+
After it, you can then check the Dynamo database to see if a record has been created, and check the email.
152+
153+
154+
Please keep in mind, that the `Express workflow` is different from the `Standard workflow`. Check the differences [here](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html). The main point here is the fact that you can't see all transitions between states. However, you can still use Workflow Studio.
155+
156+
157+
## Logs
158+
159+
![logs](./images/metrics-express.png)
160+
161+
162+
By default, the logging is disabled. You can easily enabled it. The description is [here](https://www.serverless.com/plugins/serverless-step-functions#cloudwatch-logs).
163+
164+
## Log retention
165+
166+
The log retention is setup for 30 days. To change it simply change the value of this attribute in `serverless.yml` file:
167+
168+
``` bash
169+
logRetentionInDays: 30
170+
```
171+
172+
## Advanced configuration
173+
More options could be found in the plugin [repository](https://github.com/serverless-operations/serverless-step-functions).
174+
175+
## Structure
176+
177+
| Path | Explanation |
178+
|-----------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
179+
| `./src` | All code for the project. |
180+
| `./src/handlers/send-email` | Handler for lambda. |
181+
| `./src/common/` | Space for common, reusable pieces of code. |
182+
| `./src/common/adapters/ses_adapter.rb` | Adapter for the Amazon SES with the usage of AWS SDK for Ruby. Only used for sending emails. |
183+
| `./src/common/services/send_email_service.rb` | The service object pattern is widely used within ruby/rails developers. A class that is responsible for doing only one thing. In our case is creating an email. | |
184+
185+
## Serverless plugins
186+
187+
For this example, there are two serverless plugins used:
188+
189+
| Plugin | Explanation |
190+
|-----------------------|------------------------------------------------------------------------------------------------|
191+
| [serverless-ruby-layer](https://www.npmjs.com/package/serverless-ruby-layer) | For bundling ruby gems from `Gemfile` and deploys them to the lambda layer. |
192+
| [serverless-step-functions](https://www.npmjs.com/package/serverless-step-functions) | Serverless Framework plugin for AWS Step Functions. |
193+
194+
## Ruby gems
195+
196+
| Gem | Explanation |
197+
|--------------------|--------------------------------------------------------------------------------------------------------------------------------|
198+
| `aws-sdk-ses` | It's a part of the AWS SDK for Ruby. Used for Amazon SES, in the case of this example - sending emails. |
199+
200+
## Remove service
201+
202+
To remove the service do:
203+
204+
```bash
205+
sls remove
206+
```
207+
And the stack will be removed from the AWS.
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)