367 words
2 minutes
Terraform Basics
Terraform Workflow
Write the terraform configuration =====> Plan the infrastructure =====> deploy the infrastructure
Terraform Initialization
terraform initinitializes the working directory for terraform
Terraform Key Concepts
terraform planAfter initializtion, reads the terraform code and shows the plan of execution / deployment. In this phase, the authentication of credentials takes placeterraform applyDeploys the infrastructure and statements in codeterraform destoryDestorys all of the deployed infrastructure and removes the resourcesWARNING
Non-reversible command. Proceed with caution
Stages of Terrform
Write IaC =====> terraform init =====> terraform plan =====> terraform apply
Terraform Providers
- providers are cloud vendors like aws, azure, google cloud etc. to which terrraform interacts to create resources
- terraform providers should always be versioned
Creating a machine using AWS as a provider
Project Files and What they do

- main.tf contains the resource defining code and provider defining code.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
resource "aws_instance" "instance_tf" {
ami = "ami-05803413c51f242b7"
instance_type = "t2.micro"
}- provider.tf.example contains the information needed by the provides, sometimes confidential secrets and passwords. Never share the providers.tf file if they contain your secrets. The .example has to be removed to use the file. Terraform will automatically use the provider.tf file to get the provider configuration files.
provider "aws" {
access_key = "your aws access key"
secret_key = "your aws secret key"
region = "your aws region"
}Currently, these are the only files you need to get an instance in AWS running.
Passing the credentials as environment variables
- from your terminal, export your environmental variables as
export AWS_ACCESS_KEY = "your key" - to check you can use:
env | grep -i awswhich shows all the environmental variables starting with aws. - to pass the environmental variables, no need to specify them when using
terraform planorterraform apply. Terraform will automatically read the variables from your environment and access them.
Terraform State
- important for resource tracking
- is a way that terraform read to identify what has been deployed
- also seen as a database for terraform
- named as
terraform.tfstate - usually stored in same working directory, can also be stored remotely
CAUTIONNever loose terraform state file
Github Repo for the practice files
Find the attached github repo for the files if you need to take a look. Also, some files will be missing as they may contain secrets. Figure them out yourself
Waiting for api.github.com...
Terraform Basics
https://dev-tutorials.pages.dev/posts/terraform/terraformbasics/