For enterprise AI systems, service accounts are safer than individual API keys when properly implemented. Service accounts isolate machine identity from human users, enforce least-privilege access, and support auditability at scale. Individual keys tied to people create credential sprawl, increase breach risk when employees leave, and complicate revocation. This guide shows how to deploy service accounts securely and retire individual keys where possible.
What You'll Need
- Access to your cloud provider's identity and access management (IAM) console (e.g., AWS IAM, Azure AD, GCP IAM)
- Enterprise AI platform admin permissions (e.g., for Azure OpenAI, Amazon Bedrock, Google Vertex AI)
- Service account management tools (e.g., AWS CLI, Azure CLI, gcloud CLI)
- Secrets manager or vault (e.g., AWS Secrets Manager, Azure Key Vault, HashiCorp Vault)
- Monitoring and logging access for audit trails (e.g., CloudTrail, Azure Monitor, Stackdriver)
Step-by-Step
- Create a dedicated service account. In your cloud IAM console, navigate to Service Accounts (AWS), Managed Identities (Azure), or Service Accounts (GCP). Create a new account named clearly for its purpose—e.g.,
ai-inference-svcorrag-pipeline-runner. Avoid generic names likeadminormachine. Assign it only to the AI service resource (e.g., specific Bedrock model ARN, Vertex AI endpoint) and grant minimal permissions:aiplatform.endpoints.predictorbedrock:InvokeModel, nothing broader. Do not attach broad policies likeAWSBedrockFullAccess. - Generate and store credentials securely. For AWS IAM roles, attach the role to an EC2 instance, ECS task, or Lambda function—no long-lived keys are needed. For Azure, use system-assigned or user-assigned managed identity and grant it access via RBAC. For GCP, download the service account JSON key only if required (e.g., for on-prem workloads), and store it immediately in a secrets manager. Never commit the key to source control. In AWS, prefer IAM roles over keys; in Azure, use managed identities; in GCP, prefer workload identity federation over service account keys where possible.
- Rotate credentials automatically. Configure rotation policies: AWS Secrets Manager can rotate IAM access keys every 90 days; Azure Key Vault supports automatic certificate-based rotation for managed identities; GCP allows scheduled key rotation via Cloud Scheduler + Cloud Functions. For service account keys, enforce a maximum lifetime (e.g., 90 days) and disable automated rotation only if no rotation API exists—document why. Rotate immediately if any environment variable or config file containing a key is exposed.
- Update AI application code to use the new identity. Replace hard-coded individual API keys with SDK defaults that pick up environment-based credentials. For example, in Python with boto3, call
client = boto3.client('bedrock-runtime')without explicit keys—the SDK uses the IAM role attached to the container or instance. In Java, useAwsCredentialsProviderChainorDefaultCredentialsProvider. For Node.js, letfromEnv()orfromNodeProviderChain()supply credentials. AvoidAZURE_CLIENT_IDorGOOGLE_APPLICATION_CREDENTIALSpointing to local files unless the file is mounted via secrets volume and not committed. - Revoke and disable individual API keys. Audit existing keys: in AWS IAM, list access keys and delete those not tied to service accounts; in Azure AD, review app registrations and service principal credentials; in GCP, list service accounts and delete user-managed keys. For user-owned keys (e.g., legacy personal keys used for AI calls), disable or delete them after confirming no application uses them. Set up alerts for new key creation to catch unauthorized provisioning.
- Enable audit logging for AI calls. Turn on logging for your AI platform: AWS CloudTrail for Bedrock, Azure Monitor for Azure OpenAI, GCP Audit Logs for Vertex AI. Ensure logs capture caller identity (service account email or ARN), timestamp, model name, and whether the call succeeded. Route logs to a SIEM for correlation. Verify logs show the service account name—not a human user—when the call originates from an automated pipeline.
Common Mistakes
- Using user-assigned managed identities without restricting scope—always bind to specific resources, not whole subscriptions or projects.
- Storing service account keys in GitHub repos because "it's just for testing"—never do this; use mock APIs or local emulators instead.
- Granting
aiplatform.userorbedrock.fullaccessroles instead of model-specific permissions. - Forgetting to rotate keys after a team member leaves; service accounts prevent this, but legacy keys tied to individuals remain a risk if not cleaned up.
- Assuming managed identities work cross-account—by default they don't; you must explicitly grant cross-account access using resource-based policies or external IDs.
Tips
- Name service accounts after function, not person:
ml-inference-prodnotjohns-ai-key. - Use tags or labels to track ownership:
owner:ml-platform,env:prodhelps with cleanup and cost allocation. - Test permissions with
aws iam simulate-custom-policy,az role assignment list, orgcloud iam simulate-policybefore deploying. - Enable Cloud Audit Logs for IAM changes—track who created or modified service accounts.
- For multi-tenant AI services, use separate service accounts per tenant to limit blast radius.
Troubleshooting
If AI calls fail after switching to service accounts:
- Check the error message:
AccessDeniedExceptionmeans insufficient permissions—review the service account's IAM policy and ensure it includes the exact model or endpoint ARN/ID. - Verify credential loading: in AWS, run
aws sts get-caller-identityfrom the same environment as your app; confirm it returns the service role ARN, not a user. - Confirm network access: some AI endpoints require VPC endpoints or private links—ensure the service account's environment can reach them.
- Check token expiration: if using short-lived tokens (e.g., GCP Workload Identity), verify the pod or container has time-synced clocks and can reach the metadata server.
- If rotation broke access, temporarily re-enable the old key to confirm, then reapply rotation steps and validate the new key works before deleting the old one.