Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Initialize A Local Terraform Configuration

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9
At a glance
Powered by AI
The key takeaways are that Terraform can be used to provision infrastructure from configuration files and supports providers for major cloud platforms and other services. It uses the init, plan, apply and destroy commands to manage the lifecycle.

The three steps are: 1) Create an account and organization on Terraform Cloud, 2) Generate an API token, 3) Edit the Terraform configuration file to specify the organization and workspace.

The Terraform configuration file needs to be edited to specify the backend as remote and include the organization name and workspace name under which it will run on Terraform Cloud.

Terraform Cloud

 Step 1 of 3 

Initialize a Local Terraform


Configuration
Initialize a Local Terraform Configuration
Start by running Terraform locally on a project we've created for you.
This Terraform config uses the random_pet provider to generate a random name
like endless-mastodon or feasible-cougar. It doesn't require any credentials and it
won't cost any money to run.
Change into the project directory:
cd ~/random-pet-demo
Initialize the project:
terraform init
Apply the configuration. You'll be asked to confirm by typing "yes", and then the name of
an animal will be printed as an output.
terraform apply
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

random_pet.server: Creating...
random_pet.server: Creation complete after 0s [id=feasible-cougar]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

random_server_id = feasible-cougar
In the next step, you'll execute this configuration on Terraform Cloud.

 Step 2 of 3 

Configure a Terraform Cloud token


Configure a Terraform Cloud token
There are three steps to running an existing Terraform configuration on Terraform
Cloud. Two steps occur on the Terraform Cloud website:
 Create a free account including an organization and a workspace
 Generate an API token and copy it to a local file
 Edit the existing Terraform configuration with the name of the Terraform Cloud
organization and workspace

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"
}

Edit the Terraform configuration


Finally, edit ~/random-pet-demo/main.tf with the name of the organization you
created earlier. Under workspaces, use random-pet-demo as the name. This workspace
will be created on Terraform Cloud the first time we run terraform init in the next
step.
terraform {
backend "remote" {
organization = "hashicorp-education"
workspaces {
name = "random-pet-demo"
}
}
}
In the next step, you'll migrate your existing project to Terraform Cloud.
Pn4vQ3yvFzifjA.atlasv1.FpmMIR8ieLhSKtKT3e9NIGVvt9u9BWGUmLMZKLbGIAHdq4YdWVfhPxLs7w
Nr3CHaiPU

9n86x3A2l3JF9A.atlasv1.VJVjY7radPnffxyrCdw2iTzzeO0lgrWv0nBqait9XEnpgaZmKaoz9k8BaZ
A92MR6IsY
Your Interactive Bash Terminal. A safe place to learn and execute commands version 0.1.

$ mkdir -p ~/random-pet-demo

$ cd ~/random-pet-demo && curl -L https://raw.githubusercontent.com/hashicorp/demo-terraform-


101/master/getting-started/tfc/main.tf -O

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 409 100 409 0 0 1366 0 --:--:-- --:--:-- --:--:-- 1363

$ cat <<EOT >> ~/.terraformrc

> credentials "app.terraform.io" {

> token = ""

>}

Terraform Cloud
 Step 2 of 3 

Configure a Terraform Cloud token


Configure a Terraform Cloud token
There are three steps to running an existing Terraform configuration on Terraform
Cloud. Two steps occur on the Terraform Cloud website:
 Create a free account including an organization and a workspace
 Generate an API token and copy it to a local file
 Edit the existing Terraform configuration with the name of the Terraform Cloud
organization and workspace

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"
}

Edit the Terraform configuration


Finally, edit ~/random-pet-demo/main.tf with the name of the organization you
created earlier. Under workspaces, use random-pet-demo as the name. This workspace
will be created on Terraform Cloud the first time we run terraform init in the next
step.
terraform {
backend "remote" {
organization = "hashicorp-education"
workspaces {
name = "random-pet-demo"
}
}
}
In the next step, you'll migrate your existing project to Terraform Cloud.
 Step 3 of 3 

Migrate State to Terraform Cloud


Migrate State to Terraform Cloud
Finally, let's migrate the project to Terraform Cloud and execute the configuration
remotely. The steps include:
 Run init on your local configuration
 Delete your local terraform.tfstate file
 Run apply to execute the configuration remotely
 Make further changes and run apply again

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.

Delete local state


You may need to delete the local terraform.tfstate file to avoid warnings. State is
now stored on Terraform Cloud so this file is no longer needed.
rm terraform.tfstate
Run apply
Then, run apply to run the configuration in Terraform Cloud. This will also automatically
create the random-pet-demo workspace on Terraform Cloud.
terraform apply
Because the existing state was copied to Terraform Cloud, and because no changes
were made to the Terraform configuration code, no changes need to be provisioned at
this time.

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"
}

resource "docker_container" "nginx" {


image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 80
}
}
You don't have to edit or even understand the code. It defines two resources: a Docker
disk image that packages the Nginx webserver, and a Docker container that gives it a
name and runs it on port 80.
Init
All Terraform workflows start with the init command. Terraform searches the
configuration for both direct and indirect references to providers (such as Docker).
Terraform then attempts to load the required plugins.
terraform init

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.

You might also like