Followers

Terraform providers resources

 

Terraform Blocks

This block can be called in 3 ways. All means the same.

  1. Terraform Block
  2. Terraform Settings Block
  3. Terraform Configuration Block

Each terraform block can contain a number of settings related to Terraform's behavior.

NOTE:-

Within a terraform block, only constant values can be used; arguments may not refer to named objects such as resources, input variables, etc, and may not use any of the Terraform language built-in functions.

image

Terraform Settings, Providers & Resource Blocks

Step-01: Introduction

  1. Terraform Settings
  2. Terraform Providers
  3. Terraform Resources
  4. Terraform File Function
  5. Create EC2 Instance using Terraform and provision a webserver with userdata.

image

Step-02: In c1-versions.tf - Create Terraform Settings Block

Understand about Terraform Settings Block and create it

# Terrform Block
terraform {
requrequired_version = "~> 1.0.8"  # allow right most version like 1.0.9 not the major version
                                   # if it is 1.0 then it will allow 1.xxxx because right most is 0 in this case
required_providers {
    //aws is an attribute you can specify any variable name here and same name is to mention on provider block
  aws ={   
      source="hashicorp/aws"
      version="~>3.0"
  }
}                                   
}

Step-03: In c1-versions.tf - Create Terraform Providers Block

Understand about Terraform Providers

image

image

image

image

Configure AWS Credentials in the AWS CLI if not configured

Verify AWS Credentials

  1. cat $HOME/.aws/credentials
  2. Create AWS Providers Block

Provider Block

# Terrform Block
terraform {
required_version = "~> 1.0.8"  # allow right most version like 1.0.9 not the major version
                                   # if it is 1.0 then it will allow 1.xxxx because right most is 0 in this case
required_providers {
    //aws is an attribute you can specify any variable name here and same name is to mention on provider block
  aws ={
      source="hashicorp/aws"
      version="~>3.0"
  }
}
}
provider "aws"{
region="ap-south-1"
profile="rprofile"
}

Step-04: In c2-ec2instance.tf - Create Resource Block

Understand about Resources

  1. Create EC2 Instance Resource
  2. Understand about File Function
  3. Understand about Resources - Argument Reference
  4. Understand about Resources - Attribute Reference

Resource: EC2 Instance

image

resource "aws_instance" "myins" {
    ami = "ami-0108d6a82a783b352"
    instance_type = "t2.micro"
    user_data = file("${path.module}/app1-install.sh")
    tags = {
      "Name" = "demo"
    }

}

Step-05: Review file app1-install.sh

#! /bin/bash
# Instance Identity Metadata Reference - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html

sudo yum update -y
sudo yum install -y httpd
sudo systemctl enable httpd
sudo service httpd start  
sudo echo '<h1>Welcome to StackSimplify - APP-1</h1>' | sudo tee /var/www/html/index.html
sudo mkdir /var/www/html/app1
sudo echo '<!DOCTYPE html> <html> <body style="background-color:rgb(250, 210, 210);"> <h1>Welcome to Terraform - APP-1</h1> <p>Terraform Demo</p> <p>Application Version: V1</p> </body></html>' | sudo tee /var/www/html/app1/index.html
sudo curl http://169.254.169.254/latest/dynamic/instance-identity/document -o /var/www/html/app1/metadata.html

Step-06: Execute Terraform Commands

Terraform Initialize

terraform init

Observation:

  1. Initialized Local Backend

  2. Downloaded the provider plugins (initialized plugins)

  3. Review the folder structure ".terraform folder"

Terraform Validate

terraform validate

Observation:

  1. If any changes to files, those will come as printed in stdout (those file names will be printed in CLI)

Terraform Plan

terraform plan

Observation:

  1. No changes - Just prints the execution plan

Terraform Apply

terraform apply

[or]

terraform apply -auto-approve

Observations:

  1. Create resources on cloud

  2. Created terraform.tfstate file when you run the terraform apply command

Step-07: Access Application

Important Note: verify if default VPC security group has a rule to allow port 80

Access index.html

curl localhost

curl localhost/app1/index.html

Access metadata.html

curl localhost/app1/metadata.html

Step-08: Terraform State - Basics

Understand about Terraform State

Terraform State file terraform.tfstate

image

image

Understand about Desired State and Current State

image

Step-09: Clean-Up

Terraform Destroy

terraform plan -destroy # You can view destroy plan using this command

terraform destroy

Clean-Up Files

rm -rf .terraform*

rm -rf terraform.tfstate*

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
static_page
DevOpsWorld: Terraform providers resources
Terraform providers resources
DevOpsWorld
https://www.devopsworld.co.in/p/terraform-providers-resources.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/p/terraform-providers-resources.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content