1+ # #############################################################################
2+ # Resource group
3+ # #############################################################################
4+
15module "resource_group" {
26 source = " terraform-ibm-modules/resource-group/ibm"
37 version = " 1.3.0"
@@ -6,6 +10,10 @@ module "resource_group" {
610 existing_resource_group_name = var. resource_group
711}
812
13+ # #############################################################################
14+ # Key Protect instance and root key
15+ # #############################################################################
16+
917module "key_protect" {
1018 source = " terraform-ibm-modules/kms-all-inclusive/ibm"
1119 version = " 5.1.25"
@@ -25,7 +33,11 @@ module "key_protect" {
2533 ]
2634}
2735
28- module "event_notification" {
36+ # #############################################################################
37+ # Event Notifications
38+ # #############################################################################
39+
40+ module "event_notifications" {
2941 source = " terraform-ibm-modules/event-notifications/ibm"
3042 version = " 2.7.0"
3143 resource_group_id = module. resource_group . resource_group_id
@@ -35,14 +47,13 @@ module "event_notification" {
3547 region = var. en_region
3648}
3749
50+ # s2s auth policy required for Secrets Manager to manage Event Notifications service credentials
3851resource "ibm_iam_authorization_policy" "en_policy" {
3952 source_service_name = " secrets-manager"
4053 roles = [" Key Manager" ]
4154 target_service_name = " event-notifications"
42- target_resource_instance_id = module. event_notification . guid
43- description = " Allow the Secret manager Key Manager role access to event-notifications with guid ${ module . event_notification . guid } ."
44- # Scope of policy now includes the key, so ensure to create new policy before
45- # destroying old one to prevent any disruption to every day services.
55+ target_resource_instance_id = module. event_notifications . guid
56+ description = " Grant Secret Manager a 'Key Manager' role to the Event Notifications instance ${ module . event_notifications . guid } for managing service credentials."
4657 lifecycle {
4758 create_before_destroy = true
4859 }
@@ -53,6 +64,10 @@ resource "time_sleep" "wait_for_en_policy" {
5364 create_duration = " 30s"
5465}
5566
67+ # #############################################################################
68+ # Secrets Manager
69+ # #############################################################################
70+
5671module "secrets_manager" {
5772 depends_on = [time_sleep . wait_for_en_policy ]
5873 source = " ../.."
@@ -65,30 +80,29 @@ module "secrets_manager" {
6580 is_hpcs_key = false
6681 kms_key_crn = module. key_protect . keys [" ${ var . prefix } -sm.${ var . prefix } -sm-key" ]. crn
6782 enable_event_notification = true
68- existing_en_instance_crn = module. event_notification . crn
83+ existing_en_instance_crn = module. event_notifications . crn
6984 secrets = [
85+ # Example creating new secrets group with secrets in it
7086 {
7187 secret_group_name = " ${ var . prefix } -secret-group"
72- secrets = [{
73- secret_name = " ${ var . prefix } -kp-key-crn"
74- secret_type = " arbitrary"
75- secret_payload_password = module.key_protect.keys[" ${ var . prefix } -sm.${ var . prefix } -sm-key" ].crn
76- },
88+ secrets = [
89+ # Example creating Event Notifications service credential secret
7790 {
78- # Arbitrary service credential for source service event notifications, with role Event-Notification-Publisher
7991 secret_name = " ${ var . prefix } -service-credential"
8092 secret_type = " service_credentials" # checkov:skip=CKV_SECRET_6
81- secret_description = " Created by secrets-manager-module complete example"
82- service_credentials_source_service_crn = module.event_notification .crn
93+ secret_description = " Created by secrets-manager-module advanced example"
94+ service_credentials_source_service_crn = module.event_notifications .crn
8395 service_credentials_source_service_role_crn = " crn:v1:bluemix:public:event-notifications::::serviceRole:Event-Notification-Publisher"
8496 },
97+ # Example creating arbitrary secret
8598 {
86- secret_name = " ${ var . prefix } -custom-service-credential "
99+ secret_name = " ${ var . prefix } -arbitrary-example "
87100 secret_type = " arbitrary"
88101 secret_payload_password = var.ibmcloud_api_key
89102 }
90103 ]
91104 },
105+ # Example creating secret in existing secret group
92106 {
93107 secret_group_name = " default"
94108 existing_secret_group = true
@@ -103,18 +117,19 @@ module "secrets_manager" {
103117}
104118
105119# #############################################################################
106- # Code Engine Project
120+ # Code Engine configuration
121+ # (required to use create a custom credential)
107122# #############################################################################
123+
124+ # Create new code engine project
108125module "code_engine_project" {
109126 source = " terraform-ibm-modules/code-engine/ibm//modules/project"
110127 version = " 4.5.13"
111128 name = " ${ var . prefix } -project"
112129 resource_group_id = module. resource_group . resource_group_id
113130}
114131
115- # #############################################################################
116- # Code Engine Secret
117- # #############################################################################
132+ # Create new code engine secret
118133locals {
119134 registry_hostname = " private.de.icr.io"
120135 output_image = " ${ local . registry_hostname } /${ resource . ibm_cr_namespace . rg_namespace . name } /custom-engine-job"
@@ -133,19 +148,13 @@ module "code_engine_secret" {
133148 }
134149}
135150
136- # #############################################################################
137- # Container Registry Namespace
138- # #############################################################################
151+ # Create new Container Registry namespace
139152resource "ibm_cr_namespace" "rg_namespace" {
140153 name = " ${ var . prefix } -crn"
141154 resource_group_id = module. resource_group . resource_group_id
142155}
143156
144- # #############################################################################
145- # Code Engine Build
146- # #############################################################################
147-
148- # For example the region is hardcoded to us-south in order to hardcode the output image and region for creating Code Engine Project and build
157+ # Build example Go application in Code Engine project which dynamically generates User IBM Cloud IAM API Keys
149158module "code_engine_build" {
150159 source = " terraform-ibm-modules/code-engine/ibm//modules/build"
151160 version = " 4.5.13"
@@ -161,10 +170,7 @@ module "code_engine_build" {
161170 output_image = local. output_image
162171}
163172
164- # #############################################################################
165- # Code Engine Job
166- # #############################################################################
167-
173+ # Pull the sample job config from github
168174data "http" "job_config" {
169175 url = " https://raw.githubusercontent.com/IBM/secrets-manager-custom-credentials-providers/refs/heads/main/ibmcloud-iam-user-apikey-provider-go/job_config.json"
170176 request_headers = {
@@ -176,6 +182,7 @@ locals {
176182 job_env_variables = jsondecode (data. http . job_config . response_body ). job_env_variables
177183}
178184
185+ # Run the Code Engine job
179186module "code_engine_job" {
180187 depends_on = [module . code_engine_build ]
181188 source = " terraform-ibm-modules/code-engine/ibm//modules/job"
@@ -194,7 +201,7 @@ module "code_engine_job" {
194201}
195202
196203# #############################################################################
197- # Custom Credential Engine and secret
204+ # Create Custom Credential engine
198205# #############################################################################
199206
200207module "custom_credential_engine" {
@@ -213,8 +220,12 @@ module "custom_credential_engine" {
213220 iam_credential_secret_name = " ${ var . prefix } -test-iam-secret"
214221}
215222
216- # Currently the main module cannot be called again as some of the count for resources depends on a computable input existing_en_instance_crn which will give error if the value is not available during planning
217- # As a workaround the secret manager secret is directly being created via module call
223+ # #############################################################################
224+ # Create Custom Credential secret
225+ # (using secrets-manager-secret to create the custom credential secret as it
226+ # can only be done after the Custom Credential engine is configured)
227+ # #############################################################################
228+
218229module "secret_manager_custom_credential" {
219230 depends_on = [module . secrets_manager , module . custom_credential_engine ]
220231 source = " terraform-ibm-modules/secrets-manager-secret/ibm"
0 commit comments