Creating and accessing a Database Instance using Secret Manager and Terraform/OpenTofu
In this tutorial, you will use a Terraform/OpenTofu configuration file that builds your entire infrastructure, such as a Database Instance, a secret, and a version in which your database credentials are stored. You will then be able to access your database credentials securely from the Scaleway console.
Before you start
To complete the following actions, you must have:
- A Scaleway account logged into the console (External link)
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- A valid API key
- Created an SSH key
Clone the GitHub repository and configure your environment variables
The Scaleway GitHub repository (External link) contains all the code you need to deploy your infrastructure.
-
Open a terminal and clone the repository to your local machine using the following command:
Copygit clone git@github.com:scaleway/tutorial-sem-terraform.git -
Access the
tutorial-sem-terraformdirectory:Copycd tutorial-sem-terraform -
Paste the following command in your terminal to configure your environment variables. Make sure that you replace the placeholder values with your own.
Copyexport SCW_ACCESS_KEY="<SCWxxxxxxxxxxxxxxxxx>" export SCW_SECRET_KEY="<xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx>" export SCW_PROJECT_ID="<xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx>" export SCW_ORGANIZATION_ID="<xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx>"
Understanding the files and directories
In the tutorial-sem-terraform repository, you will find the following folder structure:
examples/
├─ key-value/
├─ app/
├─ infra/- The
key-valuefolder contains two additional folders namedappandinfra. - The
appfolder will interact with the database and ask Secret Manager for the password to connect to the database. - The
infrafolder contains the files that will configure your database and secret.
Build and deploy your infrastructure
- Access the
infrafolder using the following command:Copycd examples/key-value/infra - Run the following command to download the dependencies and initialize Terraform/OpenTofu:
Copy
terraform init - Run the following command to configure your infrastructure:
Copy
terraform apply - Type
yesandEnterwhen prompted to enter a value. An output similar to the following displays:CopyApply complete! Resources: 3 added, 0 changed, 0 destroyed. Outputs: database_public_endpoint = tolist([ { "endpoint_id" = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" "hostname" = "" "ip" = "<public-endpoint>" "name" = "" "port" = <port> }, ]) - Navigate to the
appfolder:Copycd .. && cd app - Run the following command:
An output similar to the following should display:Copy
go run .CopySCW_DEFAULT_REGION: fr-parSuccessfully connected!
Retrieve your database credentials
- Click Secret Manager in the Security and Identity section of the Scaleway console (External link) side menu.
- Click the
database_secretsecret.
- Click the Versions tab.
- Click seeMore icon next to the secret version. Your database's credentials display.
Visit our Help Center and find the answers to your most frequent questions.
Visit Help Center