Skip to content

Conversation

@bucha09
Copy link
Contributor

@bucha09 bucha09 commented Mar 2, 2020

Possible fix for this issue #306 It allows to add already created role to CloudWatch event

Possible fix for this issue serverless-operations#306 It allows to add already created role to CloudWatch event
@theburningmonk
Copy link
Collaborator

@bucha09 LGTM and should work, can you add some tests around it?

Taras Buchko added 2 commits March 11, 2020 20:58
It allows to use already created role in CloudWatch event which trigger StepFunctions
@bucha09
Copy link
Contributor Author

bucha09 commented Mar 11, 2020

@bucha09 LGTM and should work, can you add some tests around it?

Made some research and add some fixes to code. It'll use iamRole when it defined in serverless.yml or if not defined, then leave as it was (create new role).
example:

Made some research and add some fixes to code. It'll use iamRole when it defined in serverless.yml or if not defined, then leave as it was (create new role).
example:

cloudwatchEvent:

    name: SwapApproveOrReject-${opt:stage}
        #iamRole: "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
        event:
          source:
            - "aws.codepipeline"

cloudformation template

  "State": "ENABLED",

    "Name": "SwapApproveOrReject-test",
    "Targets": [
      {
        "Arn": {
          "Ref": "DeleteOldDeploymentDashtest"
        },
        "Id": "deleteOldDeploymentCloudWatchEvent",
        "RoleArn": "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
      }
    ]

and if iamRole is defined:

  • cloudwatchEvent:

        name: SwapApproveOrReject-${opt:stage}
        iamRole: "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
        event:
          source:
    

then cloudformation template:

 "State": "ENABLED",

    "Name": "SwapApproveOrReject-test",
    "Targets": [
      {
        "Arn": {
          "Ref": "DeleteOldDeploymentDashtest"
        },
        "Id": "deleteOldDeploymentCloudWatchEvent",
        "RoleArn": {
          "Fn::GetAtt": [
            "DeleteOldDeploymentEventToStepFunctionsRole",
            "Arn"
          ]
        }
      }
    ]
  }
}

as result:
Screenshot from 2020-03-11 20-48-32

@bucha09
Copy link
Contributor Author

bucha09 commented Mar 12, 2020

@theburningmonk Finally, I've fixed issues and add description in readme.md, fixed lint problems. Tested, and looks like everything is ok

@theburningmonk
Copy link
Collaborator

@bucha09 I don't understand your example.. are the cloudformation templates pasted the wrong way round?

"Arn": { "Ref": "${stateMachineLogicalId}" },
"Id": "${cloudWatchId}",
"RoleArn": {
${IamRole ? `"RoleArn":"${IamRole}"` : `"RoleArn": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels a bit weird, having the entire blob inside an if/else, can you do the if-else on ln47 when you assign the value of IamRole instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theburningmonk I'll do my best)

@bucha09
Copy link
Contributor Author

bucha09 commented Mar 12, 2020

@theburningmonk Now, cloudformation paste correct RoleArn if Role is defined in serverless.yml. If Role isn't defined in serverless.yml then leave as it was (it'll create new role)

@theburningmonk
Copy link
Collaborator

@bucha09 sorry, I meant the example you cited here. When you set the IAM role explicitly

name: SwapApproveOrReject-${opt:stage}
    iamRole: "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"

I'd have expected the CloudFormation template to reflect that:

    "State": "ENABLED",
    "Name": "SwapApproveOrReject-test",
    "Targets": [
      {
        "Arn": {
          "Ref": "DeleteOldDeploymentDashtest"
        },
        "Id": "deleteOldDeploymentCloudWatchEvent",
        "RoleArn": "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
      }
    ]

is that not the case?

@bucha09 LGTM and should work, can you add some tests around it?

Made some research and add some fixes to code. It'll use iamRole when it defined in serverless.yml or if not defined, then leave as it was (create new role).
example:

Made some research and add some fixes to code. It'll use iamRole when it defined in serverless.yml or if not defined, then leave as it was (create new role).
example:

cloudwatchEvent:

    name: SwapApproveOrReject-${opt:stage}
        #iamRole: "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
        event:
          source:
            - "aws.codepipeline"

cloudformation template

  "State": "ENABLED",

    "Name": "SwapApproveOrReject-test",
    "Targets": [
      {
        "Arn": {
          "Ref": "DeleteOldDeploymentDashtest"
        },
        "Id": "deleteOldDeploymentCloudWatchEvent",
        "RoleArn": "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
      }
    ]

and if iamRole is defined:

  • cloudwatchEvent:
        name: SwapApproveOrReject-${opt:stage}
        iamRole: "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
        event:
          source:
    

then cloudformation template:

 "State": "ENABLED",

    "Name": "SwapApproveOrReject-test",
    "Targets": [
      {
        "Arn": {
          "Ref": "DeleteOldDeploymentDashtest"
        },
        "Id": "deleteOldDeploymentCloudWatchEvent",
        "RoleArn": {
          "Fn::GetAtt": [
            "DeleteOldDeploymentEventToStepFunctionsRole",
            "Arn"
          ]
        }
      }
    ]
  }
}

as result:
Screenshot from 2020-03-11 20-48-32

@bucha09
Copy link
Contributor Author

bucha09 commented Mar 13, 2020

@theburningmonk
Sorry, my mistake in description
If cloudwatch event looks like this. Role disabled or isn't defined:

cloudwatchEvent:

    name: SwapApproveOrReject-${opt:stage}
        #iamRole: "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
        event:
          source:
            - "aws.codepipeline"

then cloudformation template:

"State": "ENABLED",

    "Name": "SwapApproveOrReject-test",
    "Targets": [
      {
        "Arn": {
          "Ref": "DeleteOldDeploymentDashtest"
        },
        "Id": "deleteOldDeploymentCloudWatchEvent",
        "RoleArn": {
          "Fn::GetAtt": [
            "DeleteOldDeploymentEventToStepFunctionsRole",
            "Arn"
          ]
        }
      }

and if iamRole is defined:

 cloudwatchEvent:

        name: SwapApproveOrReject-${opt:stage}
        iamRole: "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
        event:
          source:

then cloudformation template

"State": "ENABLED",

    "Name": "SwapApproveOrReject-test",
    "Targets": [
      {
        "Arn": {
          "Ref": "DeleteOldDeploymentDashtest"
        },
        "Id": "deleteOldDeploymentCloudWatchEvent",
        "RoleArn": "arn:aws:iam::accountid:role/Events-InvokeStepFunctions-Role"
      }
    ]

@theburningmonk theburningmonk merged commit 3a57548 into serverless-operations:master Apr 2, 2020
@theburningmonk
Copy link
Collaborator

🎉 This PR is included in version 2.17.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

@bucha09
Copy link
Contributor Author

bucha09 commented Apr 2, 2020

@theburningmonk Thanks!

@bucha09 bucha09 deleted the patch-1 branch April 2, 2020 11:48
ss-betseqnzr pushed a commit to BetSEQNZR/serverless-step-functions that referenced this pull request Sep 8, 2023
Add custom role to cloudwatch event which trigger step functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants