AWSTemplateFormatVersion: '2010-09-09'
Description: Monte Carlo SQS queue for Databricks integration with OpenTelemetry Collector.
Metadata:
  License: >
    Copyright 2023 Monte Carlo Data, Inc.

    The Software contained herein (the "Software") is the intellectual property of Monte Carlo Data, Inc. ("Licensor"),
    and Licensor retains all intellectual property rights in the Software, including any and all derivatives, changes and
    improvements thereto. Only customers who have entered into a commercial agreement with Licensor for use or
    purchase of the Software ("Licensee") are licensed or otherwise authorized to use the Software, and any Licensee
    agrees that it obtains no copyright or other intellectual property rights to the Software, except for the license
    expressly granted below or in accordance with the terms of their commercial agreement with Licensor (the
    "Agreement"). Subject to the terms and conditions of the Agreement, Licensor grants Licensee a non-exclusive,
    non-transferable, non-sublicensable, revocable, limited right and license to use the Software, in each case solely
    internally within Licensee's organization for non-commercial purposes and only in connection with the service
    provided by Licensor pursuant to the Agreement, and in object code form only. Without Licensor's express prior
    written consent, Licensee may not, directly or indirectly, (i) distribute the Software, any portion thereof, or any
    modifications, enhancements, or derivative works of any of the foregoing (collectively, the "Derivatives") to any
    third party, (ii) license, market, sell, offer for sale or otherwise attempt to commercialize any Software, Derivatives,
    or portions thereof, (iii) use the Software, Derivatives, or any portion thereof for the benefit of any third party, (iv)
    use the Software, Derivatives, or any portion thereof in any manner or with respect to any commercial activity
    which competes, or is reasonably likely to compete, with any business that Licensor conducts, proposes to conduct
    or demonstrably anticipates conducting, at any time; or (v) seek any patent or other intellectual property rights or
    protections over or in connection with any Software of Derivatives.
Parameters:
  StorageBucketArn:
    Description: ARN of the S3 bucket used by the Agent and OpenTelemetry Collector to store data sampling records and telemetry data.
    Type: String
    AllowedPattern: '^arn:aws:s3:::.*$'
    ConstraintDescription: Must be a valid S3 bucket ARN
  OpenTelemetryCollectorExternalAccessRoleArn:
    Description: The ARN of the IAM role for external access to the OpenTelemetry S3 bucket
    Type: String
    AllowedPattern: '^arn:aws:iam::.*:role/.*$'
    ConstraintDescription: Must be a valid IAM role ARN
Resources:
  SqsNotificationQueue:
    Type: AWS::SQS::Queue
    Properties:
      VisibilityTimeout: 30
      MessageRetentionPeriod: 1209600
      ReceiveMessageWaitTimeSeconds: 20
      Tags:
        - Key: Service
          Value: "mcd-otel-collector"
        - Key: Provider
          Value: "monte-carlo"
  SqsS3AccessPolicy:
    Type: AWS::SQS::QueuePolicy
    Properties:
      Queues:
        - !Ref SqsNotificationQueue
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: s3.amazonaws.com
            Action:
              - sqs:SendMessage
            Resource: !GetAtt SqsNotificationQueue.Arn
            Condition:
              ArnLike:
                aws:SourceArn: !Ref StorageBucketArn
  SqsDatabricksAccessPolicy:
    Type: AWS::SQS::QueuePolicy
    Properties:
      Queues:
        - !Ref SqsNotificationQueue
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              AWS: !Ref OpenTelemetryCollectorExternalAccessRoleArn
            Action:
              - sqs:ReceiveMessage
              - sqs:DeleteMessage
              - sqs:GetQueueAttributes
              - sqs:GetQueueUrl
            Resource: !GetAtt SqsNotificationQueue.Arn
  OpenTelemetryCollectorSqsAccessPolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      Description: "Policy allowing OpenTelemetry Collector to access SQS queue for Databricks notifications"
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action:
              - sqs:ReceiveMessage
              - sqs:DeleteMessage
              - sqs:GetQueueAttributes
              - sqs:GetQueueUrl
            Resource: !GetAtt SqsNotificationQueue.Arn
      Roles:
        - !Select [1, !Split ['/', !Ref OpenTelemetryCollectorExternalAccessRoleArn]]
Outputs:
  SqsNotificationQueueArn:
    Description: The ARN of the SQS queue for S3 event notifications
    Value: !GetAtt SqsNotificationQueue.Arn
  SqsNotificationQueueUrl:
    Description: The URL of the SQS queue for S3 event notifications
    Value: !Ref SqsNotificationQueue
  OpenTelemetryCollectorSqsAccessPolicyArn:
    Description: The ARN of the IAM managed policy for SQS access
    Value: !Ref OpenTelemetryCollectorSqsAccessPolicy
