{% for perm in resources -%}
resource "aws_lambda_permission" "permission_{{ perm.function_name | replace('-', '_') | lower }}_{{ loop.index }}" {
  statement_id  = "{{ perm.statement_id }}"
  action        = "lambda:InvokeFunction"
  function_name = "{{ perm.function_name }}"
  principal     = "apigateway.amazonaws.com"

  {%- if perm.get('rest_api_id') %}
  source_arn = "arn:aws:execute-api:*:*:{{ perm.rest_api_id }}/*/*/*"
  {%- endif %}

  {%- if perm.get('source_account') %}
  source_account = "{{ perm.source_account }}"
  {%- endif %}
}
{% endfor %}