|
| 1 | +locals { |
| 2 | + cognito_role_name = "${terraform.workspace}-cognito-unauth-role" |
| 3 | +} |
| 4 | + |
| 5 | +resource "aws_iam_role" "cognito_unauthenticated" { |
| 6 | + count = local.is_production ? 0 : 1 |
| 7 | + name = local.cognito_role_name |
| 8 | + |
| 9 | + assume_role_policy = jsonencode({ |
| 10 | + Version = "2012-10-17", |
| 11 | + Statement = [ |
| 12 | + { |
| 13 | + Effect = "Allow", |
| 14 | + Principal : { |
| 15 | + Federated : "cognito-identity.amazonaws.com" |
| 16 | + }, |
| 17 | + Action = "sts:AssumeRoleWithWebIdentity", |
| 18 | + Condition = { |
| 19 | + StringEquals = { |
| 20 | + "cognito-identity.amazonaws.com:aud" = aws_cognito_identity_pool.cloudwatch_rum[0].id |
| 21 | + }, |
| 22 | + "ForAnyValue:StringLike" = { |
| 23 | + "cognito-identity.amazonaws.com:amr" = "unauthenticated" |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + ] |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +resource "aws_iam_policy" "cloudwatch_rum_cognito_access" { |
| 32 | + count = local.is_production ? 0 : 1 |
| 33 | + name = "${terraform.workspace}-cloudwatch-rum-cognito-access-policy" |
| 34 | + description = "Policy for unauthenticated Cognito identities" |
| 35 | + |
| 36 | + policy = jsonencode( |
| 37 | + { |
| 38 | + "Version" : "2012-10-17", |
| 39 | + "Statement" : [ |
| 40 | + { |
| 41 | + "Effect" : "Allow", |
| 42 | + "Action" : "rum:PutRumEvents", |
| 43 | + "Resource" : "arn:aws:rum:${local.current_region}:${local.current_account_id}:appmonitor/${aws_rum_app_monitor.ndr[0].id}" |
| 44 | + } |
| 45 | + ] |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +resource "aws_iam_role_policy_attachment" "cloudwatch_rum_cognito_unauth" { |
| 50 | + count = local.is_production ? 0 : 1 |
| 51 | + role = aws_iam_role.cognito_unauthenticated[0].name |
| 52 | + policy_arn = aws_iam_policy.cloudwatch_rum_cognito_access[0].arn |
| 53 | +} |
| 54 | + |
| 55 | +resource "aws_cognito_identity_pool_roles_attachment" "cloudwatch_rum" { |
| 56 | + count = local.is_production ? 0 : 1 |
| 57 | + identity_pool_id = aws_cognito_identity_pool.cloudwatch_rum[0].id |
| 58 | + |
| 59 | + roles = { |
| 60 | + unauthenticated = aws_iam_role.cognito_unauthenticated[0].arn |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +resource "aws_cognito_identity_pool" "cloudwatch_rum" { |
| 65 | + count = local.is_production ? 0 : 1 |
| 66 | + identity_pool_name = "${terraform.workspace}-cloudwatch-rum-identity-pool" |
| 67 | + allow_unauthenticated_identities = true |
| 68 | +} |
| 69 | + |
| 70 | +resource "aws_rum_app_monitor" "ndr" { |
| 71 | + count = local.is_production ? 0 : 1 |
| 72 | + name = "${terraform.workspace}-app-monitor" |
| 73 | + domain = "*.${var.domain}" |
| 74 | + cw_log_enabled = false |
| 75 | + |
| 76 | + app_monitor_configuration { |
| 77 | + identity_pool_id = aws_cognito_identity_pool.cloudwatch_rum[0].id |
| 78 | + allow_cookies = true |
| 79 | + enable_xray = false |
| 80 | + session_sample_rate = 1.0 |
| 81 | + telemetries = ["errors", "performance", "http"] |
| 82 | + } |
| 83 | +} |
0 commit comments