AWSTemplateFormatVersion: 2010-09-09
Description: Lambda function serving Lex Bot in order to interact with SMART on FHIR backend services 
Parameters:
  LambdaFunctionName:
    Type: String 
    Default: DemoPatientBot
    Description: The name for Lambda function
  BackendClientId:
    Type: String 
    Description: The Clinet ID from SMART on FHIR backend service. This will be obtained through Epic App Orchard
  KMSCMKKeyId:
    Type: String
    Description: The Key Id for KMS CMK created earlier for signing JWT token. This will be obtained through AWS KMS service.
  FHIREndpointAPI:
    Type: String
    Default: https://apporchard.epic.com/interconnect-aocurprd-oauth/api/FHIR/STU3/
    Description: The endpoint URL for FHIR APIs
  FHIREndpointOAuth:
    Type: String
    Default: https://apporchard.epic.com/interconnect-aocurprd-oauth/oauth2/token
    Description: The endpoint URL for retrieving oauth2 token

Resources:
  SmartBotClientRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: SmartBotClientLambdaRole
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          -
            Effect: "Allow"
            Principal:
              Service:
                - "lambda.amazonaws.com"
            Action:
              - "sts:AssumeRole"
      Policies:  
      - PolicyName: LambdaRolePolicy
        PolicyDocument:
          Version: 2012-10-17
          Statement:
            - Effect: Allow
              Action:
                - logs:CreateLogStream
                - logs:CreateLogGroup
                - logs:PutLogEvents
                - kms:DescribeKey
                - kms:Sign
              Resource: "*"
  SmartBotClientFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: !Ref LambdaFunctionName
      Handler: index.lambda_handler
      Runtime: python3.7
      Description: Lambda function serving Lex Bot in order to interact with SMART on FHIR backend services 
      Role: !GetAtt SmartBotClientRole.Arn
      MemorySize: 512
      Timeout: 60
      Environment:
        Variables:
          client_id: !Ref BackendClientId
          kms_key_id: !Ref KMSCMKKeyId
          endpoint_stu3: !Ref FHIREndpointAPI
          endpoint_token: !Ref FHIREndpointOAuth
      Code:
        S3Bucket: connect-epic-us-east-1
        S3Key: lambda_function.zip
              
Outputs:
  SmartBotClientFunction:
    Description: Lambda function serving Lex Bot in order to interact with SMART on FHIR backend services 
    Value: !GetAtt SmartBotClientFunction.Arn