In questa guida rapida, creerai un file di configurazione Terraform
che esegue il provisioning di un bucket di archiviazione e carica un oggetto sample_file.txt
nel
bucket. Per completare questa guida rapida, utilizzerai l'editor di Cloud Shell, il terminale Cloud Shell e l'interfaccia a riga di comando Terraform, preinstallata in Cloud Shell.
Prima di iniziare
Per configurare un progetto per questa guida rapida, completa i seguenti passaggi:
- Accedi al tuo account Google Cloud. Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti gratuiti per l'esecuzione, il test e il deployment dei carichi di lavoro.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Assicurati che la fatturazione sia attivata per il tuo progetto Google Cloud.
-
Attiva l'API Cloud Storage.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Assicurati che la fatturazione sia attivata per il tuo progetto Google Cloud.
-
Attiva l'API Cloud Storage.
Crea la struttura delle cartelle e il file di configurazione di Terraform
Per creare il file di configurazione Terraform e il file che caricherai come oggetto in Cloud Storage, segui questi passaggi:
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
- Imposta il progetto Google Cloud predefinito a cui vuoi applicare la configurazione di Terraform:
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
- Nel terminale Cloud Shell, imposta la home directory come attiva:
cd
- Crea una nuova cartella denominata
terraform
:
mkdir terraform
- Avvia l'editor di Cloud Shell facendo clic su Apri editor sulla barra degli strumenti della finestra di Cloud Shell.
- Nel riquadro Explorer, fai clic con il tasto destro del mouse sulla cartella
terraform
, quindi fai clic su Nuovo file. - Inserisci
main.tf
come nome del file e fai clic su OK. - Nel riquadro Explorer, fai clic con il tasto destro del mouse sulla cartella
terraform
, quindi fai clic su Nuovo file. - Inserisci
sample_file.txt
come nome del file e fai clic su OK.
Definisci l'infrastruttura nel file di configurazione Terraform
Per definire l'infrastruttura di cui vuoi eseguire il provisioning nel file di configurazione Terraform, segui questi passaggi:
Nell'editor di Cloud Shell, apri il file
main.tf
.Copia il seguente esempio nel file
main.tf
.# Create new storage bucket in the US # location with Standard Storage resource "google_storage_bucket" "static" { name = "BUCKET_NAME" location = "US" storage_class = "STANDARD" uniform_bucket_level_access = true } # Upload a text file as an object # to the storage bucket resource "google_storage_bucket_object" "default" { name = "OBJECT_NAME" source = "OBJECT_PATH" content_type = "text/plain" bucket = google_storage_bucket.static.id }
Sostituisci:
BUCKET_NAME con il nome del bucket che vuoi creare. Ad esempio,
my-bucket
.OBJECT_NAME con il nome dell'oggetto che vuoi caricare. Per questa guida rapida, inserisci il nome
sample_file.txt
.OBJECT_PATH con il percorso dell'oggetto da caricare. Per questa guida rapida, inserisci il percorso
~/terraform/sample_file.txt
.
Salva il file
main.tf
.
Inizializza la directory di lavoro contenente il file di configurazione Terraform
Per inizializzare Terraform e la directory contenente il file di configurazione Terraform, completa i seguenti passaggi:
Per aprire il terminale Cloud Shell, fai clic su Apri terminale nella barra degli strumenti dell'editor di Cloud Shell.
Nel terminale Cloud Shell, imposta la cartella
terraform
come directory di lavoro corrente:cd ~/terraform
Inizializza Terraform:
terraform init
Se viene richiesto di autorizzare Cloud Shell, fai clic su Autorizza.
Terraform inizializza la directory di lavoro. Se inizializza correttamente la directory di lavoro, Terraform restituisce un output simile al seguente:
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.
Visualizza l'anteprima del piano di esecuzione
Il piano di esecuzione di Terraform si basa sulla configurazione Terraform e indica le modifiche che Terraform pianifica di apportare all'infrastruttura e ai servizi Cloud Storage.
Visualizza il piano di esecuzione di Terraform:
terraform plan
Output di esempio:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# google_storage_bucket.static will be created
+ resource "google_storage_bucket" "static" {
+ force_destroy = false
+ id = (known after apply)
+ location = "US"
+ name = "my-bucket"
+ project = "my-project"
+ public_access_prevention = (known after apply)
+ self_link = (known after apply)
+ storage_class = "STANDARD"
+ uniform_bucket_level_access = true
+ url = (known after apply)
+ versioning {
+ enabled = (known after apply)
}
+ website {
+ main_page_suffix = (known after apply)
+ not_found_page = (known after apply)
}
}
# google_storage_bucket_object.default will be created
+ resource "google_storage_bucket_object" "default" {
+ bucket = (known after apply)
+ content_type = "text/plain"
+ crc32c = (known after apply)
+ detect_md5hash = "different hash"
+ id = (known after apply)
+ kms_key_name = (known after apply)
+ md5hash = (known after apply)
+ media_link = (known after apply)
+ name = "sample_file.txt"
+ output_name = (known after apply)
+ self_link = (known after apply)
+ source = "sample_file.txt"
+ storage_class = (known after apply)
}
Plan: 2 to add, 0 to change, 0 to destroy.
Applica le modifiche proposte nel piano di esecuzione
Per applicare le modifiche al file di configurazione Terraform, completa questi passaggi:
Applica le modifiche del piano di esecuzione all'infrastruttura Cloud Storage con il comando seguente. Quando applichi le modifiche, Terraform crea un bucket di archiviazione e carica
sample_file.txt
nel bucket.terraform apply
Output di esempio:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # google_storage_bucket.static will be created + resource "google_storage_bucket" "static" { + force_destroy = false + id = (known after apply) + location = "US" + name = "my-bucket" + project = "my-project" + public_access_prevention = (known after apply) + self_link = (known after apply) + storage_class = "STANDARD" + uniform_bucket_level_access = true + url = (known after apply) + versioning { + enabled = (known after apply) } + website { + main_page_suffix = (known after apply) + not_found_page = (known after apply) } } # google_storage_bucket_object.default will be created + resource "google_storage_bucket_object" "default" { + bucket = (known after apply) + content_type = "text/plain" + crc32c = (known after apply) + detect_md5hash = "different hash" + id = (known after apply) + kms_key_name = (known after apply) + md5hash = (known after apply) + media_link = (known after apply) + name = "sample_file.txt" + output_name = (known after apply) + self_link = (known after apply) + source = "sample_file.txt" + storage_class = (known after apply) } Plan: 2 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:
Digita
yes
e premi Invio.In caso di esito positivo, Terraform restituisce un output simile al seguente:
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
Visualizza il bucket di archiviazione e l'oggetto caricato
Nella console Google Cloud, vai alla pagina Bucket di Cloud Storage.Viene visualizzato il nuovo bucket, contenente l'oggetto sample_file.txt
. Tieni presente che il provisioning delle risorse potrebbe richiedere alcuni minuti dopo l'esecuzione di terraform apply
.
Pulizia del progetto
Per evitare addebiti imprevisti da parte delle risorse Google Cloud create durante questa guida rapida, completa i seguenti passaggi per eseguire la pulizia delle risorse:
Nel terminale Cloud Shell, imposta la cartella
terraform
come directory di lavoro corrente:cd ~/terraform
Elimina le risorse Cloud Storage che hai creato in base al file di configurazione Terraform:
terraform destroy
In caso di esito positivo, Terraform restituisce un output simile al seguente:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # google_storage_bucket.static will be destroyed - resource "google_storage_bucket" "static" { - default_event_based_hold = false -> null - force_destroy = false -> null - id = "my-bucket" -> null - labels = {} -> null - location = "US" -> null - name = "" -> null - project = "example-project" -> null - public_access_prevention = "inherited" -> null - requester_pays = false -> null - self_link = "https://www.googleapis.com/storage/v1/b/cbonnie-bucket-9" -> null - storage_class = "STANDARD" -> null - uniform_bucket_level_access = true -> null - url = "gs://BUCKET_NAME" -> null } # google_storage_bucket_object.default will be destroyed - resource "google_storage_bucket_object" "default" { - bucket = "my-bucket" -> null - content_type = "text/plain" -> null - crc32c = "yZRlqg==" -> null - detect_md5hash = "XrY7u+Ae7tCTyyK7j1rNww==" -> null - event_based_hold = false -> null - id = "my-bucket-sample_file.txt" -> null - md5hash = "XrY7u+Ae7tCTyyK7j1rNww==" -> null - media_link = "https://storage.googleapis.com/download/storage/v1/b/BUCKET_NAME/o/sample_file.txt?generation=1675800386233102&alt=media" -> null - metadata = {} -> null - name = "sample_file.txt" -> null - output_name = "sample_file.txt" -> null - self_link = "https://www.googleapis.com/storage/v1/b/BUCKET_NAME/o/sample_file.txt" -> null - source = "sample_file.txt" -> null - storage_class = "STANDARD" -> null - temporary_hold = false -> null } Plan: 0 to add, 0 to change, 2 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:
Digita
yes
e premi Invio. In caso di esito positivo, Terraform restituisce un output simile al seguente:Destroy complete! Resources: 2 destroyed.
Nell'editor di Cloud Shell, fai clic con il tasto destro del mouse sulla cartella
terraform
nel riquadro Explorer, quindi fai clic su Elimina.Quando richiesto, fai clic su OK per confermare.
Per verificare che il bucket e l'oggetto siano stati eliminati, vai alla pagina Bucket nella console Google Cloud.
Passaggi successivi
- Vedi Risorse Terraform disponibili per Cloud Storage.
- Consulta le risorse Terraform per altri prodotti Google Cloud.