Skip to content

Commit

Permalink
Add an example of AWS StepFunctions in Python (pulumi#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeduffy committed Jul 17, 2018
1 parent 351cd51 commit a295ef5
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws-py-stepfunctions/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: stepfunctions-py
description: Basic example of AWS Step Functions in Python.
runtime: python
21 changes: 21 additions & 0 deletions aws-py-stepfunctions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# AWS Step Functions (Python)

A basic example that demonstrates using AWS Step Functions with a Lambda function, written in Python.

```
# Install dependencies
$ pip install -r ./requirements.txt
# Create and configure a new stack
$ pulumi stack init stepfunctions-dev
$ pulumi config set aws:region us-east-2
# Preview and run the deployment
$ pulumi update
# Start execution using the AWS CLI (or from the console at https://console.aws.amazon.com/states)
$ aws stepfunctions start-execution --state-machine-arn $(pulumi stack output state_machine_arn)
# Remove the app and its stack
$ pulumi destroy && pulumi stack rm -y
```
31 changes: 31 additions & 0 deletions aws-py-stepfunctions/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2016-2018, Pulumi Corporation. All rights reserved.

import iam
import pulumi
from pulumi_aws import lambda_, sfn

hello_world_fn = lambda_.Function('helloWorldFunction',
role=iam.lambda_role.arn,
runtime="python2.7",
handler="hello_step.hello",
code=pulumi.AssetArchive({
'.': pulumi.FileArchive('./step_hello')
})
)

state_defn = state_machine = sfn.StateMachine('stateMachine',
role_arn=iam.sfn_role.arn,
definition="""{
"Comment": "A Hello World example of the Amazon States Language using an AWS Lambda Function",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "%s",
"End": true
}
}
}""" % hello_world_fn.arn
)

pulumi.output('state_machine_arn', state_machine.id)
66 changes: 66 additions & 0 deletions aws-py-stepfunctions/iam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2016-2018, Pulumi Corporation. All rights reserved.

from pulumi_aws import config, iam

lambda_role = iam.Role('lambdaRole',
assume_role_policy="""{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}"""
)

lambda_role_policy = iam.RolePolicy('lambdaRolePolicy',
role=lambda_role.id,
policy="""{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}]
}"""
)

sfn_role = iam.Role('sfnRole',
assume_role_policy="""{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "states.%s.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}""" % config.region
)

sfn_role_policy = iam.RolePolicy('sfnRolePolicy',
role=sfn_role.id,
policy="""{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": "*"
}
]
}"""
)
2 changes: 2 additions & 0 deletions aws-py-stepfunctions/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pulumi>=0.14.0
pulumi-aws>=0.14.3
2 changes: 2 additions & 0 deletions aws-py-stepfunctions/step_hello/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def handler(event, context):
return "Hello world!"

0 comments on commit a295ef5

Please sign in to comment.