--- subcategory: "Service Accounts" --- # Resource: mongodbatlas_service_account `mongodbatlas_service_account` provides a Service Account resource. The resource lets you create, update, delete, and import a Service Account for the specified Organization. ~> **IMPORTANT WARNING:** Managing Service Accounts with Terraform **exposes sensitive organizational secrets** in Terraform's state. We suggest following [Terraform's best practices](https://developer.hashicorp.com/terraform/language/state/sensitive-data). -> **NOTE:** To rotate secrets, see [Guide: Service Account Secret Rotation](../guides/service-account-secret-rotation). ## Example Usages The following example creates a Service Account without an Atlas-generated secret, then creates its first secret as a managed resource. ```terraform # Create a Service Account without an Atlas-generated secret, then create the first secret # explicitly with mongodbatlas_service_account_secret so this configuration owns it. resource "mongodbatlas_service_account" "this" { org_id = var.org_id name = "example-service-account" description = "Example Service Account" roles = ["ORG_READ_ONLY"] without_initial_secret = true } resource "mongodbatlas_service_account_secret" "this" { org_id = var.org_id client_id = mongodbatlas_service_account.this.client_id secret_expires_after_hours = 2160 # 90 days } output "secret_id" { description = "The ID of the Service Account secret." value = mongodbatlas_service_account_secret.this.secret_id } output "secret" { description = "The secret value for the Service Account. Returned only when the secret is created." sensitive = true value = mongodbatlas_service_account_secret.this.secret } ``` ## Schema ### Required - `description` (String) Human readable description for the Service Account. - `name` (String) Human-readable name for the Service Account. The name is modifiable and does not have to be unique. - `org_id` (String) Unique 24-hexadecimal digit string that identifies the organization that contains your projects. - `roles` (Set of String) A list of organization-level roles for the Service Account. ### Optional - `secret_expires_after_hours` (Number) The expiration time of the new Service Account secret, provided in hours. The minimum and maximum allowed expiration times are subject to change and are controlled by the organization's settings. Set this field when you set `without_initial_secret` to false or omit `without_initial_secret`. Do not set this field when you set `without_initial_secret` to true. You cannot update this field after you create the Service Account. - `without_initial_secret` (Boolean) When true, creates the Service Account without generating an initial secret. If you set this field to true, do not set `secret_expires_after_hours`. ### Read-Only - `client_id` (String) The Client ID of the Service Account. - `created_at` (String) The date that the Service Account was created on. This parameter expresses its value in the ISO 8601 timestamp format in UTC. - `secrets` (Attributes List) A list of secrets associated with the specified Service Account. (see [below for nested schema](#nestedatt--secrets)) - `system_managed` (Boolean) Indicates whether the Service Account is system managed. ### Nested Schema for `secrets` Read-Only: - `created_at` (String) The date that the secret was created on. This parameter expresses its value in the ISO 8601 timestamp format in UTC. - `expires_at` (String) The date for the expiration of the secret. This parameter expresses its value in the ISO 8601 timestamp format in UTC. - `last_used_at` (String) The last time the secret was used. This parameter expresses its value in the ISO 8601 timestamp format in UTC. - `masked_secret_value` (String) The masked Service Account secret. - `secret` (String, Sensitive) The secret for the Service Account. It will be returned only the first time after creation. - `secret_id` (String) Unique 24-hexadecimal digit string that identifies the secret. ## Import Import the Service Account resource by using the Organization ID and Client ID in the format `ORG_ID/CLIENT_ID`, e.g. ``` $ terraform import mongodbatlas_service_account.test 6117ac2fe2a3d04ed27a987v/mdb_sa_id_1234567890abcdef12345678 ``` -> **NOTE:** Atlas does not populate `secret_expires_after_hours` or `without_initial_secret` during import. Omit both attributes from the resource definition when you import a Service Account. For more information, see [Create One Organization Service Account](https://www.mongodb.com/docs/api/doc/atlas-admin-api-v2/operation/operation-createorgserviceaccount) in the MongoDB Atlas API documentation.