Terraform


Terraform installation via wget

ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base/basevaribale$ wget https://releases.hashicorp.com/terraform/0.8.0/terraform_0.8.0_linux_amd64.zip

Then unzip the directory and update in bash profile.

you can strat with terraform version command to check version

ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base/basevaribale$ terraform version
Terraform v0.12.21



Installing via configuration management
There are also configuration management resources available for installing Terraform.
You can find:
• A Puppet module for Terraform.
• A Chef cookbook for Terraform.
• An Ansible role for Terraform.
• A Terraform Docker container.
Getting started


Terraform  (init/apply/plan/validate)
https://www.terraform.io/docs/providers/aws/r/instance.html

ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base$ ################INITILIZATION OF AWS IN TERRAFORM###################
ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base$ cat base.tf
provider "aws" {
region     = "ap-south-1"
access_key = "AKIAJCQWLV3LYUWPIQVQ"
secret_key = "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"
}

resource "aws_instance" "lava" {
  ami           = "ami-0d9462a653c34dab7"
  instance_type = "t2.micro"
  key_name      = "TEST"
}

resource "aws_eip" "lavaeip" {

instance = "aws_instance.lava.id"

  vpc = true


}




ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base$ terraform init

Initializing the backend...

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 2.50"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base$ terraform validate
Success! The configuration is valid.

ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_eip.lavaeip will be created
  + resource "aws_eip" "lavaeip" {
      + allocation_id     = (known after apply)
      + association_id    = (known after apply)
      + domain            = (known after apply)
      + id                = (known after apply)
      + instance          = "aws_instance.lava.id"
      + network_interface = (known after apply)
      + private_dns       = (known after apply)
      + private_ip        = (known after apply)
      + public_dns        = (known after apply)
      + public_ip         = (known after apply)
      + public_ipv4_pool  = (known after apply)
      + vpc               = true
    }

  # aws_instance.lava will be created
  + resource "aws_instance" "lava" {
      + ami                          = "ami-0d9462a653c34dab7"
      + arn                          = (known after apply)
      + associate_public_ip_address  = (known after apply)
      + availability_zone            = (known after apply)
      + cpu_core_count               = (known after apply)
      + cpu_threads_per_core         = (known after apply)
      + get_password_data            = false
      + host_id                      = (known after apply)
      + id                           = (known after apply)
      + instance_state               = (known after apply)
      + instance_type                = "t2.micro"
      + ipv6_address_count           = (known after apply)
      + ipv6_addresses               = (known after apply)
      + key_name                     = "TEST"
      + network_interface_id         = (known after apply)
      + password_data                = (known after apply)
      + placement_group              = (known after apply)
      + primary_network_interface_id = (known after apply)
      + private_dns                  = (known after apply)
      + private_ip                   = (known after apply)
      + public_dns                   = (known after apply)
      + public_ip                    = (known after apply)
      + security_groups              = (known after apply)
      + source_dest_check            = true
      + subnet_id                    = (known after apply)
      + tenancy                      = (known after apply)
      + volume_tags                  = (known after apply)
      + vpc_security_group_ids       = (known after apply)

      + ebs_block_device {
          + delete_on_termination = (known after apply)
          + device_name           = (known after apply)
          + encrypted             = (known after apply)
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + snapshot_id           = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = (known after apply)
          + volume_type           = (known after apply)
        }

      + ephemeral_block_device {
          + device_name  = (known after apply)
          + no_device    = (known after apply)
          + virtual_name = (known after apply)
        }

      + network_interface {
          + delete_on_termination = (known after apply)
          + device_index          = (known after apply)
          + network_interface_id  = (known after apply)
        }

      + root_block_device {
          + delete_on_termination = (known after apply)
          + encrypted             = (known after apply)
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = (known after apply)
          + volume_type           = (known after apply)
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base$ terraform plan -out lava.plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_eip.lavaeip will be created
  + resource "aws_eip" "lavaeip" {
      + allocation_id     = (known after apply)
      + association_id    = (known after apply)
      + domain            = (known after apply)
      + id                = (known after apply)
      + instance          = "aws_instance.lava.id"
      + network_interface = (known after apply)
      + private_dns       = (known after apply)
      + private_ip        = (known after apply)
      + public_dns        = (known after apply)
      + public_ip         = (known after apply)
      + public_ipv4_pool  = (known after apply)
      + vpc               = true
    }

  # aws_instance.lava will be created
  + resource "aws_instance" "lava" {
      + ami                          = "ami-0d9462a653c34dab7"
      + arn                          = (known after apply)
      + associate_public_ip_address  = (known after apply)
      + availability_zone            = (known after apply)
      + cpu_core_count               = (known after apply)
      + cpu_threads_per_core         = (known after apply)
      + get_password_data            = false
      + host_id                      = (known after apply)
      + id                           = (known after apply)
      + instance_state               = (known after apply)
      + instance_type                = "t2.micro"
      + ipv6_address_count           = (known after apply)
      + ipv6_addresses               = (known after apply)
      + key_name                     = "TEST"
      + network_interface_id         = (known after apply)
      + password_data                = (known after apply)
      + placement_group              = (known after apply)
      + primary_network_interface_id = (known after apply)
      + private_dns                  = (known after apply)
      + private_ip                   = (known after apply)
      + public_dns                   = (known after apply)
      + public_ip                    = (known after apply)
      + security_groups              = (known after apply)
      + source_dest_check            = true
      + subnet_id                    = (known after apply)
      + tenancy                      = (known after apply)
      + volume_tags                  = (known after apply)
      + vpc_security_group_ids       = (known after apply)

      + ebs_block_device {
          + delete_on_termination = (known after apply)
          + device_name           = (known after apply)
          + encrypted             = (known after apply)
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + snapshot_id           = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = (known after apply)
          + volume_type           = (known after apply)
        }

      + ephemeral_block_device {
          + device_name  = (known after apply)
          + no_device    = (known after apply)
          + virtual_name = (known after apply)
        }

      + network_interface {
          + delete_on_termination = (known after apply)
          + device_index          = (known after apply)
          + network_interface_id  = (known after apply)
        }

      + root_block_device {
          + delete_on_termination = (known after apply)
          + encrypted             = (known after apply)
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = (known after apply)
          + volume_type           = (known after apply)
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

This plan was saved to: lava.plan

To perform exactly these actions, run the following command to apply:
    terraform apply "lava.plan"

ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base$


ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base/basevaribale$ ls -ltr
total 20
-rw-rw-r-- 1 ubuntu ubuntu 2044 Feb 29 10:00 basevar.plan
-rw-rw-r-- 1 ubuntu ubuntu 2876 Feb 29 10:15 terraform.tfstate.backup
-rw-rw-r-- 1 ubuntu ubuntu  157 Feb 29 10:27 terraform.tfstate
-rw-rw-r-- 1 ubuntu ubuntu  540 Feb 29 10:30 var.tf
-rw-rw-r-- 1 ubuntu ubuntu  206 Feb 29 10:49 basevar.tf


***terraform.tfstate file will be take backup automatically before we execute terraform apply***

To find out the terraform version?
ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base/basevaribale$ terraform version
Terraform v0.12.21
+ provider.aws v2.51.0

Set path in linux/ubuntu

ubuntu@kubernetes-worker:~$ cat .bash_profile
export PATH=${PATH}:/home/ubuntu/terraform-project

ubuntu@kubernetes-worker:~$


****Variable's Declaration method***
ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base/basevaribale$ cat basevar.tf
provider "aws" {
access_key = var.access_key
secret_key = var.secret_key
region = var.region
}
resource "aws_instance" "TESTING" {
ami = var.ami
instance_type=var.instype
security_groups = var.secgroup
}

*****Create variable file with extension of Tf***
Note:For default type of variables no need to provide type parameter seprately.
ubuntu@kubernetes-worker:~/terraform-project/terrformcfg/base/basevaribale$ cat var.tf
variable "access_key" {
description = "The AWS access key."
default="AKIAJCQWLV3LYUWPIQVQ"
}
variable "secret_key" {
description = "The AWS secret key."
default = "InkKtKGUkRKMD4eDMaXPcfNK1h6NpPu+3hwq8w6L"
}
variable "region" {
description = "The AWS region."
default = "ap-south-1"
}
variable "ami" {
description = " the required ami "
default = "ami-0d9462a653c34dab7"
}
variable "instype" {
description = " select the instancetype"
default = "t2.micro"
}
variable "secgroup" {
description = "select the tuflee secgroup"   
type = string  ###this will ask parameter value in run time####
}

Type constraint:

it is used restrict all unwanted values and allows the required value from variable which is declared in var.tf




Type constraints are created from a mixture of type keywords and type constructors. The supported type keywords are:
The type constructors allow you to specify complex types such as collections:




The name of a variable can be any valid identifier except the following:

AES S3 bucket integrate as mount in EC2 instance

NOTE:https://howto.lintel.in/how-to-mount-aws-s3-bucket-on-linux/

After creation Ec2 instance

yum -y install automake fuse fuse-devel gcc-c++ libcurl-devel libxml2-devel make openssl-dev
el


sudo yum install git

Complete!
[root@ip-172-31-41-86 ~]# git clone https://github.com/s3fs-fuse/s3fs-fuse.git
Cloning into 's3fs-fuse'...
remote: Enumerating objects: 37, done.
remote: Counting objects: 100% (37/37), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 5876 (delta 16), reused 17 (delta 6), pack-reused 5839
Receiving objects: 100% (5876/5876), 3.53 MiB | 2.86 MiB/s, done.
Resolving deltas: 100% (4067/4067), done.
[root@ip-172-31-41-86 ~]# ^C
[root@ip-172-31-41-86 ~]# ls
s3fs-fuse
[root@ip-172-31-41-86 ~]# cd s*
[root@ip-172-31-41-86 s3fs-fuse]# ls
AUTHORS  autogen.sh  ChangeLog  COMPILATION.md  configure.ac  COPYING  doc  INSTALL  Makefile.am  README.md  src  test
[root@ip-172-31-41-86 s3fs-fuse]#  ./autogen.sh
--- Make commit hash file -------
--- Finished commit hash file ---
--- Start autotools -------------
configure.ac:26: installing './config.guess'
configure.ac:26: installing './config.sub'
configure.ac:27: installing './install-sh'
configure.ac:27: installing './missing'
src/Makefile.am: installing './depcomp'
parallel-tests: installing './test-driver'
--- Finished autotools ----------
[root@ip-172-31-41-86 s3fs-fuse]# ./configure
checking for sys/xattr.h... yes
checking attr/xattr.h usability... no
checking attr/xattr.h presence... no
checking for attr/xattr.h... no
checking sys/extattr.h usability... no
checking sys/extattr.h presence... no
checking for sys/extattr.h... no
checking s3fs build with nettle(GnuTLS)... no
checking s3fs build with OpenSSL... no
checking s3fs build with GnuTLS... no
checking s3fs build with NSS... no
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for common_lib_checking... yes
checking compile s3fs with... OpenSSL
checking for DEPS... no
configure: error: Package requirements (fuse >= 2.8.4 libcurl >= 7.0 libxml-2.0 >= 2.6 libcrypto >= 0.9 ) were not met:

No package 'libcrypto' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables DEPS_CFLAGS
and DEPS_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
[root@ip-172-31-41-86 s3fs-fuse]# make**FAILED**

mkdir -p /var/s3fs-demo-fs
[root@ip-172-31-41-86 s3fs-fuse]#
[root@ip-172-31-41-86 s3fs-fuse]# aws s3 mb s3://s3fs-demobk
make_bucket: s3fs-demobk
[root@ip-172-31-41-86 s3fs-fuse]# s3fs s3fs-demobk /var/s3fs-demo-fs -o iam_role-ec2t
os3
-bash: s3fs: command not found
[root@ip-172-31-41-86 s3fs-fuse]# bash
[root@ip-172-31-41-86 s3fs-fuse]# s3fs
bash: s3fs: command not found

###FIX S3FS NOT FOUND###
yum install gcc libstdc++-devel gcc-c++ curl-devel libxml2-devel openssl-devel mailcap fuse fuse-devel
wget http://s3fs.googlecode.com/files/s3fs-1.61.tar.gz
tar xvzf s3fs-1.61.tar.gz
cd s3fs-1.61/
./configure --prefix=/usr
make
make install
vim  /etc/passwd-s3fs
chmod 640 /etc/passwd-s3fs
s3fs yourBucketNameHere /mnt


s3fs s3fs-demobk /var/s3fs-demo-fs -o iam_role_ec2tos3

to fix passwd-s3fs issue need to enter the accessid and key id


Access Key ID:AKIAJVTBI7PNBULL4XSA
Secret Access Key:OFKq1LcUkuBQ0BYF9oenCkZ/+EoQ8SrScwlD6Y2i

AKIAJVTBI7PNBULL4XSA:OFKq1LcUkuBQ0BYF9oenCkZ/+EoQ8SrScwlD6Y2i

s3fs s3fs-demobk /var/s3fs-demo-fs -o asswd_file=/etc/passwd-s3fs

syntax --->s3fs mybucketname /path/to/mountpoint -o passwd_file=/etc/passwd-s3fs