AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SLater Rest API Service
Globals:
  Api:
    OpenApiVersion: 3.0.1
    CacheClusterEnabled: false
    CacheClusterSize: '0.5'
    TracingEnabled: true
    MethodSettings:
    - CacheDataEncrypted: true
      CacheTtlInSeconds: 300
      CachingEnabled: true
      HttpMethod: '*'
      ResourcePath: /*
      ThrottlingBurstLimit: 5000
      ThrottlingRateLimit: 10000
      LoggingLevel: ERROR
      MetricsEnabled: true
      DataTraceEnabled: true
    AccessLogSetting:
      DestinationArn:
        Fn::GetAtt:
        - SlaterApiGatewayLogging
        - Arn
      Format: $context.identity.sourceIp $context.identity.caller $context.identity.user
        [$context.requestTime] "$context.httpMethod $context.resourcePath $context.protocol"
        $context.status $context.responseLength $context.requestId
  Function:
    Timeout: 28
    MemorySize: 3008
    Tracing: Active
    Runtime: python3.8
    Environment:
      Variables:
        AWS_XRAY_TRACING_NAME: SLater
        AWS_XRAY_SDK_ENABLED: 'true'
        AWS_XRAY_CONTEXT_MISSING: LOG_ERROR
        INVALIDATE_SECRETS_CACHE: 'false'
Mappings:
  Accounts:
    '747207162522':
      EnvironmentTagValue: PRD
      StageName: prod
      UserPoolDomainValue: slater-auth
      HostedZoneId: Z2ECL21AX7NLEE
      AcmCertificateArn: arn:aws:acm:us-east-1:747207162522:certificate/cb9f2998-9f5c-4055-a4e5-305686204d59
      ApiDomainName: api.pearsoncloudmanagement.com
      ReservedConcurrentExecutions: 10
      ProvisionedConcurrentExecutions: 5
      DeploymentPreference: AllAtOnce
      LogRetentionDays: 30
      LogLevel: error
      snsProtocol: https
      snsEndpoint: https://events.pagerduty.com/integration/50b49fde8b364b1dafe96398542a86f5/enqueue
      ruleState: ENABLED
      SecurityGroupIds:
      - sg-0ecf33316989e9edd
      SubnetIds:
      - subnet-02f2580fae0856acd
      - subnet-0b8784b23441cfd50
    '825997664289':
      EnvironmentTagValue: QA
      StageName: qa
      UserPoolDomainValue: slater-auth-qa
      HostedZoneId: Z089994214M7V7X28INY7
      AcmCertificateArn: arn:aws:acm:us-east-1:825997664289:certificate/d4c8822b-c0d8-4ba5-a853-7ed41ebccc63
      ApiDomainName: api-qa.pearsoncloudmanagement.com
      ReservedConcurrentExecutions: 10
      ProvisionedConcurrentExecutions: 5
      DeploymentPreference: AllAtOnce
      LogRetentionDays: 30
      LogLevel: error
      snsProtocol: email
      snsEndpoint: slater-healthstatus-failure@amazon.pearson.com
      ruleState: ENABLED
      SecurityGroupIds:
      - sg-08934bdb6b09d31da
      SubnetIds:
      - subnet-0b921e6d60be8cb30
      - subnet-0edf56a22d03b6354
    '530786275774':
      EnvironmentTagValue: DEV
      StageName: dev
      UserPoolDomainValue: slater-auth-dev
      HostedZoneId: Z08197632ITWOHM8Y6D1C
      AcmCertificateArn: arn:aws:acm:us-east-1:530786275774:certificate/136087b3-c7bf-40aa-8e93-b35b2e9ef820
      ApiDomainName: api-dev.pearsoncloudmanagement.com
      ReservedConcurrentExecutions: 10
      ProvisionedConcurrentExecutions: 1
      DeploymentPreference: AllAtOnce
      LogRetentionDays: 60
      LogLevel: warning
      snsProtocol: email
      snsEndpoint: slater-healthstatus-failure@amazon.pearson.com
      ruleState: ENABLED
      SecurityGroupIds:
      - sg-05e361e506470f2ec
      SubnetIds:
      - subnet-0b6eaec961b2bb7d0
      - subnet-028135f39251de0d5
Parameters:
  SlaterBasePath:
    Description: Base Path Mapping
    Type: String
    Default: slater
  LayerName:
    Description: Python-Layer Used by SLater
    Type: String
    Default: pcm-slater-python-layer
  LayerVersion:
    Description: Version number of the Slater lambda layer
    Type: Number
    Default: 28
  LocalLayerUri:
    Description: Full path to local lambda layer
    Type: String
    Default: ci/mock/layer/
Conditions:
  Local:
    Fn::Equals:
    - Ref: AWS::AccountId
    - '123456789012'
  Prod:
    Fn::Equals:
    - Ref: AWS::AccountId
    - '747207162522'
  NoLocalLayer:
    Fn::Equals:
    - Ref: LocalLayerUri
    - ''
  IgnoreLocalLayer:
    Fn::Not:
    - Fn::Equals:
      - Ref: LocalLayerUri
      - ''
Resources:
  UserPool:
    Type: AWS::Cognito::UserPool
    Properties:
      UserPoolName: slater-user-pool
      UserPoolTags:
        Name: slater-user-pool
        t_dcl: '2'
        t_environment:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - EnvironmentTagValue
  AuthDomain:
    Type: AWS::Cognito::UserPoolDomain
    Properties:
      Domain:
        Fn::FindInMap:
        - Accounts
        - Ref: AWS::AccountId
        - UserPoolDomainValue
      UserPoolId:
        Ref: UserPool
  AdminResourceServer:
    Type: AWS::Cognito::UserPoolResourceServer
    Properties:
      Identifier: slater
      Name: slater
      Scopes:
      - ScopeName: write
        ScopeDescription: Write access
      - ScopeName: read
        ScopeDescription: Read access
      - ScopeName: delete
        ScopeDescription: Delete access
      UserPoolId:
        Ref: UserPool
  ROResourceServer:
    Type: AWS::Cognito::UserPoolResourceServer
    Properties:
      Identifier: slater-ro
      Name: slater-ro
      Scopes:
      - ScopeName: read
        ScopeDescription: Read access
      UserPoolId:
        Ref: UserPool
  AdminPoolClient:
    Type: AWS::Cognito::UserPoolClient
    Properties:
      ClientName: slater-client
      GenerateSecret: true
      UserPoolId:
        Ref: UserPool
      AllowedOAuthFlows:
      - client_credentials
      AllowedOAuthFlowsUserPoolClient: true
      AllowedOAuthScopes:
      - slater/read
      - slater/write
      - slater/delete
    DependsOn: AdminResourceServer
  ROPoolClient:
    Type: AWS::Cognito::UserPoolClient
    Properties:
      ClientName: slater-ro-client
      GenerateSecret: true
      UserPoolId:
        Ref: UserPool
      AllowedOAuthFlows:
      - client_credentials
      AllowedOAuthFlowsUserPoolClient: true
      AllowedOAuthScopes:
      - slater-ro/read
    DependsOn: ROResourceServer
  SlaterApiGatewayLogging:
    DependsOn: ApiGatewayAccount
    Type: AWS::Logs::LogGroup
    Properties:
      RetentionInDays: 7
  SlaterApiGatewayRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - apigateway.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: /
      ManagedPolicyArns:
      - arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs
  ApiGatewayAccount:
    DependsOn:
    - SlaterApiGatewayRole
    - SlaterApiGateway
    Type: AWS::ApiGateway::Account
    Properties:
      CloudWatchRoleArn:
        Fn::GetAtt:
        - SlaterApiGatewayRole
        - Arn
  SlaterApiGateway:
    DependsOn: UserPool
    Type: AWS::Serverless::Api
    Properties:
      StageName:
        Fn::FindInMap:
        - Accounts
        - Ref: AWS::AccountId
        - StageName
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location: s3://prsn-code-artifacts-us-east-1-530786275774/slater/dev/sam-packages/ee2b98f7ad13c7a6bcae5c1c5545e745
      EndpointConfiguration: REGIONAL
      Domain:
        CertificateArn:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - AcmCertificateArn
        DomainName:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - ApiDomainName
        Route53:
          HostedZoneId:
            Fn::FindInMap:
            - Accounts
            - Ref: AWS::AccountId
            - HostedZoneId
        BasePath:
        - Ref: SlaterBasePath
        Auth:
          Authorizers:
            CognitoAuthorizer:
              UserPoolArn:
                Fn::GetAtt:
                - UserPool
                - UserPoolName
      BinaryMediaTypes:
      - multipart/form-data
  SlaterApiHealthCheck:
    Type: AWS::Route53::HealthCheck
    Properties:
      HealthCheckConfig:
        Type: HTTPS
        ResourcePath: /slater/healthstatus
        FullyQualifiedDomainName:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - ApiDomainName
        RequestInterval: 30
        FailureThreshold: 3
      HealthCheckTags:
      - Key: t_dcl
        Value: '2'
      - Key: Name
        Value: SlaterApiHealthCheck
  SlaterApiHealthCheckTopic:
    Type: AWS::SNS::Topic
    Properties:
      DisplayName: SlaterApiHealthCheckTopic
      Subscription:
      - Endpoint:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - snsEndpoint
        Protocol:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - snsProtocol
      Tags:
      - Key: t_dcl
        Value: '2'
      - Key: Name
        Value: SlaterApiHealthCheckTopic
  SlaterApiHealthCheckAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: SlaterApiHealthCheck failures
      AlarmName: SlaterApiHealthCheckAlarm
      ComparisonOperator: LessThanThreshold
      EvaluationPeriods: 2
      Threshold: 1
      MetricName: HealthCheckStatus
      Namespace: AWS/Route53
      Period: 60
      Statistic: Minimum
      TreatMissingData: breaching
      AlarmActions:
      - Ref: SlaterApiHealthCheckTopic
      Dimensions:
      - Name: HealthCheckId
        Value:
          Ref: SlaterApiHealthCheck
  CommonLambdaIamPolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      Description: Common Lambda Policy
      PolicyDocument:
        Version: 2012-10-17
        Statement:
        - Effect: Allow
          Action:
          - secretsmanager:List*
          - secretsmanager:Describe*
          - secretsmanager:Get*
          - logs:CreateLogGroup
          - logs:CreateLogStream
          - logs:PutLogEvents
          - cloudformation:DescribeStackResources
          - cognito-idp:DescribeUserPoolClient
          - xray:PutTraceSegments
          - xray:PutTelemetryRecords
          Resource: '*'
        - Effect: Allow
          Action:
          - s3:PutObject
          - s3:GetObject
          - s3:DeleteObject
          - lambda:InvokeFunction
          Resource:
          - Fn::Sub: arn:aws:s3:::prsn-slater-batch-uploads-us-east-1-${AWS::AccountId}/*
          - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:function:*
  ApiGatewayInvokeLambdaFinderPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName:
        Ref: FinderFunction
      Principal: apigateway.amazonaws.com
  FinderFunction:
    Type: AWS::Serverless::Function
    Description: Slater Finder Function
    Properties:
      FunctionName: FinderFunction
      Handler: function.handler
      CodeUri: s3://prsn-code-artifacts-us-east-1-530786275774/slater/dev/sam-packages/dd611a28e36b944f95192df03a0e2891
      Environment:
        Variables:
          LOG_LEVEL:
            Fn::FindInMap:
            - Accounts
            - Ref: AWS::AccountId
            - LogLevel
      Layers:
      - Fn::If:
        - NoLocalLayer
        - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
        - Fn::If:
          - Local
          - Ref: SlaterLayerLocal
          - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
      VpcConfig:
        SecurityGroupIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SecurityGroupIds
        SubnetIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SubnetIds
      Events:
        GetContactsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacts
            Method: GET
        GetAccountsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accounts
            Method: GET
        GetAccountApplicationsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountapplications
            Method: GET
        GetContactTypesApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacttypes
            Method: GET
        GetAccountContactsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountcontacts
            Method: GET
        GetApplicationContactsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applicationcontacts
            Method: GET
        GetApplicationsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applications
            Method: GET
        GetLobsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /lobs
            Method: GET
        GetMasterPayersApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /masterpayers
            Method: GET
        GetAccountTypesApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accounttypes
            Method: GET
        GetBusinessUnitsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /businessunits
            Method: GET
        GetTowersApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /towers
            Method: GET
        GetBatchGroupApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /batchgroup
            Method: GET
        GetBatchErrorsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /batcherrors
            Method: GET
        CdsAllSnapshotApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /cdsallsnapshot
            Method: GET
        ExportApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /export
            Method: GET
        GetValidExportsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /validexports
            Method: GET
        GetCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /customfields
            Method: GET
        GetAccountCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountcustomfields
            Method: GET
        GetApplicationCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applicationcustomfields
            Method: GET
      Policies:
      - Ref: CommonLambdaIamPolicy
  ApiGatewayInvokeLambdaCreatorPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName:
        Ref: CreatorFunction
      Principal: apigateway.amazonaws.com
  CreatorFunction:
    Type: AWS::Serverless::Function
    Description: Slater Creator Function
    Properties:
      FunctionName: CreatorFunction
      Handler: function.handler
      CodeUri: s3://prsn-code-artifacts-us-east-1-530786275774/slater/dev/sam-packages/7a8144653b4fdd741372e99bd3985763
      Environment:
        Variables:
          LOG_LEVEL:
            Fn::FindInMap:
            - Accounts
            - Ref: AWS::AccountId
            - LogLevel
      Layers:
      - Fn::If:
        - NoLocalLayer
        - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
        - Fn::If:
          - Local
          - Ref: SlaterLayerLocal
          - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
      VpcConfig:
        SecurityGroupIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SecurityGroupIds
        SubnetIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SubnetIds
      Events:
        CreateContactApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacts
            Method: POST
        CreateAccountApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accounts
            Method: POST
        CreateApplicationApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applications
            Method: POST
        CreateContactTypesApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacttypes
            Method: POST
        CreateAccountContactsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountcontacts
            Method: POST
        CreateApplicationContactsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applicationcontacts
            Method: POST
        CreateAccountTypesApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accounttypes
            Method: POST
        CreateBusinessUnitsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /businessunits
            Method: POST
        CreateLobsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /lobs
            Method: POST
        CreateMasterPayersApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /masterpayers
            Method: POST
        CreateTowersApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /towers
            Method: POST
        CreateAuthTokenApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /authtoken
            Method: POST
        CreateCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /customfields
            Method: POST
        CreateAccountCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountcustomfields
            Method: POST
        CreateApplicationCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applicationcustomfields
            Method: POST
      Policies:
      - Ref: CommonLambdaIamPolicy
  ApiGatewayInvokeLambdaDeleterPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName:
        Ref: DeleterFunction
      Principal: apigateway.amazonaws.com
  DeleterFunction:
    Type: AWS::Serverless::Function
    Description: Slater Deleter Function
    Properties:
      FunctionName: DeleterFunction
      Handler: function.handler
      CodeUri: s3://prsn-code-artifacts-us-east-1-530786275774/slater/dev/sam-packages/c4e8adec9ea6da4a095f0b4b798c6091
      Environment:
        Variables:
          LOG_LEVEL:
            Fn::FindInMap:
            - Accounts
            - Ref: AWS::AccountId
            - LogLevel
      Layers:
      - Fn::If:
        - NoLocalLayer
        - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
        - Fn::If:
          - Local
          - Ref: SlaterLayerLocal
          - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
      VpcConfig:
        SecurityGroupIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SecurityGroupIds
        SubnetIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SubnetIds
      Events:
        DeleteContactApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacts
            Method: DELETE
        DeleteContactTypesApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacttypes
            Method: DELETE
        DeleteAccountContactsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountcontacts
            Method: DELETE
        DeleteApplicationContactsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applicationcontacts
            Method: DELETE
        DeleteAccountApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accounts
            Method: DELETE
        DeleteApplicationApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applications
            Method: DELETE
        DeleteCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /customfields
            Method: DELETE
        DeleteAccountCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountcustomfields
            Method: DELETE
        DeleteApplicationCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applicationcustomfields
            Method: DELETE
      Policies:
      - Ref: CommonLambdaIamPolicy
  ApiGatewayInvokeLambdaUpdaterPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName:
        Ref: UpdaterFunction
      Principal: apigateway.amazonaws.com
  UpdaterFunction:
    Type: AWS::Serverless::Function
    Description: Slater Updater Function
    Properties:
      FunctionName: UpdaterFunction
      Handler: function.handler
      CodeUri: s3://prsn-code-artifacts-us-east-1-530786275774/slater/dev/sam-packages/5d68bb821de126f4c233dd7e1a4b6b2a
      Environment:
        Variables:
          LOG_LEVEL:
            Fn::FindInMap:
            - Accounts
            - Ref: AWS::AccountId
            - LogLevel
      Layers:
      - Fn::If:
        - NoLocalLayer
        - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
        - Fn::If:
          - Local
          - Ref: SlaterLayerLocal
          - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
      VpcConfig:
        SecurityGroupIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SecurityGroupIds
        SubnetIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SubnetIds
      Events:
        UpdateContactApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacts
            Method: PUT
        UpdateContactTypesApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacttypes
            Method: PUT
        UpdateAccountApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accounts
            Method: PUT
        UpdateApplicationApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applications
            Method: PUT
        UpdateAccountContactApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountcontacts
            Method: PUT
        UpdateApplicationContactApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applicationcontacts
            Method: PUT
        UpdateLobApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /lobs
            Method: PUT
        UpdateMasterPayerApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /masterpayers
            Method: PUT
        UpdateAccountTypesApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accounttypes
            Method: PUT
        UpdateBusinessUnitsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /businessunits
            Method: PUT
        UpdateTowersApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /towers
            Method: PUT
      Policies:
      - Ref: CommonLambdaIamPolicy
  ApiGatewayInvokeLambdaPatcherPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName:
        Ref: PatcherFunction
      Principal: apigateway.amazonaws.com
  PatcherFunction:
    Type: AWS::Serverless::Function
    Description: Slater Patcher Function
    Properties:
      FunctionName: PatcherFunction
      Handler: function.handler
      CodeUri: s3://prsn-code-artifacts-us-east-1-530786275774/slater/dev/sam-packages/6423ba74c82d5e3bccdae7425f9fc4eb
      Environment:
        Variables:
          LOG_LEVEL:
            Fn::FindInMap:
            - Accounts
            - Ref: AWS::AccountId
            - LogLevel
      Layers:
      - Fn::If:
        - NoLocalLayer
        - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
        - Fn::If:
          - Local
          - Ref: SlaterLayerLocal
          - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
      VpcConfig:
        SecurityGroupIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SecurityGroupIds
        SubnetIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SubnetIds
      Events:
        PatchContactApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacts
            Method: PATCH
        PatchContactTypesApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /contacttypes
            Method: PATCH
        PatchAccountApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accounts
            Method: PATCH
        PatchApplicationApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applications
            Method: PATCH
        PatchAccountContactApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountcontacts
            Method: PATCH
        PatchApplicationContactApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applicationcontacts
            Method: PATCH
        PatchLobApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /lobs
            Method: PATCH
        PatchMasterPayerApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /masterpayers
            Method: PATCH
        PatchAccountTypesApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accounttypes
            Method: PATCH
        PatchBusinessUnitsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /businessunits
            Method: PATCH
        PatchTowersApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /towers
            Method: PATCH
        PatchCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /customfields
            Method: PATCH
        PatchAccountCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /accountcustomfields
            Method: PATCH
        PatchApplicationCustomFieldsApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /applicationcustomfields
            Method: PATCH
      Policies:
      - Ref: CommonLambdaIamPolicy
  ApiGatewayInvokeLambdaUploaderPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName:
        Ref: UploaderFunction
      Principal: apigateway.amazonaws.com
  UploaderFunction:
    Type: AWS::Serverless::Function
    Description: Slater Uploader Function
    Properties:
      FunctionName: UploaderFunction
      Handler: function.handler
      CodeUri: s3://prsn-code-artifacts-us-east-1-530786275774/slater/dev/sam-packages/268838b352e16baec1a2451459582038
      Layers:
      - Fn::If:
        - NoLocalLayer
        - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
        - Fn::If:
          - Local
          - Ref: SlaterLayerLocal
          - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
      VpcConfig:
        SecurityGroupIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SecurityGroupIds
        SubnetIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SubnetIds
      Events:
        BatchGroupUploadApi:
          Type: Api
          Properties:
            RestApiId:
              Ref: SlaterApiGateway
            Path: /batchgroup
            Method: POST
      Policies:
      - Ref: CommonLambdaIamPolicy
      Environment:
        Variables:
          BATCH_UPLOAD_BUCKET:
            Fn::Sub: prsn-slater-batch-uploads-us-east-1-${AWS::AccountId}
          BATCH_PROCESS_FUNCTION:
            Ref: BatchProcessFunction
          LOG_LEVEL:
            Fn::FindInMap:
            - Accounts
            - Ref: AWS::AccountId
            - LogLevel
  SlaterLayerLocal:
    Type: AWS::Serverless::LayerVersion
    Condition: IgnoreLocalLayer
    Properties:
      LayerName: mock-slater-python-layer
      Description: PCM Custom Slater Python Layer
      ContentUri: s3://prsn-code-artifacts-us-east-1-530786275774/slater/dev/sam-packages/beb3981c35b83d07ce64e8fcbbdd341d
      CompatibleRuntimes:
      - python3.7
      - python3.8
      LicenseInfo: MIT
      RetentionPolicy: Retain
    Metadata:
      BuildMethod: python3.8
  ApiGatewayInvokeLambdaBatchProcessPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName:
        Ref: BatchProcessFunction
      Principal: apigateway.amazonaws.com
  BatchProcessFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: BatchFunctionLogGroup
      Handler: function.handler
      CodeUri: s3://prsn-code-artifacts-us-east-1-530786275774/slater/dev/sam-packages/f6f8086511dbeb169813f2e6e180b7ea
      Layers:
      - Fn::If:
        - NoLocalLayer
        - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
        - Fn::If:
          - Local
          - Ref: SlaterLayerLocal
          - Fn::Sub: arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:${LayerName}:${LayerVersion}
      VpcConfig:
        SecurityGroupIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SecurityGroupIds
        SubnetIds:
          Fn::FindInMap:
          - Accounts
          - Ref: AWS::AccountId
          - SubnetIds
      Policies:
      - Ref: CommonLambdaIamPolicy
      Timeout: 900
      MemorySize: 3008
      Tracing: Active
      Environment:
        Variables:
          BATCH_UPLOAD_BUCKET:
            Fn::Sub: prsn-slater-batch-uploads-us-east-1-${AWS::AccountId}
          LOG_LEVEL:
            Fn::FindInMap:
            - Accounts
            - Ref: AWS::AccountId
            - LogLevel
Outputs:
  CommonLambdaIamPolicy:
    Description: CommonLambdaIamPolicy
    Value:
      Ref: CommonLambdaIamPolicy
  SlaterApiGateway:
    Description: SlaterApiGateway
    Value:
      Ref: SlaterApiGateway
  FinderFunction:
    Description: Finder REST API endpoint
    Value:
      Fn::GetAtt:
      - FinderFunction
      - Arn
  CreatorFunction:
    Description: Creator REST API endpoint
    Value:
      Fn::GetAtt:
      - CreatorFunction
      - Arn
  UpdaterFunction:
    Description: Updater REST API endpoint
    Value:
      Fn::GetAtt:
      - UpdaterFunction
      - Arn
  PatcherFunction:
    Description: Patcher REST API endpoint
    Value:
      Fn::GetAtt:
      - PatcherFunction
      - Arn
  DeleterFunction:
    Description: Deleter REST API endpoint
    Value:
      Fn::GetAtt:
      - DeleterFunction
      - Arn
  UploaderFunction:
    Description: Uploader REST API endpoint
    Value:
      Fn::GetAtt:
      - UploaderFunction
      - Arn
  BatchProcessFunction:
    Description: Batch Process REST API endpoint
    Value:
      Fn::GetAtt:
      - BatchProcessFunction
      - Arn
  ApiGatewayInvokeLambdaFinderPermission:
    Description: API Gateway Lambda Execute Permissions
    Value:
      Ref: ApiGatewayInvokeLambdaFinderPermission
  ApiGatewayInvokeLambdaCreatorPermission:
    Description: API Gateway Lambda Execute Permissions
    Value:
      Ref: ApiGatewayInvokeLambdaCreatorPermission
  ApiGatewayInvokeLambdaUpdaterPermission:
    Description: API Gateway Lambda Execute Permissions
    Value:
      Ref: ApiGatewayInvokeLambdaUpdaterPermission
  ApiGatewayInvokeLambdaDeleterPermission:
    Description: API Gateway Lambda Execute Permissions
    Value:
      Ref: ApiGatewayInvokeLambdaDeleterPermission
  ApiGatewayInvokeLambdaUploaderPermission:
    Description: API Gateway Lambda Execute Permissions
    Value:
      Ref: ApiGatewayInvokeLambdaUploaderPermission
  ApiGatewayInvokeLambdaBatchProcessPermission:
    Description: API Gateway Lambda Execute Permissions
    Value:
      Ref: ApiGatewayInvokeLambdaBatchProcessPermission
