Skip to content

Commit 60abf53

Browse files
committed
update iam role implementaion
1 parent 96795d8 commit 60abf53

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

lib/deploy.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
deploy() {
88
this.awsStateLanguage = {};
99
this.functionArns = {};
10+
const region = this.options.region || 'us-east-1';
1011

1112
this.iamPolicyStatement = `{
1213
"Version": "2012-10-17",
@@ -22,6 +23,23 @@ module.exports = {
2223
}
2324
`;
2425

26+
this.assumeRolePolicyDocument = `{
27+
"Version": "2012-10-17",
28+
"Statement": [
29+
{
30+
"Effect": "Allow",
31+
"Principal": {
32+
"Service": "states.${region}.amazonaws.com"
33+
},
34+
"Action": "sts:AssumeRole"
35+
}
36+
]
37+
}
38+
`;
39+
40+
this.iamRoleName = `serverless-step-functions-executerole-${region}`;
41+
this.iamPolicyName = `serverless-step-functions-executepolicy-${region}`;
42+
2543
this.serverless.cli.log(`Start to deploy ${this.options.state} step function...`);
2644
BbPromise.bind(this)
2745
.then(this.yamlParse)
@@ -39,7 +57,7 @@ module.exports = {
3957
return this.provider.request('IAM',
4058
'getRole',
4159
{
42-
RoleName: 'StatesExecutionRole-us-east-1',
60+
RoleName: this.iamRoleName,
4361
},
4462
this.options.stage,
4563
this.options.region)
@@ -74,15 +92,33 @@ module.exports = {
7492
return this.provider.request('IAM',
7593
'createRole',
7694
{
77-
AssumeRolePolicyDocument: this.iamPolicyStatement,
95+
AssumeRolePolicyDocument: this.assumeRolePolicyDocument,
7896
RoleName: this.iamRoleName,
7997
},
8098
this.options.stage,
8199
this.options.region)
82100
.then((result) => {
83101
this.iamRoleArn = result.Role.Arn;
84-
return BbPromise.resolve();
85-
});
102+
return this.provider.request('IAM',
103+
'createPolicy',
104+
{
105+
PolicyDocument: this.iamPolicyStatement,
106+
PolicyName: this.iamPolicyName,
107+
},
108+
this.options.stage,
109+
this.options.region)
110+
})
111+
.then((result) => {
112+
return this.provider.request('IAM',
113+
'attachRolePolicy',
114+
{
115+
PolicyArn: result.Policy.Arn,
116+
RoleName: this.iamRoleName,
117+
},
118+
this.options.stage,
119+
this.options.region)
120+
})
121+
.then(() => BbPromise.resolve());
86122
},
87123

88124
getStateMachineArn() {

0 commit comments

Comments
 (0)