Skip to content

Commit f9e913f

Browse files
Revert "Merge pull request #190 from horike37/feature/intrinsic_functions"
This reverts commit bcdd812, reversing changes made to 2b006d6.
1 parent bcdd812 commit f9e913f

File tree

6 files changed

+20
-302
lines changed

6 files changed

+20
-302
lines changed

README.md

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ plugins:
1515
```
1616
1717
## Setup
18-
Specifies your statemachine definition using Amazon States Language in a `definition` statement in serverless.yml. You can use CloudFormation intrinsic functions such as `Ref` and `Fn::GetAtt` to reference Lambda functions, SNS topics, SQS queues and DynamoDB tables declared in the same `serverless.yml`.
19-
20-
Alternatively, you can also provide the raw ARN, or SQS queue URL, or DynamoDB table name as a string. If you need to construct the ARN by hand, then we recommend to use the [serverless-pseudo-parameters](https://www.npmjs.com/package/serverless-pseudo-parameters) plugin together to make it easier.
18+
Specifies your statemachine definition using Amazon States Language in a `definition` statement in serverless.yml.
19+
We recommend to use [serverless-pseudo-parameters](https://www.npmjs.com/package/serverless-pseudo-parameters) plugin together so that it makes it easy to set up `Resource` section under `definition`.
2120

2221
```yml
2322
functions:
24-
hello:
23+
hellofunc:
2524
handler: handler.hello
2625
2726
stepFunctions:
@@ -46,8 +45,7 @@ stepFunctions:
4645
States:
4746
HelloWorld1:
4847
Type: Task
49-
Resource:
50-
Ref: HelloLambdaFunction
48+
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello
5149
End: true
5250
dependsOn: CustomIamRole
5351
tags:
@@ -70,8 +68,7 @@ stepFunctions:
7068
States:
7169
HelloWorld2:
7270
Type: Task
73-
Resource:
74-
Ref: HelloLambdaFunction
71+
Resource: arn:aws:states:#{AWS::Region}:#{AWS::AccountId}:activity:myTask
7572
End: true
7673
dependsOn:
7774
- DynamoDBTable
@@ -518,8 +515,7 @@ functions:
518515
States:
519516
HelloWorld1:
520517
Type: Task
521-
Resource:
522-
Ref: HelloLambdaFunction
518+
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello
523519
End: true
524520
525521
@@ -828,8 +824,7 @@ stepFunctions:
828824
States:
829825
FirstState:
830826
Type: Task
831-
Resource:
832-
Ref: HelloLambdaFunction
827+
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello
833828
Next: wait_using_seconds
834829
wait_using_seconds:
835830
Type: Wait
@@ -849,8 +844,7 @@ stepFunctions:
849844
Next: FinalState
850845
FinalState:
851846
Type: Task
852-
Resource:
853-
Ref: HelloLambdaFunction
847+
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello
854848
End: true
855849
plugins:
856850
- serverless-step-functions
@@ -872,8 +866,7 @@ stepFunctions:
872866
States:
873867
HelloWorld:
874868
Type: Task
875-
Resource:
876-
Ref: HelloLambdaFunction
869+
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello
877870
Retry:
878871
- ErrorEquals:
879872
- HandledError
@@ -953,8 +946,7 @@ stepFunctions:
953946
States:
954947
HelloWorld:
955948
Type: Task
956-
Resource:
957-
Ref: HelloLambdaFunction
949+
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello
958950
Catch:
959951
- ErrorEquals: ["HandledError"]
960952
Next: CustomErrorFallback
@@ -1002,8 +994,7 @@ stepFunctions:
1002994
States:
1003995
FirstState:
1004996
Type: Task
1005-
Resource:
1006-
Ref: Hello1LambdaFunction
997+
Resource: arn:aws:lambda:${opt:region}:${self:custom.accountId}:function:${self:service}-${opt:stage}-hello1
1007998
Next: ChoiceState
1008999
ChoiceState:
10091000
Type: Choice
@@ -1017,21 +1008,18 @@ stepFunctions:
10171008
Default: DefaultState
10181009
FirstMatchState:
10191010
Type: Task
1020-
Resource:
1021-
Ref: Hello2LambdaFunction
1011+
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello2
10221012
Next: NextState
10231013
SecondMatchState:
10241014
Type: Task
1025-
Resource:
1026-
Ref: Hello3LambdaFunction
1015+
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello3
10271016
Next: NextState
10281017
DefaultState:
10291018
Type: Fail
10301019
Cause: "No Matches!"
10311020
NextState:
10321021
Type: Task
1033-
Resource:
1034-
Ref: Hello4LambdaFunction
1022+
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello4
10351023
End: true
10361024
plugins:
10371025
- serverless-step-functions

lib/deploy/stepFunctions/compileIamRole.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
const _ = require('lodash');
33
const BbPromise = require('bluebird');
44
const path = require('path');
5-
const { isIntrinsic } = require('../../utils/aws');
65

76
function getTaskStates(states) {
87
return _.flatMap(states, state => {
@@ -29,12 +28,6 @@ function sqsQueueUrlToArn(serverless, queueUrl) {
2928
const accountId = match[2];
3029
const queueName = match[3];
3130
return `arn:aws:sqs:${region}:${accountId}:${queueName}`;
32-
} else if (isIntrinsic(queueUrl) && queueUrl.Ref) {
33-
// most likely we'll see a { Ref: LogicalId }, which we need to map to
34-
// { Fn::GetAtt: [ LogicalId, Arn ] } to get the ARN
35-
return {
36-
'Fn::GetAtt': [queueUrl.Ref, 'Arn'],
37-
};
3831
}
3932
serverless.cli.consoleLog(`Unable to parse SQS queue url [${queueUrl}]`);
4033
return [];
@@ -65,14 +58,6 @@ function getSnsPermissions(serverless, state) {
6558
}
6659

6760
function getDynamoDBArn(tableName) {
68-
if (isIntrinsic(tableName) && tableName.Ref) {
69-
// most likely we'll see a { Ref: LogicalId }, which we need to map to
70-
// { Fn::GetAtt: [ LogicalId, Arn ] } to get the ARN
71-
return {
72-
'Fn::GetAtt': [tableName.Ref, 'Arn'],
73-
};
74-
}
75-
7661
return {
7762
'Fn::Join': [
7863
':',
@@ -212,7 +197,7 @@ function getIamPermissions(serverless, taskStates) {
212197
return getEcsPermissions();
213198

214199
default:
215-
if (isIntrinsic(state.Resource) || state.Resource.startsWith('arn:aws:lambda')) {
200+
if (state.Resource.startsWith('arn:aws:lambda')) {
216201
return [{
217202
action: 'lambda:InvokeFunction',
218203
resource: state.Resource,

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -900,103 +900,4 @@ describe('#compileIamRole', () => {
900900
.Properties.Policies[0];
901901
expectDenyAllPolicy(policy);
902902
});
903-
904-
it('should respect CloudFormation intrinsic functions for Resource', () => {
905-
serverless.service.stepFunctions = {
906-
stateMachines: {
907-
myStateMachine: {
908-
name: 'stateMachine',
909-
definition: {
910-
StartAt: 'Lambda',
911-
States: {
912-
Lambda: {
913-
Type: 'Task',
914-
Resource: {
915-
Ref: 'MyFunction',
916-
},
917-
Next: 'Sns',
918-
},
919-
Sns: {
920-
Type: 'Task',
921-
Resource: 'arn:aws:states:::sns:publish',
922-
Parameters: {
923-
Message: {
924-
'Fn::GetAtt': ['MyTopic', 'TopicName'],
925-
},
926-
TopicArn: {
927-
Ref: 'MyTopic',
928-
},
929-
},
930-
Next: 'Sqs',
931-
},
932-
Sqs: {
933-
Type: 'Task',
934-
Resource: 'arn:aws:states:::sqs:sendMessage',
935-
Parameters: {
936-
QueueUrl: {
937-
Ref: 'MyQueue',
938-
},
939-
MessageBody: 'This is a static message',
940-
},
941-
Next: 'DynamoDB',
942-
},
943-
DynamoDB: {
944-
Type: 'Task',
945-
Resource: 'arn:aws:states:::dynamodb:putItem',
946-
Parameters: {
947-
TableName: {
948-
Ref: 'MyTable',
949-
},
950-
},
951-
Next: 'Parallel',
952-
},
953-
Parallel: {
954-
Type: 'Parallel',
955-
End: true,
956-
Branches: [
957-
{
958-
StartAt: 'Lambda2',
959-
States: {
960-
Lambda2: {
961-
Type: 'Task',
962-
Resource: {
963-
Ref: 'MyFunction2',
964-
},
965-
End: true,
966-
},
967-
},
968-
},
969-
],
970-
},
971-
},
972-
},
973-
},
974-
},
975-
};
976-
977-
serverlessStepFunctions.compileIamRole();
978-
serverlessStepFunctions.compileStateMachines();
979-
const policy = serverlessStepFunctions.serverless.service
980-
.provider.compiledCloudFormationTemplate.Resources.IamRoleStateMachineExecution
981-
.Properties.Policies[0];
982-
983-
const statements = policy.PolicyDocument.Statement;
984-
985-
const lambdaPermissions = statements.find(x => x.Action[0] === 'lambda:InvokeFunction');
986-
expect(lambdaPermissions.Resource).to.be.deep.equal([
987-
{ Ref: 'MyFunction' }, { Ref: 'MyFunction2' }]);
988-
989-
const snsPermissions = statements.find(x => x.Action[0] === 'sns:Publish');
990-
expect(snsPermissions.Resource).to.be.deep.equal([{ Ref: 'MyTopic' }]);
991-
992-
const sqsPermissions = statements.find(x => x.Action[0] === 'sqs:SendMessage');
993-
expect(sqsPermissions.Resource).to.be.deep.equal([{
994-
'Fn::GetAtt': ['MyQueue', 'Arn'],
995-
}]);
996-
997-
const dynamodbPermissions = statements.find(x => x.Action[0] === 'dynamodb:PutItem');
998-
expect(dynamodbPermissions.Resource).to.be.deep.equal([{
999-
'Fn::GetAtt': ['MyTable', 'Arn'],
1000-
}]);
1001-
});
1002903
});

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
'use strict';
22
const _ = require('lodash');
33
const BbPromise = require('bluebird');
4-
const { isIntrinsic } = require('../../utils/aws');
54

6-
function randomName() {
7-
const chars = 'abcdefghijklmnopqrstufwxyzABCDEFGHIJKLMNOPQRSTUFWXYZ1234567890';
8-
const pwd = _.sampleSize(chars, 10);
9-
return pwd.join('');
5+
function isIntrinsic(obj) {
6+
const isObject = typeof obj === 'object';
7+
return isObject && Object.keys(obj).some((k) => k.startsWith('Fn::') || k.startsWith('Ref'));
108
}
119

1210
function toTags(obj, serverless) {
@@ -28,40 +26,8 @@ function toTags(obj, serverless) {
2826
return tags;
2927
}
3028

31-
// return an iterable of
32-
// [ ParamName, IntrinsicFunction ]
33-
// e.g. [ 'mptFnX05Fb', { Ref: 'MyTopic' } ]
34-
// this makes it easy to use _.fromPairs to construct an object afterwards
35-
function* getIntrinsicFunctions(obj) {
36-
// eslint-disable-next-line no-restricted-syntax
37-
for (const key in obj) {
38-
if (Object.prototype.hasOwnProperty.call(obj, key)) {
39-
const value = obj[key];
40-
41-
if (Array.isArray(value)) {
42-
// eslint-disable-next-line guard-for-in, no-restricted-syntax
43-
for (const idx in value) {
44-
const innerFuncs = Array.from(getIntrinsicFunctions(value[idx]));
45-
for (const x of innerFuncs) {
46-
yield x;
47-
}
48-
}
49-
} else if (isIntrinsic(value)) {
50-
const paramName = randomName();
51-
// eslint-disable-next-line no-param-reassign
52-
obj[key] = `\${${paramName}}`;
53-
yield [paramName, value];
54-
} else if (typeof value === 'object') {
55-
const innerFuncs = Array.from(getIntrinsicFunctions(value));
56-
for (const x of innerFuncs) {
57-
yield x;
58-
}
59-
}
60-
}
61-
}
62-
}
63-
6429
module.exports = {
30+
isIntrinsic,
6531
compileStateMachines() {
6632
if (this.isStateMachines()) {
6733
this.getAllStateMachines().forEach((stateMachineName) => {
@@ -76,17 +42,7 @@ module.exports = {
7642
DefinitionString = JSON.stringify(stateMachineObj.definition)
7743
.replace(/\\n|\\r|\\n\\r/g, '');
7844
} else {
79-
const functionMappings = Array.from(getIntrinsicFunctions(stateMachineObj.definition));
80-
if (_.isEmpty(functionMappings)) {
81-
DefinitionString = JSON.stringify(stateMachineObj.definition, undefined, 2);
82-
} else {
83-
DefinitionString = {
84-
'Fn::Sub': [
85-
JSON.stringify(stateMachineObj.definition, undefined, 2),
86-
_.fromPairs(functionMappings),
87-
],
88-
};
89-
}
45+
DefinitionString = JSON.stringify(stateMachineObj.definition, undefined, 2);
9046
}
9147
} else {
9248
const errorMessage = [

0 commit comments

Comments
 (0)