locals {
  {{ resource_name }}_lambda_name = "{{ resource_name }}-{{ workspace }}"
  {{ resource_name }}_lambda_arn = join("", aws_lambda_function.{{ resource_name }}.*.arn, data.aws_lambda_function.{{ resource_name }}.*.arn)
  {{ resource_name }}_lambda_uri = join("", aws_lambda_function.{{ resource_name }}.*.invoke_arn, data.aws_lambda_function.{{ resource_name }}.*.invoke_arn)
  {%- if environment_variable_files %}
  {{ resource_name }}_environment_variables = [
    for envars_string in data.local_file.{{ resource_name }}_environment_variables_files: jsondecode(envars_string.content)
  ]
  {%- endif %}
}

# ---------------------------------------------------------------------------------------------------------------------
# LAMBDA
# ---------------------------------------------------------------------------------------------------------------------

data "aws_s3_bucket_object" "{{ resource_name }}-b64sha256" {
  provider = aws.{{ resource_name }}
  bucket = local.{{ resource_name }}_bucket
  key = "${local.{{ resource_name }}_key}.b64sha256"
}

{% if environment_variable_files -%}
data "local_file" "{{ resource_name }}_environment_variables_files" {
  for_each = {for envar_file in {{ environment_variable_files | tojson }}: envar_file => "../${envar_file}"}
  filename = each.value
}
{%- endif %}

resource "aws_lambda_function" "{{ resource_name }}" {
  provider = aws.{{ resource_name }}
  count = local.{{ resource_name }}_when_stage
  function_name = local.{{ resource_name }}_lambda_name
  s3_bucket = local.{{ resource_name }}_bucket
  s3_key = local.{{ resource_name }}_key
  source_code_hash = local.{{ resource_name }}_when_stage > 0 ? data.aws_s3_bucket_object.{{ resource_name }}-b64sha256.body : ""
  role = local.{{ resource_name }}_role_arn
  handler = "{{ module_file }}.{{ handler }}"
  {%- if layers %}
  layers = local.{{ resource_name }}_layer_arns
  {%- endif %}
  runtime = local.{{ resource_name }}_python
  timeout = {{ timeout }}
  memory_size = {{ memory_size }}
  {%- if environment_variable_files != [] %}
  environment {
    variables = merge({{ environment_variables | tojson }},[for item in local.{{ resource_name }}_environment_variables: merge(item,local.{{ resource_name }}_environment_variables...)][0], {"WORKSPACE":"{{ workspace }}"})
  }
  {%- else %}
  environment {
    variables = {"WORKSPACE":"{{ workspace }}"}
  }
  {%- endif %}
  tracing_config {
    mode = "Active"
  }
  vpc_config {
    security_group_ids = local.{{ resource_name }}_security_group_ids
    subnet_ids = local.{{ resource_name }}_subnet_ids
  }
  tags = local.{{ resource_name }}_tags

  depends_on = [
    aws_cloudwatch_log_group.lambda_{{ resource_name }},
  ]
}

data "aws_lambda_function" "{{ resource_name }}" {
  provider = aws.{{ resource_name }}
  count = {{ 'local.{}_when_default'.format(resource_name) if step == "update" else 0}}
  function_name = local.{{ resource_name }}_lambda_name
}

resource "aws_lambda_permission" "{{ resource_name }}" {
  provider = aws.{{ resource_name }}
  count = local.{{ resource_name }}_when_stage
  statement_id = "AllowExecutionFromAPIGateway"
  action = "lambda:InvokeFunction"
  function_name = local.{{ resource_name }}_lambda_name
  principal = "apigateway.amazonaws.com"
  source_arn = "${join("", data.aws_api_gateway_rest_api.{{ resource_name }}.*.execution_arn)}/*/*"
  depends_on = [aws_lambda_function.{{ resource_name }}]
}

resource "aws_cloudwatch_log_group" "lambda_{{ resource_name }}" {
  provider = aws.{{ resource_name }}
  count = local.{{ resource_name }}_when_stage
  name = "/aws/lambda/${local.{{ resource_name }}_lambda_name}"
  retention_in_days = 7
}
