Create the IAM Module

The IAM module contains the configuration for the groups and policies. Define each group and policy as a resource in the configuration, and declare the required variables.

About Policies and Groups

To control user access to the resources in your topology, create groups, and assign policies to grant the required permissions to each group.

The following table lists the groups and permissions required, typically, for a multitier architecture:

Group Permissions
DBAdmins
  • Read all the resources in the tenancy.
  • Manage the database resources.
IAMAdminManagers
  • Manage users.
  • Manage the Administrators and NetSecAdmins groups.

Note: Oracle creates the Administrators group when you subscribe to Oracle Cloud. The users in this group have full access to all the resources in the tenancy, including managing users and groups. Limit the membership to this group.

IAMManagers
  • Manage users.
  • Manage all the groups except Administrators and NetSecAdmins.
NetworkAdmins
  • Read all the resources in the tenancy.
  • Manage all the networking resources, except security lists, internet gateways, IPSec VPN connections, and customer-premises equipment.
NetSecAdmins
  • Read all the resources in the tenancy.
  • Manage security lists, internet gateways, customer-premises equipment, IPSec VPN connections, and load balancers.
  • Use all the virtual network resources.
ReadOnly View and inspect the tenancy. This group is for users who aren't expected to create or manage any resources (for example, auditors and trainees).
StorageAdmins
  • Read all the resources in the tenancy.
  • Manage the object storage and block volume resources.
SysAdmins
  • Read all the resources in the tenancy.
  • Manage the compute and storage resources.
  • Manage compartments.
  • Use load balancers, subnets, and VNICs.

Define the Groups and Policies

Create the Terraform configuration files that define the required Oracle Cloud Infrastructure Identity and Access Management policies and groups.

Complete the following steps in the iam subdirectory:

  1. Create a text file named variables.tf, and paste the following code in the file.
    This code declares the variables used in this module.
    variable "tenancy_ocid" {}
    variable "app_tag" {}
    variable "environment" {}
  2. Create a text file named groups.tf, and paste the following code in the file.
    resource "oci_identity_group" "db_admins" {
      description = "Group for users allowed to manage the databases in the tenancy."
      name        = "DBAdmins.grp"
    }
    resource "oci_identity_group" "iam_admin_managers" {
      description = "Group for users allowed to modify the Administrators and NetSecAdmins group."
      name        = "IAMAdminManagers.grp"
    }
    
    resource "oci_identity_group" "iam_managers" {
      description = "Group for users allowed to modify all users and groups except the Administrators and NetSecAdmin groups."
      name        = "IAMManagers.grp"
    }
    
    resource "oci_identity_group" "net_sec_admins" {
      description = "Administrators of the VCNs, but restricted from the following resources: vcns, subnets, route-tables, dhcp-options, drgs, drg-attachments, vnics, vnic-attachments"
      name        = "NetSecAdmins.grp"
    }
    
    resource "oci_identity_group" "network_admins" {
      description = "Administrators of the VCNs, but restricted from the following resources: security-lists, internet-gateways, cpes, ipsec-connections"
      name        = "NetworkAdmins.grp"
    }
    
    resource "oci_identity_group" "read_only" {
      description = "Groups for users allowed to view and inspect the tenancy configuration; for example, trainees"
      name        = "ReadOnly.grp"
    }
    
    resource "oci_identity_group" "storage_admins" {
      description = "Group for users allowed manage the Storage resources in the tenancy."
      name        = "StorageAdmins.grp"
    }
    
    resource "oci_identity_group" "sys_admins" {
      description = "Group for users allowed manage the Compute and Storage resources in the tenancy. Tenant administrators should be in this group."
      name        = "SysAdmins.grp"
    }
  3. Create a text file named policies.tf, and paste the following code in the file.
    resource "oci_identity_policy" "iam_admin_managers" {
      name           = "IAMAdminManagers.pl"
      description    = "IAMAdminManagers.pl"
      compartment_id = "${var.tenancy_ocid}"
    
      statements = [
        "ALLOW GROUP ${oci_identity_group.iam_admin_managers.name} to read users IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.iam_admin_managers.name} to read groups IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.iam_admin_managers.name} to manage users IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.iam_admin_managers.name} to manage groups IN TENANCY where target.group.name = 'Administrators'",
        "ALLOW GROUP ${oci_identity_group.iam_admin_managers.name} to manage groups IN TENANCY where target.group.name = '${oci_identity_group.net_sec_admins.name}'",
      ]
    }
    
    resource "oci_identity_policy" "iam_managers" {
      name           = "IAMManagers.pl"
      description    = "IAMManagers.pl"
      compartment_id = "${var.tenancy_ocid}"
    
      statements = [
        "ALLOW GROUP ${oci_identity_group.iam_managers.name} to read users IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.iam_managers.name} to read groups IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.iam_managers.name} to manage users IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.iam_managers.name} to manage groups IN TENANCY where all {target.group.name ! = 'Administrators', target.group.name ! = '${oci_identity_group.net_sec_admins.name}'}",
      ]
    }
    
    resource "oci_identity_policy" "sys_admins" {
      name           = "SysAdmins.pl"
      description    = "SysAdmins.pl"
      compartment_id = "${var.tenancy_ocid}"
    
      statements = [
        "ALLOW GROUP ${oci_identity_group.sys_admins.name} to manage instance-family IN TENANCY where all {target.compartment.name=/*/, target.compartment.name!=/${var.app_tag}_${var.environment}_networks/}",
        "ALLOW GROUP ${oci_identity_group.sys_admins.name} to manage object-family IN TENANCY where all {target.compartment.name=/*/, target.compartment.name!=/${var.app_tag}_${var.environment}_networks/}",
        "ALLOW GROUP ${oci_identity_group.sys_admins.name} to manage volume-family IN TENANCY where all {target.compartment.name=/*/ , target.compartment.name!=/${var.app_tag}_${var.environment}_networks/}",
        "ALLOW GROUP ${oci_identity_group.sys_admins.name} to use load-balancers IN TENANCY where all {target.compartment.name=/*/ , target.compartment.name!=/${var.app_tag}_${var.environment}_networks/}",
        "ALLOW GROUP ${oci_identity_group.sys_admins.name} to use subnets IN TENANCY where target.compartment.name=/${var.app_tag}_${var.environment}_networks/",
        "ALLOW GROUP ${oci_identity_group.sys_admins.name} to use vnics IN TENANCY where target.compartment.name=/${var.app_tag}_${var.environment}_networks/",
        "ALLOW GROUP ${oci_identity_group.sys_admins.name} to use vnic-attachments IN TENANCY where target.compartment.name=/${var.app_tag}_${var.environment}_networks/",
        "ALLOW GROUP ${oci_identity_group.sys_admins.name} to manage compartments in Tenancy where all {target.compartment.name=/*/ , target.compartment.name!=/${var.app_tag}_${var.environment}_networks/, target.compartment.name!=/Shared-Infra-Services/}",
        "ALLOW GROUP ${oci_identity_group.sys_admins.name} to read all-resources IN TENANCY",
      ]
    }
    
    resource "oci_identity_policy" "storage_admins" {
      name           = "StorageAdmins.pl"
      description    = "StorageAdmins.pl"
      compartment_id = "${var.tenancy_ocid}"
    
      statements = [
        "ALLOW GROUP ${oci_identity_group.storage_admins.name} to manage object-family IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.storage_admins.name} to manage volume-family IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.storage_admins.name} to read all-resources IN TENANCY",
      ]
    }
    
    resource "oci_identity_policy" "db_admins" {
      name           = "DBAdmins.pl"
      description    = "DBAdmins.pl"
      compartment_id = "${var.tenancy_ocid}"
    
      statements = [
        "ALLOW GROUP ${oci_identity_group.db_admins.name} manage database-family IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.db_admins.name} read all-resources IN TENANCY",
      ]
    }
    
    resource "oci_identity_policy" "network_admins" {
      name           = "NetworkAdmins.pl"
      description    = "NetworkAdmins.pl"
      compartment_id = "${var.tenancy_ocid}"
    
      statements = [
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage vcns IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage subnets IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage route-tables IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage dhcp-options IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage drgs IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage cross-connects IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage cross-connect-groups IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage virtual-circuits IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage vnics IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage vnic-attachments IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to manage load-balancers IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to use virtual-network-family IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.network_admins.name} to read all-resources IN TENANCY",
      ]
    }
    
    resource "oci_identity_policy" "net_sec_admins" {
      name           = "NetSecAdmins.pl"
      description    = "NetSecAdmins.pl"
      compartment_id = "${var.tenancy_ocid}"
    
      statements = [
        "ALLOW GROUP ${oci_identity_group.net_sec_admins.name} to manage security-lists IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.net_sec_admins.name} to manage internet-gateways IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.net_sec_admins.name} to manage cpes IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.net_sec_admins.name} to manage ipsec-connections IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.net_sec_admins.name} to use virtual-network-family IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.net_sec_admins.name} to manage load-balancers IN TENANCY",
        "ALLOW GROUP ${oci_identity_group.net_sec_admins.name} to read all-resources IN TENANCY",
      ]
    }
    
    resource "oci_identity_policy" "read_only" {
      name           = "ReadOnly.pl"
      description    = "ReadOnly.pl"
      compartment_id = "${var.tenancy_ocid}"
    
      statements = ["ALLOW GROUP ${oci_identity_group.read_only.name} to read all-resources IN TENANCY"]
    }
  4. Create a file named iam_outputs.tf, and paste the following code in the file.
    This code causes Terraform to display the IDs of the resources, after they are created.
    output "db_admins_id" {
      value = "${oci_identity_group.db_admins.id}"
    }
    
    output "iam_admin_managers_id" {
      value = "${oci_identity_group.iam_admin_managers.id}"
    }
    
    output "iam_managers_id" {
      value = "${oci_identity_group.iam_managers.id}"
    }
    
    output "net_sec_admins_id" {
      value = "${oci_identity_group.net_sec_admins.id}"
    }
    
    output "network_admins_id" {
      value = "${oci_identity_group.network_admins.id}"
    }
    
    output "read_only_id" {
      value = "${oci_identity_group.read_only.id}"
    }
    
    output "storage_admins_id" {
      value = "${oci_identity_group.storage_admins.id}"
    }
    
    output "sys_admins_id" {
      value = "${oci_identity_group.sys_admins.id}"
    }