|
1 | | -# Terraform module for the Data Upload Manager (DUM) Cognito User Pool |
| 1 | +# DUM Cognito Client Setup Using Shared Cognito Infrastructure |
| 2 | +# |
| 3 | +# NOTE: |
| 4 | +# This module uses a PDS common Cognito user pool managed by a separate |
| 5 | +# Terraform stack and does NOT create or manage the user pool, domain, |
| 6 | +# users, or groups. |
| 7 | +# |
| 8 | +# It reads the existing user pool ID from SSM Parameter Store and creates |
| 9 | +# only a DUM-specific Cognito app client. The client ID is stored in a |
| 10 | +# DUM-specific SSM parameter for use by downstream services such as the |
| 11 | +# Lambda authorizer. |
| 12 | +# |
| 13 | +# This ensures clear separation of ownership: |
| 14 | +# - Shared infrastructure manages the user pool and user lifecycle |
| 15 | +# - DUM manages only its own authentication client configuration |
| 16 | +# |
| 17 | +# IMPORTANT: |
| 18 | +# This module must not modify or impact the shared user pool or any |
| 19 | +# existing users/groups within it. |
| 20 | + |
| 21 | +data "aws_ssm_parameter" "pds_common_cognito_user_pool_id" { |
| 22 | + name = var.pds_common_cognito_user_pool_id_ssm_parameter_name |
| 23 | +} |
| 24 | + |
| 25 | +resource "aws_cognito_user_pool_client" "dum_auth_client" { |
| 26 | + name = var.cognito_user_pool_client_name |
| 27 | + user_pool_id = data.aws_ssm_parameter.pds_common_cognito_user_pool_id.value |
2 | 28 |
|
3 | | -resource "aws_cognito_user_pool" "nucleus_dum_cognito_user_pool" { |
4 | | - name = var.user_pool_name |
5 | | - |
6 | | - tags = var.tags |
7 | | - |
8 | | - admin_create_user_config { |
9 | | - invite_message_template { |
10 | | - email_subject = var.email_invitation_subject |
11 | | - email_message = var.email_invitation_message |
12 | | - sms_message = var.sms_text_invitation_message |
13 | | - } |
14 | | - |
15 | | - allow_admin_create_user_only = true |
16 | | - } |
17 | | - |
18 | | - account_recovery_setting { |
19 | | - recovery_mechanism { |
20 | | - name = "verified_email" |
21 | | - priority = 1 |
22 | | - } |
23 | | - } |
24 | | - |
25 | | - auto_verified_attributes = ["email"] |
26 | | - |
27 | | - user_attribute_update_settings { |
28 | | - attributes_require_verification_before_update = ["email"] |
29 | | - } |
30 | | - |
31 | | - username_configuration { |
32 | | - case_sensitive = false |
33 | | - } |
34 | | - |
35 | | - password_policy { |
36 | | - minimum_length = 8 |
37 | | - require_lowercase = true |
38 | | - require_uppercase = true |
39 | | - require_numbers = true |
40 | | - require_symbols = true |
41 | | - } |
42 | | -} |
43 | | - |
44 | | -resource "aws_ssm_parameter" "nucleus_dum_cognito_user_pool_id_parameter" { |
45 | | - name = format(var.ssm_param_cognito_user_pool_id) |
46 | | - type = "String" |
47 | | - value = aws_cognito_user_pool.nucleus_dum_cognito_user_pool.id |
48 | | - depends_on = [aws_cognito_user_pool.nucleus_dum_cognito_user_pool] |
49 | | - |
50 | | - tags = var.tags |
51 | | -} |
52 | | - |
53 | | -resource "aws_cognito_user_pool_client" "nucleus_dum_cognito_user_pool_client" { |
54 | | - name = "nucleus-dum-cognito-user-pool-client" |
55 | | - user_pool_id = aws_ssm_parameter.nucleus_dum_cognito_user_pool_id_parameter.value |
56 | 29 | generate_secret = false |
57 | 30 | prevent_user_existence_errors = "ENABLED" |
58 | | - callback_urls = ["http://localhost:3000"] |
| 31 | + callback_urls = var.callback_urls |
59 | 32 | allowed_oauth_flows_user_pool_client = true |
60 | 33 | allowed_oauth_flows = ["code"] |
61 | 34 | allowed_oauth_scopes = ["email", "openid"] |
62 | | - explicit_auth_flows = ["ALLOW_CUSTOM_AUTH", "ALLOW_REFRESH_TOKEN_AUTH", "ALLOW_USER_PASSWORD_AUTH", "ALLOW_USER_SRP_AUTH"] |
63 | | - supported_identity_providers = ["COGNITO"] |
| 35 | + |
| 36 | + explicit_auth_flows = [ |
| 37 | + "ALLOW_CUSTOM_AUTH", |
| 38 | + "ALLOW_REFRESH_TOKEN_AUTH", |
| 39 | + "ALLOW_USER_PASSWORD_AUTH", |
| 40 | + "ALLOW_USER_SRP_AUTH" |
| 41 | + ] |
| 42 | + |
| 43 | + supported_identity_providers = ["COGNITO"] |
64 | 44 |
|
65 | 45 | refresh_token_validity = 30 |
66 | 46 | access_token_validity = 60 |
67 | 47 | id_token_validity = 60 |
| 48 | + |
68 | 49 | token_validity_units { |
69 | 50 | refresh_token = "days" |
70 | 51 | access_token = "minutes" |
71 | 52 | id_token = "minutes" |
72 | 53 | } |
73 | 54 | } |
74 | 55 |
|
75 | | -resource "aws_ssm_parameter" "nucleus_dum_cognito_user_pool_client_id_parameter" { |
76 | | - name = format(var.ssm_param_cognito_user_pool_client_id) |
77 | | - type = "String" |
78 | | - value = aws_cognito_user_pool_client.nucleus_dum_cognito_user_pool_client.id |
79 | | - depends_on = [aws_cognito_user_pool_client.nucleus_dum_cognito_user_pool_client] |
| 56 | +resource "aws_ssm_parameter" "dum_cognito_auth_client_id_parameter" { |
| 57 | + name = var.dum_cognito_auth_client_id_ssm_parameter_name |
| 58 | + type = "String" |
| 59 | + value = aws_cognito_user_pool_client.dum_auth_client.id |
80 | 60 |
|
81 | 61 | tags = var.tags |
82 | 62 | } |
83 | | - |
84 | | -resource "aws_cognito_user_pool_domain" "nucleus_dum_cognito_user_pool_domain" { |
85 | | - domain = var.user_pool_domain |
86 | | - user_pool_id = aws_ssm_parameter.nucleus_dum_cognito_user_pool_id_parameter.value |
87 | | -} |
88 | | - |
89 | | -resource "aws_cognito_user_group" "nucleus_dum_cognito_user_groups" { |
90 | | - count = length(var.nucleus_dum_cognito_user_groups) |
91 | | - |
92 | | - name = var.nucleus_dum_cognito_user_groups[count.index].name |
93 | | - description = var.nucleus_dum_cognito_user_groups[count.index].description |
94 | | - user_pool_id = aws_ssm_parameter.nucleus_dum_cognito_user_pool_id_parameter.value |
95 | | -} |
96 | | - |
97 | | -resource "aws_cognito_user" "nucleus_dum_cognito_initial_users" { |
98 | | - count = length(var.nucleus_dum_cognito_initial_users) |
99 | | - |
100 | | - username = var.nucleus_dum_cognito_initial_users[count.index].username |
101 | | - user_pool_id = aws_ssm_parameter.nucleus_dum_cognito_user_pool_id_parameter.value |
102 | | - desired_delivery_mediums = ["EMAIL"] |
103 | | - enabled = true |
104 | | - message_action = "SUPPRESS" |
105 | | - password = var.nucleus_dum_cognito_initial_users[count.index].password |
106 | | - |
107 | | - attributes = { |
108 | | - email = var.nucleus_dum_cognito_initial_users[count.index].email |
109 | | - email_verified = true |
110 | | - } |
111 | | -} |
112 | | - |
113 | | -resource "aws_cognito_user_in_group" "nucleus_dum_cognito_initial_user_group_assignment" { |
114 | | - count = length(var.nucleus_dum_cognito_initial_users) |
115 | | - depends_on = [aws_cognito_user.nucleus_dum_cognito_initial_users] |
116 | | - |
117 | | - username = var.nucleus_dum_cognito_initial_users[count.index].username |
118 | | - group_name = var.nucleus_dum_cognito_initial_users[count.index].group |
119 | | - user_pool_id = aws_ssm_parameter.nucleus_dum_cognito_user_pool_id_parameter.value |
120 | | -} |
0 commit comments