Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "go.sum|^.secrets.baseline$",
"lines": null
},
"generated_at": "2025-11-28T08:21:42Z",
"generated_at": "2025-11-30T18:45:40Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -90,7 +90,7 @@
"hashed_secret": "a7c93faaa770c377154ea9d4d0d17a9056dbfa95",
"is_secret": false,
"is_verified": false,
"line_number": 194,
"line_number": 198,
"type": "Secret Keyword",
"verified_result": null
}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,15 @@ You need the following permissions to run this module.
| [ibm_cos_bucket.cos_bucket1](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/cos_bucket) | resource |
| [ibm_cos_bucket_lifecycle_configuration.cos_bucket_lifecycle](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/cos_bucket_lifecycle_configuration) | resource |
| [ibm_cos_bucket_object_lock_configuration.lock_configuration](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/cos_bucket_object_lock_configuration) | resource |
| [ibm_iam_access_group_policy.access_policy](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/iam_access_group_policy) | resource |
| [ibm_iam_access_group_policy.access_policy1](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/iam_access_group_policy) | resource |
| [ibm_iam_authorization_policy.policy](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/iam_authorization_policy) | resource |
| [ibm_resource_instance.cos_instance](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_instance) | resource |
| [ibm_resource_key.resource_keys](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_key) | resource |
| [ibm_resource_tag.cos_access_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
| [random_string.bucket_name_suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
| [time_sleep.wait_for_authorization_policy](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
| [ibm_iam_access_group.public_access_group](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/iam_access_group) | data source |

### Inputs

Expand All @@ -159,6 +162,7 @@ You need the following permissions to run this module.
| <a name="input_activity_tracker_read_data_events"></a> [activity\_tracker\_read\_data\_events](#input\_activity\_tracker\_read\_data\_events) | If set to `true`, all Object Storage bucket read events (i.e. downloads) are sent to Activity Tracker Event Routing. | `bool` | `true` | no |
| <a name="input_activity_tracker_write_data_events"></a> [activity\_tracker\_write\_data\_events](#input\_activity\_tracker\_write\_data\_events) | If set to `true`, all Object Storage bucket write events (i.e. uploads) are sent to Activity Tracker Event Routing. | `bool` | `true` | no |
| <a name="input_add_bucket_name_suffix"></a> [add\_bucket\_name\_suffix](#input\_add\_bucket\_name\_suffix) | Whether to add a randomly generated 4-character suffix to the bucket name. | `bool` | `true` | no |
| <a name="input_allow_public_access_to_buckets"></a> [allow\_public\_access\_to\_buckets](#input\_allow\_public\_access\_to\_buckets) | Set it to `true` to allow the cos bucket to be publicly accessible. | `bool` | `false` | no |
| <a name="input_archive_days"></a> [archive\_days](#input\_archive\_days) | The number of days before the `archive_type` rule action takes effect. Applies only if `create_cos_bucket` is set to `true`. Set to `null` if you specify a bucket location in `cross_region_location` because archive data is not supported with cross-region buckets. If null is passed, no lifecycle configuration will be added for bucket archival. | `number` | `null` | no |
| <a name="input_archive_filter_prefix"></a> [archive\_filter\_prefix](#input\_archive\_filter\_prefix) | Apply archive lifecycle rule to only objects with the following prefix. Applies to all objects by default. | `string` | `null` | no |
| <a name="input_archive_type"></a> [archive\_type](#input\_archive\_type) | The storage class or archive type to which you want the object to transition. Possible values are `Glacier` or `Accelerated`. Applies only if `create_cos_bucket` is set to `true`. | `string` | `"Glacier"` | no |
Expand Down
12 changes: 12 additions & 0 deletions ibm_catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@
"key": "bucket_name",
"required": true
},
{
"key": "allow_public_access_to_buckets"
},
{
"key": "provider_visibility",
"hidden": true,
Expand Down Expand Up @@ -793,6 +796,9 @@
"key": "bucket_name",
"required": true
},
{
"key": "allow_public_access_to_buckets"
},
{
"key": "add_bucket_name_suffix"
},
Expand Down Expand Up @@ -1247,6 +1253,9 @@
"key": "bucket_name",
"required": true
},
{
"key": "allow_public_access_to_buckets"
},
{
"key": "provider_visibility",
"hidden": true,
Expand Down Expand Up @@ -1736,6 +1745,9 @@
"key": "bucket_name",
"required": true
},
{
"key": "allow_public_access_to_buckets"
},
{
"key": "add_bucket_name_suffix"
},
Expand Down
43 changes: 41 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ resource "ibm_iam_authorization_policy" "policy" {
}
}

# use a data lookup to get the ID of the "Public Access" IAM access group
data "ibm_iam_access_group" "public_access_group" {
access_group_name = "geretain-public-access"
}

# Create random string which is added to COS bucket name as a suffix
resource "random_string" "bucket_name_suffix" {
count = var.add_bucket_name_suffix ? 1 : 0
Expand All @@ -121,10 +126,15 @@ resource "random_string" "bucket_name_suffix" {
# - Monitoring
# - Activity Tracking
# - Versioning

locals {
random_suffix = random_string.bucket_name_suffix[0].result
}

resource "ibm_cos_bucket" "cos_bucket" {
count = (var.kms_encryption_enabled && var.create_cos_bucket) ? 1 : 0
depends_on = [time_sleep.wait_for_authorization_policy]
bucket_name = var.add_bucket_name_suffix ? "${var.bucket_name}-${random_string.bucket_name_suffix[0].result}" : var.bucket_name
bucket_name = var.add_bucket_name_suffix ? "${var.bucket_name}-${local.random_suffix}" : var.bucket_name
resource_instance_id = local.cos_instance_id
region_location = var.region
cross_region_location = var.cross_region_location
Expand Down Expand Up @@ -174,16 +184,31 @@ resource "ibm_cos_bucket" "cos_bucket" {
}
}

# create an IAM access policy to granting public access to this bucket
resource "ibm_iam_access_group_policy" "access_policy" {
count = (var.allow_public_access_to_buckets && var.kms_encryption_enabled && var.create_cos_bucket) ? 1 : 0
access_group_id = data.ibm_iam_access_group.public_access_group.groups[0].id
roles = ["Object Reader"]

resources {
service = "cloud-object-storage"
resource_type = "bucket"
resource_instance_id = local.cos_instance_guid
resource = var.add_bucket_name_suffix ? "${var.bucket_name}-${local.random_suffix}" : var.bucket_name
}
}

# Create COS bucket with:
# - Retention
# - Monitoring
# - Activity Tracking
# - Versioning
# Create COS bucket without:
# - Encryption

resource "ibm_cos_bucket" "cos_bucket1" {
count = (!var.kms_encryption_enabled && var.create_cos_bucket) ? 1 : 0
bucket_name = var.add_bucket_name_suffix ? "${var.bucket_name}-${random_string.bucket_name_suffix[0].result}" : var.bucket_name
bucket_name = var.add_bucket_name_suffix ? "${var.bucket_name}-${local.random_suffix}" : var.bucket_name
depends_on = [time_sleep.wait_for_authorization_policy]
resource_instance_id = local.cos_instance_id
region_location = var.region
Expand Down Expand Up @@ -233,6 +258,20 @@ resource "ibm_cos_bucket" "cos_bucket1" {
}
}

# create an IAM access policy to granting public access to this bucket
resource "ibm_iam_access_group_policy" "access_policy1" {
count = (var.allow_public_access_to_buckets && !var.kms_encryption_enabled && var.create_cos_bucket) ? 1 : 0
access_group_id = data.ibm_iam_access_group.public_access_group.groups[0].id
roles = ["Object Reader"]

resources {
service = "cloud-object-storage"
resource_type = "bucket"
resource_instance_id = local.cos_instance_guid
resource = var.add_bucket_name_suffix ? "${var.bucket_name}-${local.random_suffix}" : var.bucket_name
}
}

locals {
expiration_or_archiving_or_noncurrent_version_expiration_rule_enabled = (length(local.expire_enabled) != 0 || length(local.archive_enabled) != 0 || length(local.noncurrent_version_expiration_enabled) != 0 || length(local.abort_multipart_enabled) != 0)

Expand Down
2 changes: 1 addition & 1 deletion modules/buckets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ You need the following permissions to run this module.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_bucket_configs"></a> [bucket\_configs](#input\_bucket\_configs) | The Object Storage bucket configurations. | <pre>list(object({<br/> access_tags = optional(list(string), [])<br/> add_bucket_name_suffix = optional(bool, true)<br/> bucket_name = string<br/> kms_encryption_enabled = optional(bool, true)<br/> kms_guid = optional(string, null)<br/> kms_key_crn = optional(string, null)<br/> skip_iam_authorization_policy = optional(bool, false)<br/> management_endpoint_type = optional(string, "public")<br/> cross_region_location = optional(string, null)<br/> storage_class = optional(string, "smart")<br/> region_location = optional(string, null)<br/> resource_instance_id = string<br/> force_delete = optional(bool, true)<br/> single_site_location = optional(string, null)<br/> hard_quota = optional(number, null)<br/> object_locking_enabled = optional(bool, false)<br/> object_lock_duration_days = optional(number, 0)<br/> object_lock_duration_years = optional(number, 0)<br/><br/> activity_tracking = optional(object({<br/> read_data_events = optional(bool, true)<br/> write_data_events = optional(bool, true)<br/> management_events = optional(bool, true)<br/> }))<br/> archive_rule = optional(object({<br/> enable = optional(bool, false)<br/> days = optional(number, null)<br/> type = optional(string, "Glacier")<br/> archive_filter_prefix = optional(string, null)<br/> }))<br/> expire_rule = optional(object({<br/> enable = optional(bool, false)<br/> days = optional(number, null)<br/> expire_filter_prefix = optional(string, null)<br/> }))<br/> abort_multipart_enabled_rule = optional(object({<br/> enable = optional(bool, false)<br/> days = optional(number, null)<br/> abort_multipart_filter_prefix = optional(string, null)<br/> }))<br/> noncurrent_version_expiration_rule = optional(object({<br/> enable = optional(bool, false)<br/> days = optional(number, null)<br/> noncurrent_version_expiration_filter_prefix = optional(string, null)<br/> }))<br/> metrics_monitoring = optional(object({<br/> usage_metrics_enabled = optional(bool, true)<br/> request_metrics_enabled = optional(bool, true)<br/> metrics_monitoring_crn = optional(string, null)<br/> }))<br/> object_versioning = optional(object({<br/> enable = optional(bool, false)<br/> }))<br/> retention_rule = optional(object({<br/> default = optional(number, 90)<br/> maximum = optional(number, 350)<br/> minimum = optional(number, 90)<br/> permanent = optional(bool, false)<br/> }))<br/> cbr_rules = optional(list(object({<br/> description = string<br/> account_id = string<br/> rule_contexts = list(object({<br/> attributes = optional(list(object({<br/> name = string<br/> value = string<br/> }))) }))<br/> enforcement_mode = string<br/> tags = optional(list(object({<br/> name = string<br/> value = string<br/> })), [])<br/> operations = optional(list(object({<br/> api_types = list(object({<br/> api_type_id = string<br/> }))<br/> })))<br/> })), [])<br/><br/> }))</pre> | n/a | yes |
| <a name="input_bucket_configs"></a> [bucket\_configs](#input\_bucket\_configs) | The Object Storage bucket configurations. | <pre>list(object({<br/> access_tags = optional(list(string), [])<br/> add_bucket_name_suffix = optional(bool, true)<br/> bucket_name = string<br/> allow_public_access_to_buckets = optional(bool, false)<br/> kms_encryption_enabled = optional(bool, true)<br/> kms_guid = optional(string, null)<br/> kms_key_crn = optional(string, null)<br/> skip_iam_authorization_policy = optional(bool, false)<br/> management_endpoint_type = optional(string, "public")<br/> cross_region_location = optional(string, null)<br/> storage_class = optional(string, "smart")<br/> region_location = optional(string, null)<br/> resource_instance_id = string<br/> force_delete = optional(bool, true)<br/> single_site_location = optional(string, null)<br/> hard_quota = optional(number, null)<br/> object_locking_enabled = optional(bool, false)<br/> object_lock_duration_days = optional(number, 0)<br/> object_lock_duration_years = optional(number, 0)<br/><br/> activity_tracking = optional(object({<br/> read_data_events = optional(bool, true)<br/> write_data_events = optional(bool, true)<br/> management_events = optional(bool, true)<br/> }))<br/> archive_rule = optional(object({<br/> enable = optional(bool, false)<br/> days = optional(number, null)<br/> type = optional(string, "Glacier")<br/> archive_filter_prefix = optional(string, null)<br/> }))<br/> expire_rule = optional(object({<br/> enable = optional(bool, false)<br/> days = optional(number, null)<br/> expire_filter_prefix = optional(string, null)<br/> }))<br/> abort_multipart_enabled_rule = optional(object({<br/> enable = optional(bool, false)<br/> days = optional(number, null)<br/> abort_multipart_filter_prefix = optional(string, null)<br/> }))<br/> noncurrent_version_expiration_rule = optional(object({<br/> enable = optional(bool, false)<br/> days = optional(number, null)<br/> noncurrent_version_expiration_filter_prefix = optional(string, null)<br/> }))<br/> metrics_monitoring = optional(object({<br/> usage_metrics_enabled = optional(bool, true)<br/> request_metrics_enabled = optional(bool, true)<br/> metrics_monitoring_crn = optional(string, null)<br/> }))<br/> object_versioning = optional(object({<br/> enable = optional(bool, false)<br/> }))<br/> retention_rule = optional(object({<br/> default = optional(number, 90)<br/> maximum = optional(number, 350)<br/> minimum = optional(number, 90)<br/> permanent = optional(bool, false)<br/> }))<br/> cbr_rules = optional(list(object({<br/> description = string<br/> account_id = string<br/> rule_contexts = list(object({<br/> attributes = optional(list(object({<br/> name = string<br/> value = string<br/> }))) }))<br/> enforcement_mode = string<br/> tags = optional(list(object({<br/> name = string<br/> value = string<br/> })), [])<br/> operations = optional(list(object({<br/> api_types = list(object({<br/> api_type_id = string<br/> }))<br/> })))<br/> })), [])<br/><br/> }))</pre> | n/a | yes |

### Outputs

Expand Down
17 changes: 9 additions & 8 deletions modules/buckets/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ module "buckets" {
for index, bucket in var.bucket_configs :
bucket.bucket_name => bucket
}
depends_on = [time_sleep.wait_for_authorization_policy]
source = "../../"
bucket_name = each.value.bucket_name
create_cos_instance = false
add_bucket_name_suffix = each.value.add_bucket_name_suffix
skip_iam_authorization_policy = true
existing_cos_instance_id = each.value.resource_instance_id
region = each.value.region_location
depends_on = [time_sleep.wait_for_authorization_policy]
source = "../../"
bucket_name = each.value.bucket_name
allow_public_access_to_buckets = each.value.allow_public_access_to_buckets
create_cos_instance = false
add_bucket_name_suffix = each.value.add_bucket_name_suffix
skip_iam_authorization_policy = true
existing_cos_instance_id = each.value.resource_instance_id
region = each.value.region_location

cross_region_location = each.value.cross_region_location
single_site_location = each.value.single_site_location
Expand Down
Loading