Initialize A Local Terraform Configuration
Initialize A Local Terraform Configuration
Initialize A Local Terraform Configuration
Step 1 of 3
random_pet.server: Creating...
random_pet.server: Creation complete after 0s [id=feasible-cougar]
Outputs:
random_server_id = feasible-cougar
In the next step, you'll execute this configuration on Terraform Cloud.
Step 2 of 3
Create an account
Sign up for a free account at Terraform Cloud.
You'll be asked to create an organization. This could be the name of your company,
your own name, the name of your department, or similar. For this example, we'll
use hashicorp-education but you'll need to use your own unique organization name.
Generate a token
To authenticate to Terraform Cloud, you must create a .terraformrc file with a unique
token.
Go to the Tokens page under your user icon, the "User Settings" menu, and the
"Tokens" submenu. Create a token named random-pet-demo.
Copy the token displayed on the screen.
We've created a scaffold for you at ~/.terraformrc. Open it in the editor and paste your
token inside the double quotes on the token line: token = "".
credentials "app.terraform.io" {
token = "aaaa"
}
9n86x3A2l3JF9A.atlasv1.VJVjY7radPnffxyrCdw2iTzzeO0lgrWv0nBqait9XEnpgaZmKaoz9k8BaZ
A92MR6IsY
Your Interactive Bash Terminal. A safe place to learn and execute commands version 0.1.
$ mkdir -p ~/random-pet-demo
>}
Terraform Cloud
Step 2 of 3
Create an account
Sign up for a free account at Terraform Cloud.
You'll be asked to create an organization. This could be the name of your company,
your own name, the name of your department, or similar. For this example, we'll
use hashicorp-education but you'll need to use your own unique organization name.
Generate a token
To authenticate to Terraform Cloud, you must create a .terraformrc file with a unique
token.
Go to the Tokens page under your user icon, the "User Settings" menu, and the
"Tokens" submenu. Create a token named random-pet-demo.
Copy the token displayed on the screen.
We've created a scaffold for you at ~/.terraformrc. Open it in the editor and paste your
token inside the double quotes on the token line: token = "".
credentials "app.terraform.io" {
token = "aaaa"
}
Run init
First, run init to migrate the existing state to Terraform Cloud.
terraform init
You will be prompted to move state to Terraform Cloud.
Make changes
To see the effect of a remotely executed command, make a change to main.tf. Change
the stage variable to development instead of production. This will trigger the creation of
a new random pet name.
variable "stage" {
default = "development"
}
Then, run apply:
terraform apply
As with a local Terraform run, you'll be asked to confirm. But this time, you'll be running
the command in Terraform Cloud. The new random pet name is displayed as an output.
You can scroll up to find the URL to this run within your organization and workspace in
Terraform Cloud.
Or, visit Terraform Cloud and select your organization. You'll see a workspace
named random-pet-demo. Click the workspace to see that it has been run. You can
examine the output from the run or view state, variables, or other settings.
Terraform Tutorial
Terraform in 60 Seconds
A Terraform configuration is a series of code blocks that define your intended
infrastructure. You'll run the terraform command against this file to create an Nginx
webserver and view the default Nginx web page.
View code
First, open the main.tf file in the text editor by clicking this link.
terraform-docker-demo/main.tf
resource "docker_image" "nginx" {
name = "nginx:latest"
}
Apply
Now provision the webserver by running apply.
terraform apply
You will be asked to confirm. Type yes and press ENTER. It may take up to 30 seconds.
A message will display confirmation that it succeeded.
Verify
Visit this URL to view the default Nginx web page which is now live:
Nginx index page
Alternatively, you can examine Docker's process list. You will see
the tutorial container which is running Nginx.
docker ps
Destroy
To remove the Nginx webserver as defined in main.tf, run the destroy command.
terraform destroy
You will be prompted to confirm. Type yes and press ENTER.
Conclusion
You have now created and destroyed your first Terraform resources! Terraform
supports hundreds of ecosystem providers, from major cloud resources to content
delivery networks and more.
Continue learning at HashiCorp Learn and the Terraform API documentation or discuss
with others on the Terraform forum.