DEV Community

Script Koder
Script Koder

Posted on

Terraform Beginners Blog

To install Terraform

for more updated info refer to the official website link.
Documentation

# download and install yum utils
sudo yum install -y yum-utils

# add the respective repository
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

# install the terraform
sudo yum -y install terraform
Enter fullscreen mode Exit fullscreen mode

How to create a resource in Terraform

Example 1 : to create a local file in Terraform

  • to create a resource in HCL( Hashicorp Configuration Language ) we use the following code block.
resource "local_file" "pet" {
    filename = "/root/pets.txt"
    content = "We love pets!"
}

# description of the code block
--------------------------------
# resource_argument : Description
---------------------------------
# resource : block type
# "local_file" : Resource type
# "pet" : Resource Name
# filename : file path where file has to be created
# content : content which needs to be written in that file
Enter fullscreen mode Exit fullscreen mode

Example 2 : to create an EC2 instance on AWS cloud

  • to create ec2 instance using Terraform we can use below code.
resource "aws_instance" "webserver" {
    ami = "ami-id"
    instance_type = "t2.micro"
}

# description of the code block
--------------------------------
# resource_argument : Description
---------------------------------
# resource : block type
# "aws_instance" : Resource type
# "webserver" : Resource Name
# ami : image id which will be used while creating the ec2 resource
# instance_type : type of instance which needs to be provisioned
Enter fullscreen mode Exit fullscreen mode

Example 3 : to create S3 bucket on AWS cloud

  • to create s3 bucket using terraform on AWS Cloud we can use below code.
resource "aws_s3_bucket" "data" {
    bucket = "webserver-bucket-org-2207"
    acl = "private"
}

# description of the code block
--------------------------------
# resource_argument : Description
---------------------------------
# resource : block type
# "aws_s3_bucket" : Resource type
# "data" : Resource Name
# bucket : bucket name which will be assigned after resource has been created
# acl : type of access to private to given S3 bucket
Enter fullscreen mode Exit fullscreen mode

Top comments (0)