Skip to content

Commit

Permalink
Add AWS-native stepfunction example (pulumi#1087)
Browse files Browse the repository at this point in the history
Co-authored-by: Levi Blackstone <[email protected]>
  • Loading branch information
Vivek Lakshmanan and lblackstone committed Sep 30, 2021
1 parent 0255b8d commit a024097
Show file tree
Hide file tree
Showing 6 changed files with 228 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
93 changes: 93 additions & 0 deletions aws-native-ts-stepfunctions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
[![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.

Note: Some resources are not yet supported by the Native AWS provider, so we are using both the Native
and Classic provider in this example. The resources will be updated to use native resources as they are
available in AWS's Cloud Control API.

## Known error

On the first update, you may see the following error message:

```
error: operation CREATE failed with "InvalidRequest": The role defined for the function cannot be assumed by Lambda. (Service: Lambda, Status Code: 400, Request ID: c33fdd39-59d4-4ba8-8ad6-29f6c04d79eb, Extended Request ID: null)
```

Re-running the update should succeed. This issue is tracked [here](https://github.com/pulumi/pulumi-aws-native/issues/148)

## Deploying and running the program

Note: some values in this example will be different from run to run. These values are indicated
with `***`.

1. Create a new stack:

```bash
$ pulumi stack init dev
```

1. Set the AWS region:

Either using an environment variable
```bash
$ export AWS_REGION=us-west-2
```

Or with the stack config
```bash
$ pulumi config set aws:region us-west-2
$ pulumi config set aws-native:region us-west-2
```

1. Restore NPM modules via `npm install` or `yarn install`.

1. Run `pulumi up` to preview and deploy changes. After the preview is shown you will be
prompted if you want to continue or not.

```bash
$ pulumi up
Previewing update (dev)
...

Updating (dev)

View Live: https://app.pulumi.com/***/stepfunctions/dev/updates/1

Type Name Status
+ pulumi:pulumi:Stack stepfunctions-dev created
+ ├─ aws:iam:Role sfnRole created
+ ├─ aws:iam:Role lambdaRole created
+ ├─ aws:iam:RolePolicy sfnRolePolicy created
+ ├─ aws:iam:RolePolicy lambdaRolePolicy created
+ ├─ aws-native:lambda:Function helloFunction created
+ ├─ aws-native:lambda:Function worldFunction created
+ └─ aws-native:stepfunctions:StateMachine stateMachine created

Outputs:
+ stateMachineArn: "arn:aws:states:us-west-2:***:stateMachine:***"

Resources:
+ 10 created

Duration: ***
```

1. To see the resources that were created, run `pulumi stack output`:

```bash
$ pulumi stack output
Current stack outputs (1):
OUTPUT VALUE
stateMachineArn arn:aws:states:us-west-2:***:stateMachine:***
```

1. Start execution using the AWS CLI (or from the console at https://console.aws.amazon.com/states)

```bash
$ aws stepfunctions start-sync-execution --state-machine-arn $(pulumi stack output stateMachineArn)
```

1. To clean up resources, run `pulumi destroy` and answer the confirmation question at the prompt.
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 2016-2021, Pulumi Corporation.

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 a024097

Please sign in to comment.