AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: {{ description }}
Parameters:
  ENVIRONMENT:
    Type: String
    Default: test
  {% for variable in environment_variables %}
  {{ variable.alpha_name }}:
    Type: {{ variable.type }}
  {% endfor %}
Resources:
  {% for table in dynamo_tables %}
  {{ table.name }}:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName:
        Fn::Join:
          - "-"
          - - {{ table.name }}
            - Ref: ENVIRONMENT
      AttributeDefinitions:
        - AttributeName: {{ table.primary_key }}
          AttributeType: S
      KeySchema:
        - AttributeName: {{ table.primary_key }}
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST
      SSESpecification:
        SSEEnabled: True
  {% endfor %}
  CustomerManagedPolicy:
      Type: AWS::IAM::ManagedPolicy
      Properties:
        PolicyDocument:
          Version: 2012-10-17
          Statement:
            - Sid: DynamoDB
              Action:
                - 'dynamodb:*'
              Effect: Allow
              Resource:
                - "*"
            - Sid: Queue
              Action:
                - 'sqs:*'
              Effect: Allow
              Resource:
                - "*"
            - Sid: Cognito
              Action:
                - "*"
              Effect: Allow
              Resource:
                - "*"
            - Sid: SES
              Action:
                - 'ses:*'
              Effect: Allow
              Resource:
                - "*"
            - Sid: Athena
              Action:
                - 'athena:*'
              Effect: Allow
              Resource:
                - "*"
            - Sid: S3
              Action:
                - 's3:*'
              Effect: Allow
              Resource:
                - "*"
            - Sid: Glue
              Action:
                - 'glue:*'
              Effect: Allow
              Resource:
                - "*"
            - Sid: SSM
              Action:
                - 'ssm:*'
              Effect: Allow
              Resource:
                - "*"
  {% if api_functions|length > 1 %}
  RestApi:
    Type: AWS::Serverless::Api
    Properties:
      Name:
        Fn::Join:
          - ""
          - - {{ project_name }}
            - "-"
            - Ref: ENVIRONMENT
      StageName: Prod
      Auth:
        DefaultAuthorizer: CognitoAuthorizer
        Authorizers:
          CognitoAuthorizer:
            UserPoolArn: "arn:aws:cognito-idp:us-east-1:{{ account_id }}:userpool/{{ pool_id }}"
      Cors:
        AllowMethods: "'*'"
        AllowHeaders: "'*'"
        AllowOrigin: "'*'"
      Domain:
        CertificateArn: arn:aws:acm:us-east-1:{{ account_id }}:certificate/{{ certificate_id }}
        DomainName: {{ subdomain }}.{{ domain }}
        Route53:
          HostedZoneName: {{ domain }}.
  {% endif %}
  {% for function in api_functions %}
  {{ function.name }}:
    Type: AWS::Serverless::Function
    Properties:
      Policies:
        Ref: CustomerManagedPolicy
      CodeUri: .
      Handler: api.{{ function.name }}Handler
      Timeout: 300
      Runtime: python3.8
      Environment:
        Variables:
          ENVIRONMENT:
            Ref: ENVIRONMENT
          USER_POOL_ID: {{ pool_id }}
          {% for variable in environment_variables %}
          {{ variable.name }}:
            Ref: {{ variable.alpha_name }}
          {% endfor %}
      Events:
        {% for method in function.methods %}
        {{ method }}Endpoint:
          Type: Api
          Properties:
            Path: {{ function.endpoint }}
            Method: {{ method }}
            RestApiId:
              Ref: RestApi
            Auth:
              Authorizer: CognitoAuthorizer
        {% endfor %}
        CorsEndpoint:
          Type: Api
          Properties:
            Path: {{ function.endpoint }}
            Method: options
            RestApiId:
              Ref: RestApi
            Auth:
              Authorizer: NONE
  {% endfor %}
  {% for function in queue_functions %}
  {{ function.name }}:
    Type: AWS::Serverless::Function
    Properties:
      Policies:
        Ref: CustomerManagedPolicy
      CodeUri: .
      Handler: function.{{ function.name }}Handler
      Timeout: 900
      Runtime: python3.8
      Environment:
        Variables:
          ENVIRONMENT:
            Ref: ENVIRONMENT
      Events:
        MySQSEvent:
          Type: SQS
          Properties:
            Queue: arn:aws:sqs:us-east-1:{{ account_id }}:{{ function.queue }}
            BatchSize: 10
  {% endfor %}
  {% for function in schedule_functions %}
  {{ function.name }}:
    Type: AWS::Serverless::Function
    Properties:
      Policies:
        Ref: CustomerManagedPolicy
      CodeUri: .
      Handler: function.{{ function.name }}Handler
      Timeout: 900
      Runtime: python3.8
      Environment:
        Variables:
          ENVIRONMENT:
            Ref: ENVIRONMENT
      Events:
        ScheduleEvent:
          Type: Schedule
          Properties:
            Schedule: {{ function.schedule }}
  {% endfor %}
  {% for queue in queues %}
  {{ queue.name }}:
    Type: AWS::SQS::Queue
    Properties:
      VisibilityTimeout: 900
      QueueName:
        Fn::Join:
          - "-"
          - - {{ project_name }}
            - {{ queue.name }}
            - Ref: ENVIRONMENT
  {% endfor %}
