Search code examples
amazon-web-servicesaws-cloudformationaws-event-bridgeaws-cloudformation-custom-resource

CloudFormation error for substitution values


Helo, I am trying to add a trigger to a lambda function as follows. But it is throwing error as follows

 undefined method `match' for {"Ref"=>"EventRuleName"}

I have already defined a lambda and event rule prior to the creating the permissions stack . But while running the permission stack it errored out

cloudformation

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: ""

Parameters:
  LambdaFunctionName:
    Type: String
    Default: lambda-function1
  EventBridgeRuleRoleName:
    Type: String
    Default: EventBridgeRuleRole1
  EventRuleName:
    Type: String
    Default: EventBridgeRule1
  Env:
    Type: String
  AWSTenant:
    Type: String
  TeamTag:
    Type: String
    Default: team1

Resources:
  PermissionForEvent0ToInvokeLambda:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref LambdaFunctionName
      Action: "lambda:InvokeFunction"
      Principal: "events.amazonaws.com"
      SourceArn:
        Fn::GetAtt:
          - !Ref EventRuleName
          - "Arn"

Solution

  • You can't fetch the Arn from the EventRuleName because that is a string parameter.

    It would work if the EventRule is deployed in the same stack. Then you can use !Ref and fetch the Arn of the EventRule.

    If you need to separate the stacks, I would

    • Pass in the EventRule Arn into the stack instead of the EventRule Name, or
    • You could construct the ARN from the name
      PermissionForEvent0ToInvokeLambda:
        Type: AWS::Lambda::Permission
        Properties:
          FunctionName: !Ref LambdaFunctionName
          Action: "lambda:InvokeFunction"
          Principal: "events.amazonaws.com"
          SourceArn:
            Fn::Sub:
              - arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/${EventRuleName}