Skip to content

Commit 82de1ed

Browse files
authored
add sample for stepfunctions using lambdas and localstack (#148)
* add sample for stepfunctions using lambdas and localstack * ci: fix failing build * fix nits
1 parent 56576e2 commit 82de1ed

File tree

8 files changed

+181
-0
lines changed

8 files changed

+181
-0
lines changed

.github/workflows/makefile.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
- name: Install Dependencies
3232
run: |
3333
pip install virtualenv
34+
pip install --upgrade pyopenssl
3435
npm install -g serverless
3536
3637
- name: Setup config

stepfunctions-lambda/Makefile

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
export AWS_ACCESS_KEY_ID ?= test
2+
export AWS_SECRET_ACCESS_KEY ?= test
3+
export AWS_DEFAULT_REGION=us-east-1
4+
5+
usage: ## Show this help
6+
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
7+
8+
install: ## Install dependencies
9+
@which awslocal || pip install awscli-local
10+
11+
create-lambdas: ## Create the Lambdas
12+
awslocal iam create-role --role-name step-function-lambda --assume-role-policy-document file://step-trust-policy.json
13+
awslocal iam attach-role-policy --role-name step-function-lambda --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaRole
14+
15+
cp -f lambda_adam.py lambda_function.py
16+
zip package.zip lambda_function.py
17+
18+
awslocal lambda create-function \
19+
--function-name adam \
20+
--role arn:aws:iam::000000000000:role/lambda-demo \
21+
--runtime python3.9 --timeout 10 --memory-size 128 \
22+
--handler lambda_function.lambda_handler \
23+
--zip-file fileb://package.zip
24+
25+
rm package.zip lambda_function.py
26+
27+
cp -f lambda_cole.py lambda_function.py
28+
zip package.zip lambda_function.py
29+
30+
awslocal lambda create-function \
31+
--function-name cole \
32+
--role arn:aws:iam::000000000000:role/lambda-demo \
33+
--runtime python3.9 --timeout 10 --memory-size 128 \
34+
--handler lambda_function.lambda_handler \
35+
--zip-file fileb://package.zip
36+
37+
rm package.zip lambda_function.py
38+
39+
cp -f lambda_combine.py lambda_function.py
40+
zip package.zip lambda_function.py
41+
42+
awslocal lambda create-function \
43+
--function-name combine \
44+
--role arn:aws:iam::000000000000:role/lambda-demo \
45+
--runtime python3.9 --timeout 10 --memory-size 128 \
46+
--handler lambda_function.lambda_handler \
47+
--zip-file fileb://package.zip
48+
49+
rm package.zip lambda_function.py
50+
51+
start:
52+
localstack start -d
53+
54+
stop:
55+
@echo
56+
localstack stop
57+
58+
ready:
59+
@echo Waiting on the LocalStack container...
60+
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)
61+
62+
logs:
63+
@localstack logs > logs.txt
64+
65+
test-ci:
66+
make start install ready create-lambdas; return_code=`echo $$?`;\
67+
make logs; make stop; exit $$return_code;
68+
69+
.PHONY: usage install create-lambdas run stop ready logs test-ci

stepfunctions-lambda/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Creating a Lambda function with a function URL
2+
3+
In this example, we will demonstrate how to create an AWS Step Function with Lambda functions in LocalStack.
4+
5+
## Prerequisites
6+
7+
* LocalStack
8+
* Docker
9+
* `awslocal` CLI
10+
11+
## Starting up
12+
13+
Start LocalStack via:
14+
15+
```sh
16+
localstack start -d
17+
```
18+
19+
Run the following command to create the Lambda functions:
20+
21+
```sh
22+
make create-lambdas
23+
```
24+
25+
## Setting up and running Step Function
26+
27+
Create the Step Function:
28+
29+
```sh
30+
awslocal stepfunctions create-state-machine --name step-demo \
31+
--definition "$(cat step-definition.json)" \
32+
--role-arn arn:aws:iam::000000000000:role/step-function-lambda
33+
```
34+
35+
Start the execution:
36+
37+
```sh
38+
awslocal stepfunctions start-execution \
39+
--state-machine-arn arn:aws:states:us-east-1:000000000000:stateMachine:step-demo \
40+
--input '{"adam": "LocalStack", "cole": "Stack"}'
41+
```
42+
43+
This creates and invokes the flow between the three Lambda functions we created using LocalStack earlier.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def lambda_handler(event, context):
2+
print(event)
3+
return event["input"]["adam"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def lambda_handler(event, context):
2+
print(event)
3+
return event["input"]["cole"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def lambda_handler(event, context):
2+
print(event)
3+
return "Together Adam and Cole say '{}'!!".format(' '.join(event["input"]))
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"Comment": "Parallel Demo",
3+
"StartAt": "Parallel State",
4+
"States":
5+
{
6+
"Parallel State": {
7+
"Type": "Parallel",
8+
"Next": "combine",
9+
"Branches": [
10+
{
11+
"StartAt": "adam",
12+
"States": {
13+
"adam": {
14+
"Type": "Task",
15+
"Resource": "arn:aws:states:::lambda:invoke",
16+
"Parameters": {
17+
"FunctionName": "arn:aws:lambda:us-east-1:000000000000:function:adam",
18+
"Payload": {"input.$": "$"}},
19+
"OutputPath": "$.Payload",
20+
"End": true}}
21+
},
22+
{
23+
"StartAt": "cole",
24+
"States": {
25+
"cole": {
26+
"Type": "Task",
27+
"Resource": "arn:aws:states:::lambda:invoke",
28+
"Parameters": {
29+
"FunctionName": "arn:aws:lambda:us-east-1:000000000000:function:cole",
30+
"Payload": {"input.$": "$"}},
31+
"OutputPath": "$.Payload",
32+
"End": true
33+
}
34+
}
35+
}
36+
]
37+
},
38+
"combine": {
39+
"Type": "Task",
40+
"Resource": "arn:aws:states:::lambda:invoke",
41+
"Parameters": {
42+
"FunctionName": "arn:aws:lambda:us-east-1:000000000000:function:combine",
43+
"Payload": {"input.$": "$"}},
44+
"OutputPath": "$.Payload",
45+
"End": true}
46+
}
47+
}
48+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Principal": {"Service": ["states.amazonaws.com"]},
7+
"Action": "sts:AssumeRole"
8+
}
9+
]
10+
}
11+

0 commit comments

Comments
 (0)