2. About me: Metin Kale
I’m an AWS Certified Developer - Associate
I’m a Senior Java Developer
I can be reached at metinkale@gmail.com
My twitter handle is @metinkale
My LinkedIn is https://www.linkedin.com/in/metinkale
3. What’s API Gateway & Step Functions?
API Gateway is the service that allows you to create serverless endpoints
It integrates with many of AWS services
● $3.50 per million API calls received, plus the cost of data transfer out, in gigabytes
● $0.09/GB for the first 10 TB (and a sliding scale after this)
Step Functions is “a web service that enables you to coordinate the components
of distributed applications and microservices using visual workflows”
● You are charged by the number of transitions/mth
● Each transition above that are charged at $0.00025
4. Use Case: Insurance Policy Flows
Our architect decided to use Spring Cloud Data Flow within a Kubernetes cluster
I think there’s a better way… and that’s AWS Step Functions!
What we want:
● Create a new insurance policy
● Cancel an existing insurance policy
These two goals are my modus operandi for this project
5. Amazon States Language
You define and write your Step Functions (State Machine) in a JSON document
Root Fields
● StartAt (required)
● States (required)
● Comment
● Timeout
● Version
Resource: https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html
6. Amazon States Language
Let’s examine the States object
Typical Fields
● The name of your State
● Type (task, choice, succeed, fail, parallel, pass, wait)
● Next
● End
● Comment
8. API Gateway - POST
● Create a POST
● Under Integration Type, choose “AWS Service”
○ AWS Region: your region
○ Aws Service: Step Functions
○ Use HTTP Method: POST
○ Action: startExecution
○ Execution Role: create a new role (or existing one) with:
■ AWSStepFunctionsFullAccess
■ AmazonAPIGatewayPushToCloudWatchLogs
9. API Gateway - POST
● Under Integration Request
○ Body Mapping Templates
■ Content-Type: application/json
Add the following JSON:
{
“input”:"$util.escapeJavaScript($input.json('$'))",
"stateMachineArn": "arn:aws:states:us-east-1:ACCOUNT_ID:stateMachine:CreatePolicyFlow"
}
10. API Gateway - POST
● Under Integration Response
○ Open up the 200 status arrow
○ Under Body Mapping Templates, select application/json
Add the following JSON:
#set($obj = $input.json('$.executionArn'))
#set($tmp = $obj.replace('arn:aws:states:us-east-1:ACCOUNT_ID:execution:CreatePolicyFlow:', ''))
#set($executionId = $tmp.replace('"', ''))
#set($url = "https://api.javaauthority.com/executions?executionid=$executionId&flowid=CreatePolicyFlow")
{
"executionStatusUrl":"$url"
}
11. API Gateway - GET
● Create a new Resource called ‘execution’
● Under Method Request, add two URL Query String Parameters
○ executionid
○ Flowid
● Under Integration Request
○ Integration Type: AWS Service
○ AWS Region: your region
○ AWS Service: Steps Functions
○ HTTP Method: POST
○ Action: DescribeExecution
○ Execution Role: your IAM role
12. API Gateway - GET
● Still under Integration Request
○ Body Mapping Templates: application/json
{
"executionArn":
"arn:aws:states:us-east-1:ACCOUNT_ID:execution:$input.params('flowid'):$input.params('executionid')"
}
13. API Gateway - DELETE
● Create a DELETE
● Under Integration Type, choose “AWS Service”
○ AWS Region: your region
○ AWS Service: Step Functions
○ Use HTTP Method: POST
○ Action: startExecution
○ Execution Role: create a new role (or existing one) with:
■ AWSStepFunctionsFullAccess
■ AmazonAPIGatewayPushToCloudWatchLogs
14. API Gateway - DELETE
● Under Integration Request
○ Body Mapping Templates
■ Content-Type: application/json
Add the following JSON:
{
"input": "{"id":"$input.params('id')"}",
"stateMachineArn": "arn:aws:states:us-east-1:ACCOUNT_ID:stateMachine:CancelPolicyFlow"
}
15. API Gateway - DELETE
● Under Integration Response
○ Open up the 200 status arrow
○ Under Body Mapping Templates, select application/json
Add the following JSON:
#set($obj = $input.json('$.executionArn'))
#set($tmp = $obj.replace('arn:aws:states:us-east-1:ACCOUNT_ID:execution:CancelPolicyFlow:', ''))
#set($executionId = $tmp.replace('"', ''))
#set($url = "https://api.javaauthority.com/executions?executionid=$executionId&flowid=CancelPolicyFlow")
{
"executionStatusUrl":"$url"
}
16. Tips & Tricks
● Start with creating your lambda functions
● Create a simple flow and test it out
● Define your input as JSON
● Think about your Lambda naming convention
● Use the Test feature of API Gateway
Resource URL
https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html