Skip to content

Commit 767060a

Browse files
committed
iam
1 parent 53482e4 commit 767060a

File tree

1 file changed

+61
-12
lines changed

1 file changed

+61
-12
lines changed

lib/index.js

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,66 @@ class AwsStepFunctionsDeploy {
3131
this.hooks = {
3232
'deploy:stepf:deploy': this.action.bind(this),
3333
};
34+
35+
this.iamPolicyStatement = `{
36+
"Version": "2012-10-17",
37+
"Statement": [
38+
{
39+
"Effect": "Allow",
40+
"Action": [
41+
"lambda:InvokeFunction"
42+
],
43+
"Resource": "*"
44+
}
45+
]
46+
}
47+
`;
48+
this.iamRoleName = 'serverless-step-functions-executerole';
49+
}
50+
51+
action() {
52+
this.serverless.cli.consoleLog('Start Deploy Step Functions');
53+
BbPromise.bind(this)
54+
.then(this.yamlParse)
55+
.then(this.setStateMachineArn)
56+
.then(this.compile)
57+
.then(this.getIamRole)
58+
.then(this.deleteStateMachine)
59+
.then(this.createStateMachine);
60+
}
61+
62+
getIamRole() {
63+
return this.provider.request('IAM',
64+
'getRole',
65+
{
66+
RoleName: 'StatesExecutionRole-us-east-1',
67+
},
68+
this.options.stage,
69+
this.options.region)
70+
.then((result) => {
71+
this.iamRoleArn = result.Role.Arn;
72+
return BbPromise.resolve();
73+
}).catch((error) => {
74+
if (error.statusCode === 404) {
75+
return this.createIamRole();
76+
}
77+
return BbPromise.reject();
78+
});
79+
}
80+
81+
createIamRole() {
82+
return this.provider.request('IAM',
83+
'createRole',
84+
{
85+
AssumeRolePolicyDocument: this.iamPolicyStatement,
86+
RoleName: this.iamRoleName,
87+
},
88+
this.options.stage,
89+
this.options.region)
90+
.then((result) => {
91+
this.iamRoleArn = result.Role.Arn;
92+
return BbPromise.resolve();
93+
});
3494
}
3595

3696
setStateMachineArn() {
@@ -47,16 +107,6 @@ class AwsStepFunctionsDeploy {
47107
});
48108
}
49109

50-
action() {
51-
this.serverless.cli.consoleLog('Start Deploy Step Functions');
52-
BbPromise.bind(this)
53-
.then(this.yamlParse)
54-
.then(this.setStateMachineArn)
55-
.then(this.compile)
56-
.then(this.deleteStateMachine)
57-
.then(this.createStateMachine);
58-
}
59-
60110
yamlParse() {
61111
const servicePath = this.serverless.config.servicePath;
62112

@@ -90,7 +140,6 @@ class AwsStepFunctionsDeploy {
90140
throw new this.serverless.classes.Error(errorMessage);
91141
}
92142

93-
// @todo get lambda arn from functionname
94143
this.awsStateLanguage[this.options.statemachine] =
95144
JSON.stringify(this.stepFunctions[this.options.statemachine]);
96145
return BbPromise.resolve();
@@ -113,7 +162,7 @@ class AwsStepFunctionsDeploy {
113162
{
114163
definition: this.awsStateLanguage[this.options.statemachine],
115164
name: this.options.statemachine,
116-
roleArn: '',
165+
roleArn: this.iamRoleArn,
117166
},
118167
this.options.stage,
119168
this.options.region)

0 commit comments

Comments
 (0)