Skip to content

Commit af5f8bc

Browse files
initial commit
0 parents  commit af5f8bc

File tree

6 files changed

+194
-0
lines changed

6 files changed

+194
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
bin

Gopkg.lock

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[prune]
29+
go-tests = true
30+
unused-packages = true

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build:
2+
GOOS=darwin go build -o ./bin/cloudsteps.Darwin.amd64 ./cmd/cloudsteps
3+
GOOS=linux GOARCH=amd64 go build -o ./bin/cloudsteps.Linux.amd64 ./cmd/cloudsteps
4+
GOOS=linux GOARCH=386 go build -o ./bin/cloudsteps.Linux.386 ./cmd/cloudsteps
5+
GOOS=linux GOARCH=arm go build -o ./bin/cloudsteps.Linux.armhf ./cmd/cloudsteps

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Cloud Steps
2+
3+
A tool to merge a YAML step function definition in with a CloudFormation template file.
4+
5+
## Usage
6+
7+
You have this at `steps.yml`:
8+
9+
```yml
10+
StateMachineName: HelloWorld-StateMachine
11+
DefinitionString:
12+
StartAt: HelloWorld
13+
States:
14+
HelloWorld:
15+
Type: Task
16+
Resource: "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:HelloFunction"
17+
End: true
18+
RoleArn: !Sub "arn:aws:iam::${AWS::AccountId}:role/service-role/StatesExecutionRole-${AWS::Region}"
19+
```
20+
21+
But you want to put it in your cloud formation template but you [came across this problem](https://stackoverflow.com/questions/51627531/deploy-stepfunctions-with-cloudformation-from-external-definition-file). Run the command like so:
22+
23+
cloudsteps -t cftemplate.yml -in steps.yml -t
24+
25+
And it will be added to the resources in the template but embedded as JSON:
26+
27+
```yml
28+
AWSTemplateFormatVersion: '2010-09-09'
29+
Description: An example template for a Step Functions state machine.
30+
Resources:
31+
HelloWorld-StateMachine:
32+
Type: AWS::StepFunctions::StateMachine
33+
Properties:
34+
StateMachineName: HelloWorld-StateMachine
35+
DefinitionString:
36+
Fn::Sub: '{"HelloWorld":{"End":true,"Resource":"arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:HelloFunction","Type":"Task"},"StartAt":"HelloWorld","States":null}'
37+
RoleArn: !Sub arn:aws:iam::${AWS::AccountId}:role/service-role/StatesExecutionRole-${AWS::Region}
38+
```

cmd/cloudsteps/main.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
9+
"github.com/Jeffail/gabs"
10+
"github.com/ghodss/yaml"
11+
)
12+
13+
func main() {
14+
var write bool
15+
var tmplFile, in string
16+
17+
flag.BoolVar(&write, "w", false, "write the output to the template file")
18+
flag.StringVar(&tmplFile, "t", "", "the path to the template file")
19+
flag.StringVar(&in, "in", "", "file containing the step function definition")
20+
flag.Parse()
21+
22+
if tmplFile == "" {
23+
panic("no template file specified")
24+
}
25+
26+
if in == "" {
27+
panic("no step functions file specified")
28+
}
29+
30+
data, err := ioutil.ReadFile(tmplFile)
31+
if err != nil {
32+
panic(err)
33+
}
34+
35+
var x interface{}
36+
if err = yaml.Unmarshal(data, &x); err != nil {
37+
panic(err)
38+
}
39+
40+
tmpl, err := gabs.Consume(x)
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
data, err = ioutil.ReadFile(in)
46+
if err != nil {
47+
panic(err)
48+
}
49+
50+
if err = yaml.Unmarshal(data, &x); err != nil {
51+
panic(err)
52+
}
53+
54+
sfn, err := gabs.Consume(x)
55+
if err != nil {
56+
panic(err)
57+
}
58+
59+
res := gabs.New()
60+
jsonDefintion := sfn.Path("DefinitionString").String()
61+
name, _ := sfn.Path("StateMachineName").Data().(string)
62+
63+
res.Set("AWS::StepFunctions::StateMachine", "Type")
64+
res.Set(sfn.Data(), "Properties")
65+
res.Delete("Properties", "DefinitionString")
66+
res.Set(jsonDefintion, "Properties", "DefinitionString", "Fn::Sub")
67+
68+
tmpl.Set(res.Data(), "Resources", name)
69+
70+
data, err = yaml.Marshal(tmpl.Data())
71+
if err != nil {
72+
panic(err)
73+
}
74+
75+
if !write {
76+
fmt.Println(string(data))
77+
os.Exit(0)
78+
}
79+
80+
if err := ioutil.WriteFile(tmplFile, data, 0644); err != nil {
81+
panic(err)
82+
}
83+
}

0 commit comments

Comments
 (0)