### Terraform configuration for 1Password provider with item resources Source: https://github.com/1password/terraform-provider-onepassword/blob/main/README.md Configures the 1Password Terraform provider using a service account token and defines two item resources. The first uses section_map for direct field access by label, and the second uses the section (list) block syntax. Requires a vault_id variable and a valid service account token. ```HCL terraform { required_providers { onepassword = { source = "1Password/onepassword" } } } provider "onepassword" { service_account_token = "<1Password service account token>" } variable "vault_id" {} # Using section_map for direct field access by label resource "onepassword_item" "demo_credentials" { vault = var.vault_id title = "Demo API Credentials" category = "login" section_map = { "api_credentials" = { field_map = { "api_key" = { type = "CONCEALED" value = "your-api-key" } "api_secret" = { type = "CONCEALED" password_recipe = { length = 32 symbols = false } } } } } } # Access fields directly by label output "api_key" { value = onepassword_item.demo_credentials.section_map["api_credentials"].field_map["api_key"].value sensitive = true } # Using section (list) for block syntax resource "onepassword_item" "demo_login" { vault = var.vault_id title = "Demo Terraform Login" category = "password" username = "demo-username" password_recipe { length = 40 symbols = false } } ``` -------------------------------- ### Initialize Terraform with 1Password Provider Source: https://github.com/1password/terraform-provider-onepassword/blob/main/examples/README.md Run terraform init from the examples/directory to initialize the backend and install the 1password/onepassword provider version ~> 1.0.0. The output shows successful initialization and creation of the .terraform.lock.hcl lock file. ```sh $ terraform init Initializing the backend... Initializing provider plugins... - Finding 1password/onepassword versions matching "~> 1.0.0"... - Installing 1password/onepassword v1.0.0... - Installed 1password/onepassword v1.0.0 (signed by a HashiCorp partner, key ID 6681876AE08DC4BF) Partner and community providers are signed by their developers. If you'd like to know more about provider signing, you can read about it here: https://www.terraform.io/docs/cli/plugins/signing.html Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. ``` -------------------------------- ### Start Delve debugging session Source: https://github.com/1password/terraform-provider-onepassword/blob/main/CONTRIBUTING.md Starts a Delve debugging session with the --debug flag. ```sh dlv debug . -- --debug ``` -------------------------------- ### Terraform destroy command and output Source: https://github.com/1password/terraform-provider-onepassword/blob/main/examples/README.md Runs `terraform destroy` to remove all resources created in the example. The output shows the plan with three items to be destroyed and the confirmation prompt. After entering 'yes', all three onepassword_item resources are destroyed. ```sh $ terraform destroy An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # onepassword_item.demo_db will be destroyed - resource "onepassword_item" "demo_db" { - category = "database" -> null - database = "Example MySQL Instance" -> null - hostname = "localhost" -> null - id = "vaults//items/" -> null - password = (sensitive value) - port = "3306" -> null - title = "Demo TF Database" -> null - type = "mysql" -> null - username = "root" -> null - uuid = "" -> null - vault = "" -> null } # onepassword_item.demo_login will be destroyed - resource "onepassword_item" "demo_login" { - category = "login" -> null - id = "vaults//items/" -> null - password = (sensitive value) - title = "Demo Terraform Login" -> null - username = "test@example.com" -> null - uuid = "" -> null - vault = "" -> null } # onepassword_item.demo_password will be destroyed - resource "onepassword_item" "demo_password" { - category = "password" -> null - id = "vaults//items/" -> null - password = (sensitive value) - title = "Demo Password Recipe" -> null - uuid = "" -> null - vault = "" -> null - password_recipe { - digits = true -> null - length = 40 -> null - symbols = false -> null } } Plan: 0 to add, 0 to change, 3 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes onepassword_item.demo_login: Destroying... [id=vaults//items/] onepassword_item.demo_db: Destroying... [id=vaults//items/] onepassword_item.demo_password: Destroying... [id=vaults//items/] onepassword_item.demo_login: Destruction complete after 0s onepassword_item.demo_db: Destruction complete after 0s onepassword_item.demo_password: Destruction complete after 0s ``` -------------------------------- ### Retrieve an item by vault and title Source: https://github.com/1password/terraform-provider-onepassword/blob/main/docs/data-sources/item.md Use this data source to fetch details of an item by its vault UUID and either the title or UUID. The example looks up an item by title within a specified vault. ```terraform data "onepassword_item" "example" { vault = "your-vault-id" title = "your-item-title" } ``` -------------------------------- ### Read environment variables from a 1Password Environment Source: https://github.com/1password/terraform-provider-onepassword/blob/main/docs/data-sources/environment.md Use this data source to read environment variables from a 1Password Environment. Requires service account or desktop app authentication; not available with 1Password Connect. The example reads an environment by its ID and outputs the variables map as sensitive. ```terraform # Read environment variables from a 1Password Environment. # Requires service account or desktop app authentication (not 1Password Connect). data "onepassword_environment" "example" { environment_id = "your-environment-id" } # Use the variables map in a resource or output output "env_variables" { value = data.onepassword_environment.example.variables sensitive = true } ``` -------------------------------- ### Provider output with TF_REATTACH_PROVIDERS Source: https://github.com/1password/terraform-provider-onepassword/blob/main/CONTRIBUTING.md Output printed to stdout when a debugging session starts correctly. Copy the TF_REATTACH_PROVIDERS line and export it or prefix Terraform commands with it. ```sh Provider started, to attach Terraform set the TF_REATTACH_PROVIDERS env var: TF_REATTACH_PROVIDERS='{"1Password/onepassword":{"Protocol":"grpc","Pid":3382870,"Test":true,"Addr":{"Network":"unix","String":"/tmp/plugin713096927"}}}' ``` -------------------------------- ### Create onepassword_item with section (list-based sections) Source: https://github.com/1password/terraform-provider-onepassword/blob/main/docs/resources/item.md Use this example to create a login item with list-based sections. It includes a password_recipe block to generate a password and a section block with a field. This approach is an alternative to the map-based section_map attribute and cannot be used together with it. ```terraform # Example using section (list-based sections) resource "onepassword_item" "example_with_list" { vault = "your-vault-id" title = "Example Item with Section List" category = "login" password_recipe { length = 40 symbols = false } section { label = "Example section" field { label = "Example field" type = "DATE" value = "2024-01-31" } } } ``` -------------------------------- ### Retrieve item values with ephemeral resource Source: https://github.com/1password/terraform-provider-onepassword/blob/main/docs/ephemeral-resources/item.md Use this ephemeral resource to retrieve item values without storing them in Terraform state. The example shows two configurations: one using the item title and another using the UUID. Both require the vault UUID. ```terraform # Example using ephemeral resource to retrieve item values ephemeral "onepassword_item" "example" { vault = "your-vault-id" title = "your-item-title" } # Example using UUID instead of title ephemeral "onepassword_item" "example_by_uuid" { vault = "your-vault-id" uuid = "your-item-uuid" } ``` -------------------------------- ### Create onepassword_item with section_map (map-based sections) Source: https://github.com/1password/terraform-provider-onepassword/blob/main/docs/resources/item.md Use this example to create a login item with map-based sections for direct field access. The section_map attribute allows defining sections keyed by label, each with a field_map for fields. This approach is an alternative to the list-based section attribute and cannot be used together with it. ```terraform # Example using section_map (map-based sections for direct field access) resource "onepassword_item" "example_with_map" { vault = "your-vault-id" title = "Example Item with Section Map" category = "login" section_map = { "credentials" = { field_map = { "api_key" = { type = "CONCEALED" value = "my-secret-api-key" } "api_endpoint" = { type = "URL" value = "https://api.example.com" } } } "metadata" = { field_map = { "environment" = { type = "STRING" value = "production" } "created_by" = { type = "STRING" value = "terraform" } } } } } ``` -------------------------------- ### Build the provider with go build Source: https://github.com/1password/terraform-provider-onepassword/blob/main/CONTRIBUTING.md Builds the 1Password Terraform provider binary. Run from the project root. ```sh go build . ``` -------------------------------- ### Apply Terraform Configuration to Create 1Password Items Source: https://github.com/1password/terraform-provider-onepassword/blob/main/examples/README.md Run terraform apply to create three onepassword_item resources: demo_db (database), demo_login (login), and demo_password (password). The plan shows the resources to be added, and after approving with 'yes', all three are created successfully. ```sh $ terraform apply An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # onepassword_item.demo_db will be created + resource "onepassword_item" "demo_db" { + category = "database" + database = "Example MySQL Instance" + hostname = "localhost" + id = (known after apply) + password = (sensitive value) + port = "3306" + title = "Demo TF Database" + type = "mysql" + username = "root" + uuid = (known after apply) + vault = "" } # onepassword_item.demo_login will be created + resource "onepassword_item" "demo_login" { + category = "login" + id = (known after apply) + password = (sensitive value) + title = "Demo Terraform Login" + username = "test@example.com" + uuid = (known after apply) + vault = "" } # onepassword_item.demo_password will be created + resource "onepassword_item" "demo_password" { + category = "password" + id = (known after apply) + password = (sensitive value) + title = "Demo Password Recipe" + uuid = (known after apply) + vault = "" + password_recipe { + digits = true + length = 40 + symbols = false } } Plan: 3 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes onepassword_item.demo_db: Creating... onepassword_item.demo_login: Creating... onepassword_item.demo_password: Creating... onepassword_item.demo_password: Creation complete after 0s [id=vaults//items/] onepassword_item.demo_db: Creation complete after 1s [id=vaults//items/] onepassword_item.demo_login: Creation complete after 1s [id=vaults//items/] Apply complete! Resources: 3 added, 0 changed, 0 destroyed. ``` -------------------------------- ### Generate documentation with go generate Source: https://github.com/1password/terraform-provider-onepassword/blob/main/CONTRIBUTING.md Regenerates the /docs Markdown using terraform-plugin-docs. ```sh go generate ``` -------------------------------- ### Run Go tests with coverage Source: https://github.com/1password/terraform-provider-onepassword/blob/main/CONTRIBUTING.md Runs all Go tests and checks test coverage. ```sh go test -v ./... -cover ``` -------------------------------- ### Import existing 1Password item using terraform import Source: https://github.com/1password/terraform-provider-onepassword/blob/main/docs/resources/item.md Use the terraform import command to bring an existing 1Password item into Terraform state. The command requires the resource address (onepassword_item.myitem) and the item's identifier in the format vaults//items/. The comment indicates this imports an existing 1Password item. ```shell # import an existing 1Password item terraform import onepassword_item.myitem vaults//items/ ``` -------------------------------- ### Build provider without optimizations Source: https://github.com/1password/terraform-provider-onepassword/blob/main/CONTRIBUTING.md Builds the provider with optimizations disabled, required for debugging. ```sh go build -gcflags="all=-N -l" . ``` -------------------------------- ### Terraform configuration for local provider use Source: https://github.com/1password/terraform-provider-onepassword/blob/main/CONTRIBUTING.md Specifies the onepassword provider in a Terraform configuration. Requires a 1Password service account token. After copying a newly built provider to the plugins directory, run terraform init again to avoid an error. ```tf terraform { required_providers { onepassword = { source = "1Password/onepassword" } } } provider "onepassword" { service_account_token = "<1Password service account token>" } ``` -------------------------------- ### Look up vault by name with onepassword_vault data source Source: https://github.com/1password/terraform-provider-onepassword/blob/main/docs/data-sources/vault.md Use this data source to retrieve details of a vault by its name. The `name` attribute is optional; if the vault is looked up by UUID, this field will be populated with the vault's name. ```terraform data "onepassword_vault" "example" { name = "your-vault-name" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.