Terraform -AWS Installation
Install awscli for Linux
- curl “https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip” -o “awscliv2.zip”
- unzip awscliv2.zip
- sudo ./aws/install
- aws –version
Aws Configuration (rprofile is the name of the profile)
- aws configure
- aws iam list-users
Install Terraform on ubuntu
- curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add –
- sudo apt-add-repository “deb [arch=$(dpkg –print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main”
- sudo apt install terraform
Terraform Basics
- First Terraform file
- What is statefile and its importance
- Different ways to give access to tf
- Terraform Variables and its usages
- Terraform input variables
- Terraform output variables
terraform init
terraform plan
terraform apply
terraform destroy
Terraform State file
.tfstate
Example1 – Create an empty terraform file(.tf)
- Create an empty directory called( say 01)
- Goto 01 directory (cd 01)
- Create an empty file called hello.tf
- run command terraform init
- run command terraform plan
- run command terraform apply it should create terraform.tfstate file
Example2 – Create a provider aws
- Add following code
provider “aws”{}
- run command terraform init command
- it will create .terraform directory for aws plugins
Authentication
The AWS provider offers a flexible means of providing credentials for authentication. The following methods are supported, in this order, and explained below:
- Static credentials
- Environment variables
- Shared credentials/configuration file
- CodeBuild, ECS, and EKS Roles
- EC2 Instance Metadata Service (IMDS and IMDSv2)
Static credentials
Static credentials can be provided by adding an access_key and secret_key in-line in the AWS provider block:
Usage:
provider "aws" {
region = "us-west-2"
access_key = "my-access-key"
secret_key = "my-secret-key"
}
Example (To create an EC2 instance with a key)
provider "aws" {
region = "ap-south-1"
access_key = "AKIAZYCCNWPBYU7NQN7M"
secret_key = "BeO52msn/TiT3p1YrE1xkpnMsHokSEzLiCR0ZL/u"
}
resource "aws_instance" "example" {
ami = "ami-041db4a969fe3eb68"
instance_type = "t2.micro"
key_name = "devops"
tags = {
Name = "HelloWorld"
}
}