AWSTemplateFormatVersion: '2010-09-09'
Description: 'Dynatrace Data Acquisition AWS monitoring integration - metrics and smartscape  (v1.0.8 2026-06-11 12:05)'

Metadata:
  Version:
    Number: v1.0.8
    ReleaseDate: 2026-06-11 12:05

Parameters:
  pDynatraceUrl:
    Type: String
    Description: 'Your Dynatrace environment url'
    AllowedPattern: '^https:\/\/.+[^\/]$'
  pMonitoringConfigId:
    Type: String
    Description: 'AWS monitoring extension configuration ID'
  pDynatraceApiClientFunctionArn:
    Type: String
    Description: 'ARN of Dynatrace API client Lambda function'
    MinLength: 1
  pDtApiTokenSecretArn:
    Type: String
    Description: 'ARN of secret with Dynatrace settings token'
    MinLength: 1

Resources:
  CreateCredentialsSettingsObject:
    Type: Custom::DynatraceApiAccessFunction
    Properties:
      ServiceToken: !Ref pDynatraceApiClientFunctionArn
      ServiceTimeout: 120
      Step: HAS_CONFIGURATION
      DynatraceApiKeySecret: !Ref pDtApiTokenSecretArn

  MonitoringRoleStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://dynatrace-data-acquisition.s3.amazonaws.com/aws/deployment/cfn/v1.0.8/da-aws-nested-monitoring-role.yaml
      Parameters:
        pDynatraceUrl: !Ref pDynatraceUrl
        pRoleExternalId: !GetAtt CreateCredentialsSettingsObject.SettingsObjectId
      Tags:
        - Key: 'dt:CreatedBy'
          Value: 'Dynatrace'
        - Key: 'dt:EnvironmentID'
          Value: !Select [ 0, !Split [ ".", !Select [ 1, !Split [ "https://", !Ref pDynatraceUrl ] ] ] ]

  ConfigureCredentialsSettingsObject:
    Type: Custom::DynatraceApiAccessFunction
    Properties:
      ServiceToken: !Ref pDynatraceApiClientFunctionArn
      ServiceTimeout: 120
      Step: HAS_MONITORING_ROLE
      DynatraceApiKeySecret: !Ref pDtApiTokenSecretArn
      SettingsObjectId: !GetAtt CreateCredentialsSettingsObject.SettingsObjectId
      HASConnectionName: !GetAtt CreateCredentialsSettingsObject.HASConnectionName
      MonitoringRole: !GetAtt MonitoringRoleStack.Outputs.DynatraceMonitoringRoleArn

  ConfigureDynatraceConnectionCredentials:
    Type: Custom::DynatraceApiAccessFunction
    DependsOn: ConfigureCredentialsSettingsObject
    Properties:
      ServiceToken: !Ref pDynatraceApiClientFunctionArn
      ServiceTimeout: 120
      Step: MONITORING_CONFIGURATION
      DynatraceApiKeySecret: !Ref pDtApiTokenSecretArn
      MonitoringConfigurationId: !Ref pMonitoringConfigId
      SettingsObjectId: !GetAtt CreateCredentialsSettingsObject.SettingsObjectId
      HASConnectionName: !GetAtt CreateCredentialsSettingsObject.HASConnectionName
      MonitoringRole: !GetAtt MonitoringRoleStack.Outputs.DynatraceMonitoringRoleArn

Outputs:
  IAMRoleExternalId:
    Value: !GetAtt CreateCredentialsSettingsObject.SettingsObjectId