title: AWS Lambda Policy Length Exceeded - adding rules to a lambda function
body: <p>I have a website that dynamically creates rules that have cron events attached. All of these rules are associated with and call a single lambda function.</p>
  <p>I am using python and boto3 to generate the rules and apply them to the Lambda Function. (If seeing my python code that generates the rules and events would help, I'd be happy to include it here.)</p>
  <hr />
  <p>This all works, however after using my website and creating about 68 rules I got this error:</p>
  <p><code>PolicyLengthExceededException: An error occurred (PolicyLengthExceededException) when calling the AddPermission operation: The final policy size (20642) is bigger than the limit (20480).</code></p>
  <p>Every-time I create a rule and its event, a permission needs to be added to the lambda's Function Policy, and after about 68 rules, the Function Policy gets too large.</p>
  <p><strong>How can I fix this?</strong></p>
  <hr />
  <h3>Here is an example permission:</h3>
  <pre><code>{
    &quot;Sid&quot;: &quot;&lt;some_random_id_for_this_permission&quot;,
    &quot;Effect&quot;: &quot;Allow&quot;,
    &quot;Principal&quot;: {
      &quot;Service&quot;: &quot;events.amazonaws.com&quot;
    },
    &quot;Action&quot;: &quot;lambda:InvokeFunction&quot;,
    &quot;Resource&quot;: &quot;arn:aws:lambda:&lt;some_arn_id&gt;:function:_MyFunction&quot;,
    &quot;Condition&quot;: {
      &quot;ArnLike&quot;: {
        &quot;AWS:SourceArn&quot;: &quot;arn:aws:events&lt;some_arn_id&gt;:rule/my_rule_1&quot;
      }
    }
  </code></pre>
  <p>The <code>AWS:SourceArn</code> is the unique value of the rule, so I've figured I could use <code>*</code> to give permission to all rules. So I tried putting as the value for the <code>AWS:SourceArn</code>:</p>
  <p><code>arn:aws:events&lt;some_arn_data&gt;:rule/*</code></p>
  <p>But on the function dashboard, down where the list of CloudWatch Events is at, all it said was:</p>
  <p><code>The rule * could not be found.</code></p>
  <hr />
  <h3>Is there a way to make a permission that applies to all rules?</h3>
  <h3>If not, is there a different way to solve this?</h3>
  <p>And if there is no way to directly solve this issue, I could create a separate lambda for each record on the website rather than create separate rules for each record that all point to one lambda. Is there any reason why creating a separate lambda would this would be a bad idea? Such as is there a limit to the amount of Lambda functions you can have?</p>