Agenda:
-------
Introduction of Terraform.
Installation
First Terraform script .
Terraform:
-------------------
Introduction.
- Terraform is an open-source, infrastructure as code software (IaC) tool, created by HashiCorp and written in the Go programming language.
- Infrastructure as code is the process of managing infrastructure in a file or files, rather than manually configuring resources in a user interface (UI).
- Here resources are nothing but virtual machines, Elastic IP, Security Groups, Network Interfaces (VPC's, SubNets) etc...
- Terraform code is written in the HashiCorp Configuration Language (HCL) in files with the extension .tf
- Terraform allows users to use HashiCorp Configuration Language (HCL) to create the files containing definitions of their desired resources on almost any provider (AWS, GCP, Azure, Digital Ocean, OpenStack, etc) and automates the creation of those resources at the time of apply.
https://www.terraform.io/docs/providers/index.html
Pre-Requisites for terraform:
------------------------------
1) Any Cloud Provider (AWS, GCP, Azure, Digital Ocean, OpenStack, etc)
2) User credentials (Secret Key and Access Key) < create a Iam user & attach admin policy for him>
Terraform - Installation:
--------------------------
#Login as a root user in ec2 instance
#You will need to upgrade your system and packages
yum update -y
#Download the terraform software.
#Use https://www.terraform.io/downloads.html to download the terraform software.
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
#Check the version
terraform -v (OR) terraform version
#Help
terraform -help
Terraform Commands:
===================
terraform init :
----------------
- The terraform init command is used to initialize a working directory containing Terraform configuration files.
- This is the first command that should be run after writing a new Terraform configuration.
terraform fmt:
--------------
- The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style.
terraform validate:
-------------------
The terraform validate command validates whether a configuration syntax is valid or not.
terraform plan(dry run):
------------------------
- The terraform plan command is used to create an execution plan.
- This command is a convenient way to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or to the state.
terraform apply:
----------------
- terraform apply to actually create the infrastructure on AWS.
terraform destroy:
-----------------
- The terraform destroy command is used to destroy the Terraform- managed infrastructure.
provider.tf ===AWS or AZure or GCP
main.tf ==== ec2, s3 ,auto,alb
variable.tf ===
Aws cloudformation==yaml ==
First Terraform Script:
----------------------
provider "aws" {
region = "us-east-1"
access_key = "AKIA6D6JBCN6U4TTLV4T"
secret_key = "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"
}
resource "aws_instance" "MyEC2Server" {
ami = "ami-06b21ccaeff8cd686"
instance_type = "t2.micro"
}
resource "aws_instance" "MyEC2Server1" {
ami = "ami-06b21ccaeff8cd686"
instance_type = "t2.micro"
key_name = "kepypair"
count = "2"
}
Note:
-----
- provider --> cloud service provider
- resource can be EC2 instance, s3 , VPC , ELB, EBS
- The general syntax for a Terraform resource is:
resource "<PROVIDER>_<TYPE>" "<NAME>" {
[CONFIG ...]
}
Here: PROVIDER is the name of a provider (e.g., aws),
TYPE is the type of resource to create in that provider (e.g., instance),
NAME is an identifier you can use throughout the Terraform code to refer to this resource (e.g., my_instance),
and CONFIG consists of one or more arguments that are specific to that resource.”
Assignment:
-----------
- what is default IAc tool of AWS.--- cloudformation other tools such as terraform
- Advantages of IaC
tf.State Representation:
The .tfstate file is generated by Terraform, storing the state of your infrastructure defined in code. It records the mapping between the configuration and real-world resources created by Terraform.
Tracking Resource Dependencies:
It tracks dependencies and relationships between different resources, enabling Terraform to manage changes efficiently without recreating resources unnecessarily.
Concurrency and Collaboration:
Terraform uses .tfstate to prevent conflicts when multiple team members are working concurrently on the same infrastructure code, ensuring changes are synchronized and applied correctly.
Reproducibility and Plan Execution:
When applying changes, Terraform refers to the .tfstate to understand the current state of infrastructure and then generates a plan for the changes needed to achieve the desired state.
Critical for Stateful Operations:
For more complex deployments or when managing stateful resources like databases, the .tfstate file is crucial. It ensures that the infrastructure remains consistent and accurate across deployments and modifications.
================================================
S3 Bucket policy
------------------
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::339712738764:user/aws-cli-user"
},
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-s3-backend-terraform123"
},
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::339712738764:user/aws-cli-user"
},
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::my-s3-backend-terraform123/*"
}
]
}
----------------------------------------------------------
Backend.tf
-------------------------
terraform {
backend "s3" {
bucket = "my-s3-backend-terraform123"
key = "terraform.tfstate"
region = "eu-west-1"
dynamodb_table = "my-terrafoorm-dynaodb-table"
}
}