Description: AWS IoT Core and Amazon Timestream integration.
Resources:
  TimestreamDataBase:
    Type: 'AWS::Timestream::Database'
    Properties:
      DatabaseName: "IotTimestreamDataBase"
  TimestreamTable:
    Type: 'AWS::Timestream::Table'
    Properties:
      DatabaseName: !Ref TimestreamDataBase
      TableName: "IotTimestreamTable"
      RetentionProperties:
        MemoryStoreRetentionPeriodInHours: '24'
        MagneticStoreRetentionPeriodInDays: '5'
  IoTDataToTimestreamRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - iot.amazonaws.com
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Policies:
        - PolicyName: !Join
            - ''
            - - IoTDataToTimestreamInlinePolicy-
              - !Ref 'AWS::Region'
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - 'timestream:WriteRecords'
                Resource:
                  - !GetAtt
                    - TimestreamTable
                    - Arn
              - Effect: Allow
                Action:
                  - 'timestream:DescribeEndpoints'
                Resource: '*'
      Path: /service-role/
Outputs:
  TimestreamDataBaseName:
    Description: Timestream data base name
    Value: !Ref TimestreamDataBase
  TimestreamTableName:
    Description: Timestream table name
    Value: !Ref TimestreamTable
  TimestreamTableArn:
    Description: Timestream table arn
    Value: !GetAtt
      - TimestreamTable
      - Arn
  IoTDataToTimestreamRoleArn:
    Description: Arn for the IAM role to allow write to Timestream table
    Value: !GetAtt
      - IoTDataToTimestreamRole
      - Arn