You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: human-in-the-loop/README.md
+27-13Lines changed: 27 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Human in the Loop
2
2
3
-
This pattern allows you to integrate an human review or approval process into your workflows. Each task sends a message to a SNS topic which sends a notification to a human reviewer or approver by email for example. The workflow then waits until the approver completes their review. Depending on the review outcome a different Lambda function can be invoked.
3
+
This pattern allows you to integrate a human review or approval process into your workflows with **one-click email approval**. An AWS Lambda function sends an approval request via Amazon SNS email containing clickable approve/reject links. The task token is URL-encoded to ensure special characters don't break the Amazon API Gateway callback URL. The workflow pauses until the reviewer clicks a link, which triggers an API Gateway endpoint to resume the AWS Step Functions execution.
4
4
5
5
Learn more about this workflow at Step Functions workflows collection: [Human in the Loop](https://serverlessland.com/workflows/human-in-the-loop)
6
6
@@ -12,6 +12,7 @@ Important: this application uses various AWS services and there are costs associ
12
12
*[AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
@@ -41,24 +42,37 @@ Important: this application uses various AWS services and there are costs associ
41
42
42
43

43
44
44
-
1. Data that should be reviewed by a human is passed to the workflow. A message is send to a [Amazon Simple Notification Service (SNS)](https://aws.amazon.com/sns/) topic which sends out a notification via Email. The notification contains a [task token](https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-resource.html#connect-wait-token) which is automatically generated by AWS Step Functions.
45
-
2. After approving or denying, the reviewer calls the `SendTaskSuccess` API and passes the task token as well as the review result.
46
-
3. The result is evaluated by Step Functions and the corresponding AWS Lambda function is invoked.
45
+
1. Data that should be reviewed by a human is passed to the workflow. The state machine invokes a Lambda function using the `.waitForTaskToken` integration pattern. The Lambda function URL-encodes the task token and constructs approve/reject links pointing to an API Gateway endpoint.
46
+
2. The Lambda function publishes an email via [Amazon Simple Notification Service (SNS)](https://aws.amazon.com/sns/) containing the clickable approve/reject links.
47
+
3. The reviewer clicks one of the links in the email. This triggers a GET request to API Gateway.
48
+
4. API Gateway invokes a second Lambda function that decodes the task token and calls `SendTaskSuccess` (approve) or `SendTaskFailure` (reject) on the Step Functions execution.
49
+
5. The workflow resumes and routes to the appropriate processing Lambda based on the approval outcome.
50
+
47
51
48
52
## Testing
49
53
50
54
1. After deployment you receive an email titled `AWS Notification - Subscription Confirmation`. Click on the link in the email to confirm your subscription. This will allow SNS to send you emails.
51
55
2. Navigate to the AWS Step Functions console and select the `human-in-the-loop` workflow.
52
56
3. Select `Start Execution` and use any valid JSON data as input.
53
-
4. Select `Start Execution` and wait until you receive the email from SNS.
54
-
5. Copy the task token from the email.
55
-
6. Use the AWS CLI to complete the task by calling the `SendTaskSuccess` API. Replace the task token with the value you copied earlier.
Make sure to use that the cli uses the same region as the one you used to deploy your state machine.
60
-
5. Observe the task in the Step Functions console. Because response states that the approval was granted, the task transitioned to the `Process Approval` step.
61
-
6. If you trigger a new execution and replace `{"result":true}` with `{"result":false}` in step 6, the workflow transitions to `Process Rejection` respectively.
57
+
4. Select `Start Execution` and wait until you receive the approval request email from SNS.
58
+
5. Click the **Approve** or **Reject** link in the email.
59
+
6. You will see a confirmation page in your browser indicating the workflow was approved or rejected.
60
+
7. Observe the execution in the Step Functions console — the workflow transitions to `Handle approval` or `Handle rejection` based on your response.
61
+
62
+
### Testing via CLI (alternative)
63
+
64
+
You can also test the API Gateway endpoint directly:
Copy file name to clipboardExpand all lines: human-in-the-loop/example-workflow.json
+23-9Lines changed: 23 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,23 @@
1
1
{
2
2
"title": "Human in the Loop",
3
-
"description": "Wait for an approval from a human reviewer before continuing",
4
-
"language": "",
3
+
"description": "Wait for an approval from a human reviewer with one-click email approve/reject links",
4
+
"language": "Python",
5
5
"simplicity": "2 - Pattern",
6
6
"usecase": "",
7
7
"type": "Standard",
8
8
"diagram":"/resources/statemachine.png",
9
9
"videoId": "",
10
-
"level": "100",
10
+
"level": "200",
11
11
"framework": "SAM",
12
-
"services": ["sns","lambda"],
12
+
"services": ["sns","lambda","apigateway"],
13
13
"introBox": {
14
14
"headline": "How it works",
15
15
"text": [
16
-
"This pattern allows you to integrate an human review or approval process into your workflows.",
17
-
"The task sends a message to a SNS topic which sends a notification to a human reviewer or approver by email for example. The workflow then waits until the approver completes their review. Depending on the review outcome a different Lambda function can be invoked.",
18
-
"Waiting for completion of the review is done using the .waitForTaskToken service integration. The payload of the SNS message contains a task token, which is automatically generated by AWS Step Functions.",
19
-
"The task will pause until it receives that task token back with a SendTaskSuccess or SendTaskFailure API call."
16
+
"This pattern allows you to integrate a human review or approval process into your workflows with one-click email approval.",
17
+
"A Lambda function sends an approval request via SNS email containing clickable approve/reject links. The task token is URL-encoded to avoid issues with special characters in API Gateway URLs.",
18
+
"The workflow pauses using the .waitForTaskToken service integration until the reviewer clicks an approve or reject link.",
19
+
"The link triggers an API Gateway endpoint backed by a Lambda function that calls SendTaskSuccess or SendTaskFailure to resume the workflow.",
20
+
"Depending on the review outcome, a different processing path is followed."
0 commit comments