Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
099b36c
[WIP] Create SAM Statemachine without Policy
ConnorRobertson Nov 14, 2022
7f95a13
Changed basic_state_machine_inline_definition test to
ConnorRobertson Nov 15, 2022
5c98256
Made generated policy more limited.
ConnorRobertson Nov 15, 2022
003a25e
Changed the error message and policy used to a
ConnorRobertson Nov 16, 2022
ba6d5e5
Created integration tests and fixed error message
ConnorRobertson Nov 17, 2022
78f4717
Making the generated role use an empty policy.
ConnorRobertson Nov 17, 2022
75c48a1
Merge branch 'develop' into WIP-Create-SAM-Statemachine-without-Policy
Nov 17, 2022
787e2b8
Added changes requested by Slava and cleaned up
ConnorRobertson Nov 18, 2022
5184d04
Merge branch 'develop' into WIP-Create-SAM-Statemachine-without-Policy
Nov 18, 2022
9b78d94
Ran make pr
ConnorRobertson Nov 18, 2022
19bd43d
Merge branch 'WIP-Create-SAM-Statemachine-without-Policy' of https://…
ConnorRobertson Nov 18, 2022
dbd12ad
Ran make pr
ConnorRobertson Nov 18, 2022
858739e
Merge branch 'develop' into WIP-Create-SAM-Statemachine-without-Policy
Nov 18, 2022
8ee6d07
Transform Test
ConnorRobertson Nov 21, 2022
fad3d50
Merge branch 'develop' into WIP-Create-SAM-Statemachine-without-Policy
Nov 21, 2022
677fcef
Merge branch 'develop' into WIP-Create-SAM-Statemachine-without-Policy
hoffa Nov 21, 2022
65843c4
Merge branch 'develop' into WIP-Create-SAM-Statemachine-without-Policy
Nov 21, 2022
d0091f8
Merge branch 'develop' into WIP-Create-SAM-Statemachine-without-Policy
hoffa Nov 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration/combination/test_connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def test_connector_by_invoking_a_function(self, template_file_path):

@parameterized.expand(
[
("combination/connector_sfn_to_function_without_policy",),
("combination/connector_sfn_to_table_read",),
("combination/connector_sfn_to_table_write",),
("combination/connector_sfn_to_sqs_write",),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"LogicalResourceId": "TriggerStateMachineRole",
"ResourceType": "AWS::IAM::Role"
},
{
"LogicalResourceId": "MyFunctionRole",
"ResourceType": "AWS::IAM::Role"
},
{
"LogicalResourceId": "TriggerStateMachine",
"ResourceType": "AWS::StepFunctions::StateMachine"
},
{
"LogicalResourceId": "MyFunction",
"ResourceType": "AWS::Lambda::Function"
},
{
"LogicalResourceId": "MyConnectorPolicy",
"ResourceType": "AWS::IAM::ManagedPolicy"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Resources:
TriggerStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Type: EXPRESS
Definition:
StartAt: TryDoSomething
States:
TryDoSomething:
Type: Task
Resource: !Sub arn:${AWS::Partition}:states:::lambda:invoke
Parameters:
FunctionName: !Ref MyFunction
End: true

MyFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs14.x
Handler: index.handler
InlineCode: |
exports.handler = async (event) => {
console.log(JSON.stringify(event));
};
MyConnector:
Type: AWS::Serverless::Connector
Properties:
Source:
Id: TriggerStateMachine
Destination:
Id: MyFunction
Permissions:
- Write
Metadata:
SamTransformTest: true
16 changes: 8 additions & 8 deletions samtranslator/model/stepfunctions/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class StateMachineGenerator(object):
_SAM_VALUE = "SAM"
_SUBSTITUTION_NAME_TEMPLATE = "definition_substitution_%s"
_SUBSTITUTION_KEY_TEMPLATE = "${definition_substitution_%s}"
SFN_INVALID_PROPERTY_BOTH_ROLE_POLICY = (
"Specify either 'Role' or 'Policies' (but not both at the same time) or neither of them"
)

def __init__( # type: ignore[no-untyped-def]
self,
Expand Down Expand Up @@ -129,20 +132,17 @@ def to_cloudformation(self): # type: ignore[no-untyped-def]
)

if self.role and self.policies:
raise InvalidResourceException(
self.logical_id, "Specify either 'Role' or 'Policies' property and not both."
)
raise InvalidResourceException(self.logical_id, self.SFN_INVALID_PROPERTY_BOTH_ROLE_POLICY)
if self.role:
self.state_machine.RoleArn = self.role
elif self.policies:
if not self.managed_policy_map:
else:
if self.policies and not self.managed_policy_map:
raise Exception("Managed policy map is empty, but should not be.")

if not self.policies:
self.policies = []
execution_role = self._construct_role() # type: ignore[no-untyped-call]
self.state_machine.RoleArn = execution_role.get_runtime_attr("arn")
resources.append(execution_role)
else:
raise InvalidResourceException(self.logical_id, "Either 'Role' or 'Policies' property must be specified.")

self.state_machine.StateMachineName = self.name
self.state_machine.StateMachineType = self.type
Expand Down
11 changes: 4 additions & 7 deletions tests/model/stepfunctions/test_state_machine_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ def test_state_machine_no_role_or_policies(self):
self.kwargs["definition_uri"] = "s3://my-demo-bucket/my_asl_file.asl.json"
self.kwargs["role"] = None
self.kwargs["policies"] = None
with self.assertRaises(InvalidResourceException) as error:
StateMachineGenerator(**self.kwargs).to_cloudformation()
self.assertEqual(
error.exception.message,
"Resource with id [StateMachineId] is invalid. Either 'Role' or 'Policies' property must be specified.",
)
generated_resources = StateMachineGenerator(**self.kwargs).to_cloudformation()
self.assertEqual(generated_resources[1].resource_type, "AWS::IAM::Role")

def test_state_machine_both_role_and_policies(self):
self.kwargs["definition_uri"] = "s3://my-demo-bucket/my_asl_file.asl.json"
Expand All @@ -73,7 +69,8 @@ def test_state_machine_both_role_and_policies(self):
StateMachineGenerator(**self.kwargs).to_cloudformation()
self.assertEqual(
error.exception.message,
"Resource with id [StateMachineId] is invalid. Specify either 'Role' or 'Policies' property and not both.",
"Resource with id [StateMachineId] is invalid. "
+ StateMachineGenerator.SFN_INVALID_PROPERTY_BOTH_ROLE_POLICY,
)

def test_state_machine_invalid_definition_uri_string(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Resources:
TriggerStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Type: EXPRESS
Definition:
StartAt: TryDoSomething
States:
TryDoSomething:
Type: Task
Resource: !Sub arn:${AWS::Partition}:states:::lambda:invoke
Parameters:
FunctionName: !Ref MyFunction
End: true

MyFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs14.x
Handler: index.handler
InlineCode: |
exports.handler = async (event) => {
console.log(JSON.stringify(event));
};
MyConnector:
Type: AWS::Serverless::Connector
Properties:
Source:
Id: TriggerStateMachine
Destination:
Id: MyFunction
Permissions:
- Write
Metadata:
SamTransformTest: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"Metadata": {
"SamTransformTest": true
},
"Resources": {
"MyConnectorPolicy": {
"Metadata": {
"aws:sam:connectors": {
"MyConnector": {
"Destination": {
"Type": "AWS::Serverless::Function"
},
"Source": {
"Type": "AWS::Serverless::StateMachine"
}
}
}
},
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"lambda:InvokeAsync",
"lambda:InvokeFunction"
],
"Effect": "Allow",
"Resource": [
{
"Fn::GetAtt": [
"MyFunction",
"Arn"
]
}
]
}
],
"Version": "2012-10-17"
},
"Roles": [
{
"Ref": "TriggerStateMachineRole"
}
]
},
"Type": "AWS::IAM::ManagedPolicy"
},
"MyFunction": {
"Properties": {
"Code": {
"ZipFile": "exports.handler = async (event) => {\n console.log(JSON.stringify(event));\n};\n"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"MyFunctionRole",
"Arn"
]
},
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::Lambda::Function"
},
"MyFunctionRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
},
"TriggerStateMachine": {
"Properties": {
"DefinitionString": {
"Fn::Join": [
"\n",
[
"{",
" \"StartAt\": \"TryDoSomething\",",
" \"States\": {",
" \"TryDoSomething\": {",
" \"End\": true,",
" \"Parameters\": {",
" \"FunctionName\": \"${definition_substitution_1}\"",
" },",
" \"Resource\": \"${definition_substitution_2}\",",
" \"Type\": \"Task\"",
" }",
" }",
"}"
]
]
},
"DefinitionSubstitutions": {
"definition_substitution_1": {
"Ref": "MyFunction"
},
"definition_substitution_2": {
"Fn::Sub": "arn:${AWS::Partition}:states:::lambda:invoke"
}
},
"RoleArn": {
"Fn::GetAtt": [
"TriggerStateMachineRole",
"Arn"
]
},
"StateMachineType": "EXPRESS",
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::StepFunctions::StateMachine"
},
"TriggerStateMachineRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"states.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [],
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
}
}
}
Loading