Skip to content

Commit

Permalink
Merge pull request #2042 from biswanathmukherjee/myusername-feature-s…
Browse files Browse the repository at this point in the history
…tepfunctions-httpendpoint-integration

New Serverless Pattern - Step Functions HTTP Endpoint integration (Corrected PR)
  • Loading branch information
parikhudit authored Jan 23, 2024
2 parents e7d9c43 + bfc51e7 commit 7c1b583
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 0 deletions.
95 changes: 95 additions & 0 deletions stepfunctions-httpendpoint-sam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# AWS Step Functions Workflow to demonstrate direct integration with third-party HTTP API

The Step Functions Workflow can be started using the AWS CLI or from another service (e.g. API Gateway).

The SAM template deploys a Step Functions Standard workflow that invokes a third-party API directly. The SAM template contains the required resouces with IAM permission to run the application with logging enabled.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/stepfunctions-httpendpoint-sam

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

## Requirements

* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
* [Stripe Test User](https://stripe.com/docs/connect/express-accounts)

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/serverless-patterns
```
2. Change directory to the pattern directory:
```
cd stepfunctions-httpendpoint-sam
```
3. Please update the `<USER_UPDATE_ME>` and `<PASSWORD_UPDATE_ME>` in `template.yaml` file with the Strip test user based on [Stripe Documentation](https://stripe.com/docs/connect/express-accounts). At the time of writing, as per the documentation the `USER_UPDATE_ME` is `sk_test_4eC39HqLyjWDarjtT1zdp7dc` and `PASSWORD_UPDATE_ME` can be a random string. It is not validated from Stripe but it can not be set to empty as Amazon EventBridge API Connection with empty password. Please refer to the Stripe documentation for latest user details.
4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
```
sam deploy --guided
```
5. During the prompts:
* Enter a stack name
* Enter `us-east-1` or any AWS Region where you have access. Please refer to the list of supported regions for `Invoke public HTTPS endpoints and test individual states` feature in [recent feature launches](https://docs.aws.amazon.com/step-functions/latest/dg/recent-launches.html)
* Allow SAM CLI to create IAM roles with the required permissions.
Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.
6. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
## How it works
* Start the Standard Workflow using the `start-execution` api command with a JSON input payload.
* The Workflow directly invokes the third-party API.
* EventBridge API Connection is used for the authentication of the third-party API.
Please refer to the architecture diagram below:
![End to End Architecture](image/architecture.png)
## Testing
1. Run the following AWS CLI command to start the Step Functions workflow. Note, you must edit the {StateMachineHTTPEndpointArn} placeholder with the ARN of the deployed Step Functions workflow. This is provided in the stack outputs. Please replace {your-region} with the region selected at the time of deployment.
```bash
aws stepfunctions start-execution --state-machine-arn "{StateMachineHTTPEndpointArn}" --region {your-region}
```

This Step Function does not require any input. However, If you need to pass any input to your own AWS Step Function, then you can pass the same using `--input {stringifiedJson}` format. For example: `--input "{\"orderId\": \"1234567\",\"customerId\": \"98766\",\"orderDate\": \"2024-01-14\",\"amount\": 100,\"nameOnCard\": \"FIRSTNAME LASTNAME\",\"creditCardNumber\": \"1234 1234 1234 1234\",\"expiry\": \"XX/YY\",\"cvv\": \"123\"}"`


### Example output:

```bash
{
"executionArn": "arn:aws:states:{your-region}:123456789012:execution:StateMachineHTTPEndpoint-mnZFTe6jJSDu:40e520d2-4d3e-42ce-a8e3-b33bfed22fc1",
"startDate": "2024-01-10T20:06:47.616000+05:30"
}
```

3. Run the following AWS CLI command to get the output of the third-party API call. Note, you must edit the {executionArn} placeholder with the `executionArn` from the above step. Please replace {your-region} with the region selected at the time of deployment.

```bash
aws stepfunctions describe-execution --execution-arn {executionArn} --query 'output' --region {your-region}
```

This will output the response of the third-party API. The same can also be validated from the AWS Step Functions console.



## Cleanup

Delete the stack
```bash
sam delete
```

----
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
58 changes: 58 additions & 0 deletions stepfunctions-httpendpoint-sam/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"title": "AWS Step Functions with direct integration to third-party HTTP API",
"description": "The SAM template deploys a Step Functions Standard workflow that invokes a third-party API directly.",
"language": "YAML",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"Start the Standard Workflow using the start-execution api command with a JSON input payload.",
"The Workflow directly invokes the third-party API.",
"EventBridge API Connection is used for the authentication of the third-party API."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/stepfunctions-httpendpoint-sam",
"templateURL": "serverless-patterns/stepfunctions-httpendpoint-sam",
"projectFolder": "stepfunctions-httpendpoint-sam",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "AWS Step Functions - Call third-party APIs",
"link": "https://docs.aws.amazon.com/step-functions/latest/dg/connect-third-party-apis.html"
},
{
"text": "External endpoints and testing of task states now available in AWS Step Functions",
"link": "https://aws.amazon.com/blogs/aws/external-endpoints-and-testing-of-task-states-now-available-in-aws-step-functions/"
}
]
},
"deploy": {
"text": [
"sam deploy --guided"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>sam delete</code>."
]
},
"authors": [
{
"name": "Biswanath Mukherjee",
"image": "https://d1rwvjey2iif32.cloudfront.net",
"bio": "I am a Sr. Solutions Architect working at AWS India.",
"linkedin": "biswanathmukherjee"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"Comment": "A a state machine to demonstrate HTTP Endpoint direct integration",
"StartAt": "Get Customers",
"States": {
"Get Customers": {
"Type": "Task",
"Resource": "arn:aws:states:::http:invoke",
"Parameters": {
"ApiEndpoint": "https://api.stripe.com/v1/customers",
"Method": "GET",
"Authentication": {
"ConnectionArn": "${ConnectionArn}"
}
},
"Retry": [
{
"ErrorEquals": [
"States.ALL"
],
"BackoffRate": 2,
"IntervalSeconds": 1,
"MaxAttempts": 3,
"JitterStrategy": "FULL"
}
],
"End": true,
"OutputPath": "$.ResponseBody"
}
}
}
76 changes: 76 additions & 0 deletions stepfunctions-httpendpoint-sam/stepfunctions-httpendpoint-sam.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"title": "AWS Step Functions with direct integration to third-party HTTP API",
"description": "The SAM template deploys a Step Functions Standard workflow that invokes a third-party API directly.",
"language": "YAML",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"Start the Standard Workflow using the start-execution api command with a JSON input payload.",
"The Workflow directly invokes the third-party API.",
"EventBridge API Connection is used for the authentication of the third-party API."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/stepfunctions-httpendpoint-sam",
"templateURL": "serverless-patterns/stepfunctions-httpendpoint-sam",
"projectFolder": "stepfunctions-httpendpoint-sam",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "AWS Step Functions - Call third-party APIs",
"link": "https://docs.aws.amazon.com/step-functions/latest/dg/connect-third-party-apis.html"
},
{
"text": "External endpoints and testing of task states now available in AWS Step Functions",
"link": "https://aws.amazon.com/blogs/aws/external-endpoints-and-testing-of-task-states-now-available-in-aws-step-functions/"
}
]
},
"deploy": {
"text": [
"sam deploy --guided"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>sam delete</code>."
]
},
"authors": [
{
"name": "Biswanath Mukherjee",
"image": "https://d1rwvjey2iif32.cloudfront.net",
"bio": "I am a Sr. Solutions Architect working at AWS India.",
"linkedin": "biswanathmukherjee"
}
],
"patternArch": {
"icon1": {
"x": 20,
"y": 50,
"service": "sfn",
"label": "AWS Step Functions"
},
"icon2": {
"x": 80,
"y": 50,
"service": "internet",
"label": "Third-party HTTP endpoint"
},
"line1": {
"from": "icon1",
"to": "icon2"
}
}
}
107 changes: 107 additions & 0 deletions stepfunctions-httpendpoint-sam/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
A Step Functions Workflow that demonstrates direct integration with third-party HTTP API
Resources:
##########################################################################
# STEP FUNCTION #
##########################################################################

StateMachineHTTPEndpoint:
Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
Properties:
DefinitionUri: statemachine/httpendpoint-integration.asl.json
DefinitionSubstitutions:
ConnectionArn: !GetAtt EventBridgeConnection.Arn
Role: !GetAtt StatesExecutionRole.Arn
Type: STANDARD
Logging:
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt StateMachineLogGroup.Arn
IncludeExecutionData: false
Level: 'ALL'

##########################################################################
# STEP FUNCTION LOG GROUP #
##########################################################################
StateMachineLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "/", [ "stepfunctions", StateMachineHTTPEndpoint]]

##########################################################################
# Roles #
##########################################################################
StatesExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- !Sub states.${AWS::Region}.amazonaws.com
Action: "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: CWLogs
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogDelivery"
- "logs:CreateLogStream"
- "logs:GetLogDelivery"
- "logs:UpdateLogDelivery"
- "logs:DeleteLogDelivery"
- "logs:ListLogDeliveries"
- "logs:PutLogEvents"
- "logs:PutResourcePolicy"
- "logs:DescribeResourcePolicies"
- "logs:DescribeLogGroups"
Resource: "*"
- PolicyName: StatesExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "events:RetrieveConnectionCredentials"
Resource:
- !GetAtt EventBridgeConnection.Arn
- Effect: Allow
Action:
- "secretsmanager:GetSecretValue"
- "secretsmanager:DescribeSecret"
Resource:
- !Sub "arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:events!connection/*"
- Effect: Allow
Action:
- "states:InvokeHTTPEndpoint"
Resource:
- !Sub "arn:aws:states:${AWS::Region}:${AWS::AccountId}:stateMachine:*"

##########################################################################
# EventBridge Connection #
##########################################################################
EventBridgeConnection:
Type: "AWS::Events::Connection"
Properties:
AuthorizationType: BASIC
AuthParameters:
BasicAuthParameters:
Password: "<PASSWORD_UPDATE_ME>"
Username: "<USER_UPDATE_ME>"

##########################################################################
# Outputs #
##########################################################################
Outputs:
StateMachineHTTPEndpointArn:
Value: !Ref StateMachineHTTPEndpoint
Description: StateMachineHTTPEndpoint Arn

0 comments on commit 7c1b583

Please sign in to comment.