Skip to content

Commit

Permalink
Add stepfunction example
Browse files Browse the repository at this point in the history
  • Loading branch information
viveklak committed Sep 30, 2021
1 parent b14a70d commit 5c2cea3
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws-native-ts-stepfunctions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/bin/
/node_modules/
/.pulumi/
3 changes: 3 additions & 0 deletions aws-native-ts-stepfunctions/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: stepfunctions
description: Basic example of AWS Step Functions with AWS Native
runtime: nodejs
23 changes: 23 additions & 0 deletions aws-native-ts-stepfunctions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# AWS Step Functions

A basic example that demonstrates using AWS Step Functions with a Lambda function using the AWS Native provider.

```
# Create and configure a new stack
$ pulumi stack init stepfunctions-dev
$ export AWS_REGION=us-east-2
# Install dependencies
$ npm install
# Preview and run the deployment
$ pulumi up
# Start execution using the AWS CLI (or from the console at https://console.aws.amazon.com/states)
$ aws stepfunctions start-sync-execution --state-machine-arn $(pulumi stack output stateMachineArn)
# Remove the app
$ pulumi destroy
```
99 changes: 99 additions & 0 deletions aws-native-ts-stepfunctions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2021, Pulumi Corporation. All rights reserved.

import * as aws from "@pulumi/aws";
import * as awsnative from "@pulumi/aws-native";
import * as pulumi from "@pulumi/pulumi";

const region = aws.config.requireRegion();

const lambdaRole = new aws.iam.Role("lambdaRole", {
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({ Service: "lambda.amazonaws.com" }),
});

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

const helloFunction = new awsnative.lambda.Function('helloFunction',
{
role: lambdaRole.arn,
runtime: "nodejs14.x",
handler: "index.handler",
code: {
zipFile: `exports.handler = function(event, context, callback){
const response = {"response": "Hello "};
callback(null, response);
};`,
},
}, { dependsOn: lambdaRolePolicy });

const worldFunction = new awsnative.lambda.Function('worldFunction',
{
role: lambdaRole.arn,
runtime: "nodejs14.x",
handler: "index.handler",
code: {
zipFile: `exports.handler = function(event, context, callback){
var response = event.response;
const updated = { "response": response + "World!" };
callback(null, updated);
};`,
},
}, {dependsOn: lambdaRolePolicy});


const sfnRole = new aws.iam.Role("sfnRole", {
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({ Service: `states.${region}.amazonaws.com` }),
});

const sfnRolePolicy = new aws.iam.RolePolicy("sfnRolePolicy", {
role: sfnRole.id,
policy: {
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Action: [
"lambda:InvokeFunction",
],
Resource: "*",
}],
},
});

const stateMachine = new awsnative.stepfunctions.StateMachine("stateMachine", {
roleArn: sfnRole.arn,
stateMachineType: 'EXPRESS',
definitionString: pulumi.all([helloFunction.arn, worldFunction.arn])
.apply(([helloArn, worldArn]) => {
return JSON.stringify({
"Comment": "A Hello World example of the Amazon States Language using two AWS Lambda Functions",
"StartAt": "Hello",
"States": {
"Hello": {
"Type": "Task",
"Resource": helloArn,
"Next": "World",
},
"World": {
"Type": "Task",
"Resource": worldArn,
"End": true,
},
},
});
}),
}, { dependsOn: sfnRolePolicy });

export const stateMachineArn = stateMachine.id;
11 changes: 11 additions & 0 deletions aws-native-ts-stepfunctions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "stepfunctions",
"devDependencies": {
"@types/node": "^8.0.0"
},
"dependencies": {
"@pulumi/aws": "^4.0.0",
"@pulumi/pulumi": "^3.0.0",
"@pulumi/aws-native": "^0.1.0"
}
}
19 changes: 19 additions & 0 deletions aws-native-ts-stepfunctions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true
},
"files": [
"index.ts"
]
}

0 comments on commit 5c2cea3

Please sign in to comment.