More Info:
Instance principals should be used instead of API keys for compute workloads. Instance principals eliminate the need to store and rotate credentials, reducing the risk of key leakage.Risk Level
MediumAddress
Compliance, SecurityCompliance Standards
- APRA CPS 234 (Australia)
- BSI C5 (Germany)
- Brazil LGPD
- CCPA / CPRA (California)
- CIS AWS
- CIS Critical Security Controls v8
- CMMC 2.0
- CSA Cloud Controls Matrix v4
- Cloudanix Best Practice
- DPDPA
- Digital Operational Resilience Act (EU)
- Essential 8
- GDPR
- HIPAA
- ISO/IEC 27017
- ISO/IEC 27018
- ISO/IEC 27701
- KSA PDPL
- MAS Technology Risk Management (Singapore)
- MITRE ATT&CK (Cloud)
- NIS2 Directive
- NIST
- NIST SP 800-171
- NYDFS 23 NYCRR 500
- SOC2
- SWIFT Customer Security Controls Framework
- Sarbanes-Oxley IT General Controls
- UK NCSC Cyber Assessment Framework
Triage and Remediation
- Remediation
Remediation
Using Console
Using Console
Below are the steps to replace API key–based access with Instance Principals for an OCI IAM-based monitoring setup, using only the OCI Console wherever possible. This assumes monitoring code/agent is running on a Compute instance.
For Instance Principals:Example for CLI commands in scripts:No API keys, user OCID, fingerprint, or private keys are needed when using
If you tell me which exact monitoring component you’re using (e.g., OCI Alarms + custom script, OCI Management Agent, Prometheus exporter, etc.), I can give you very specific commands/config lines to change.
1. Identify what is currently using API Keys
- In the Console, go to Identity & Security > Identity > Users.
- Open the user whose API keys are being used for monitoring calls.
- Under API Keys, confirm keys exist and are used by the monitoring scripts/agent (e.g., in
~/.oci/configon the instance).
2. Create (or verify) a Dynamic Group for the Monitoring Instances
- In the Console, go to Identity & Security > Identity > Dynamic Groups.
- Click Create Dynamic Group.
- Give it a name like
monitoring-instances-dg. - Under Matching Rule, add a rule to include the specific instance(s) running monitoring. For example:
- By compartment:
- Or by instance OCID:
- By compartment:
- Click Create.
3. Create IAM Policy to Allow Instance Principals to Do the Same Monitoring Actions
Determine what services the monitoring function needs (for example: Monitoring, Logging, Events, or others).- In the Console, go to Identity & Security > Identity > Policies.
- Choose the compartment that will contain the policy (often the root compartment or the compartment containing your monitoring resources).
- Click Create Policy.
- Name it something like
monitoring-instance-principals-policy. - Add statements to allow the dynamic group to access required services.
- For Monitoring (read metrics / push custom metrics):
- For Logging (if your monitoring needs to write logs):
- For Events or Alarms (if needed):
read(list/get)use(read + create/update in some cases)manage(full control, only if absolutely required).
4. Update the Monitoring Code/Agent on the Instance to Use Instance Principals
This step is done on the instance itself (SSH), but doesn’t require console changes other than what you’ve already done.4.1. If using OCI CLI or SDK in scripts
Your current config file likely uses:- You can either remove the profile OR create a new one and reference
--auth instance_principal.
--auth instance_principal.4.2. If using the Monitoring/Management Agent
- Ensure the Management Agent’s host (the compute instance) is part of the dynamic group you created.
- The agent itself typically uses instance principals or resource principals automatically once IAM is correctly configured.
- Verify by checking the agent configuration or documentation for your specific monitoring integration; ensure no user-level API keys are configured in the agent config files.
5. Test and Then Remove API Keys
-
From the instance, run a simple call using instance principals (e.g., list metrics):
-
Confirm:
- Command succeeds.
- Monitoring data/alarms/logs are still working as expected.
-
When confirmed:
- Go to Identity & Security > Identity > Users.
- Open the user previously used.
- Under API Keys, click the trash icon next to the now-unused API key(s).
- Confirm deletion.
If you tell me which exact monitoring component you’re using (e.g., OCI Alarms + custom script, OCI Management Agent, Prometheus exporter, etc.), I can give you very specific commands/config lines to change.
Using CLI
Using CLI
Below are the practical steps to move from using user/API keys to using Instance Principals for OCI Monitoring via the OCI CLI.
If this returns regions, instance principals are working. If it fails with auth errors, continue the next steps (dynamic group + policy).
Save this as Save as Or by compartment:Save the dynamic group OCID if needed:
Policies can be created in root or in a specific compartment.Create the policy:If your monitoring is limited to a specific compartment, adjust:
you will switch to List metrics:Manage alarms (example – list alarms):Use
1. Confirm Your Instance Has Instance Principals Enabled
If your Compute instance was created normally with the default settings, instance principals are usually available automatically via the instance’s instance metadata / resource principal token. No extra agent is required.On the instance, verify the CLI can see a resource principal:2. Create a Dynamic Group for Your Instances
You must group your instances in a Dynamic Group so IAM policies can be applied to them.2.1. Get Your Compartment OCID (where the instance lives)
COMPARTMENT_OCID.2.2. Get Your Instance OCID (optional; or use conditions by compartment/tag)
INSTANCE_OCID (if you want to target this specific instance).2.3. Create Dynamic Group
You can define the dynamic group by instance OCID:3. Create IAM Policies to Allow Monitoring Operations
You now allow this dynamic group (i.e., the instances) to use the Monitoring APIs.Determine your tenancy OCID (root compartment):3.1. Example Policy (Root Compartment)
Replace<TENANCY_NAME> and use your dynamic group name.Create a policy file monitoring-policy.txt:4. Use OCI CLI with Instance Principals for Monitoring
On the instance:4.1. Stop Using User/API Key Profiles
If your scripts currently use:--auth instance_principal and omit user config entirely.4.2. Example CLI Calls with Instance Principals
Post custom metrics:--auth instance_principal in all monitoring-related OCI CLI commands executed from the instance.5. Remove Old API Keys (Clean-Up)
Once your monitoring scripts work with instance principals:-
Identify the user whose API key was used:
-
Delete the old API key:
Using Python
Using Python
Below are concise, step‑by‑step instructions to move from API keys to Instance Principals for OCI IAM (for Monitoring) using Python.
Key points:
If you paste a short snippet of your current API‑key‑based Python code, I can show you the exact before/after using Instance Principals.
1. Prerequisites
- Your Python code runs on an OCI Compute instance.
- The instance is in a compartment where you can create IAM policies and dynamic groups.
- Python
ociSDK installed:
2. Create a Dynamic Group
- In OCI Console, go to: Identity & Security → Identity → Dynamic Groups → Create Dynamic Group.
-
Define:
- Name: e.g.,
dg-monitoring-instance - Description: e.g.,
Dynamic group for monitoring Python scripts using instance principals
- Name: e.g.,
-
In Matching Rules, target the instance(s) where your Python script runs. Example rule (by instance OCID):
or by compartment:
- Click Create.
3. Create IAM Policies for the Dynamic Group
- In OCI Console, go to: Identity & Security → Identity → Policies → Create Policy.
- Select the compartment where Monitoring is used (or the root compartment if appropriate).
-
Example policy statements (adjust compartments as needed):
To read metrics (Monitoring):
To use Monitoring Queries and Alarms (if needed):
- Save the policy.
4. Remove API Key Usage from Python Code
Remove any code that:- Builds config from
~/.oci/configwith user keys (e.g.,oci.config.from_file()usinguser,fingerprint,key_file). - Explicitly sets API key details in code.
5. Use Instance Principals in the Python SDK
In your Python script, useInstancePrincipalsSecurityTokenSigner instead of user API keys.Minimal example for Monitoring:configonly needs aregionwhen using Instance Principals.- Authentication is handled automatically via the instance’s identity.
6. Test on the Target Instance
- SSH into the instance that matches the dynamic group rule.
- Run your Python script.
- If you get authorization errors:
- Confirm the instance is correctly in the Dynamic Group (matching rule is correct).
- Verify Policies are in the correct compartment and reference the correct dynamic group name.
- Make sure the script is using the right region.
7. Clean Up API Keys
Once confirmed working:- Remove user API keys from:
- OCI Console (user’s API Keys section) if no longer needed.
- Any
~/.oci/configentries that were only used for this script.
- Rotate or disable old keys in accordance with your security policy.
If you paste a short snippet of your current API‑key‑based Python code, I can show you the exact before/after using Instance Principals.
Using Terraform
Using Terraform
terraform plan should show two resources to add (+ oci_identity_dynamic_group.MONITORING_INSTANCE_PRINCIPALS and + oci_identity_policy.MONITORING_INSTANCE_PRINCIPALS_POLICY) and no changes to existing resources.
