Skip to content

Commit 4086dd0

Browse files
authored
Add files via upload
first commit
0 parents  commit 4086dd0

File tree

100 files changed

+3163
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+3163
-0
lines changed

01 - Introduction/Key-concepts.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## **Introduction to Terraform**
2+
3+
Key Concepts:
4+
a) **Infrastructure as a Code**
5+
- Managing and provisioning of infrastructure through code instead of manually
6+
- Files that contain configurations are created and makes it easy to edit and distribute
7+
- Ensures that the same environment is provisioned everytime.
8+
- Helps avoid undocumented, ad-hoc configuration changes
9+
- Easy to version control
10+
- Can create infrastructure in modular components
11+
- Gives you a template to follow for provisioning
12+
b) **Benefits:**
13+
- Cost reduction
14+
- Increase in speed of deployment
15+
- Reduce errors
16+
- Improve infrastructure consistency
17+
- Eliminate configuration drift
18+
19+
##**What is Terraform?:**
20+
- IaaC tool from Hashicorp
21+
- Used for building, changing and managing infrastructure in a safe, repeatable way
22+
- Uses HCL - Hashicorp Configuration Language - human readable
23+
- Reads configuration files and provides an execution plan which can be reviewed before being applied.
24+
- It is platform agnostic - can manage a heterogeneous environment - multi cloud
25+
- State management - creates a state file when a project is first initialized. Uses this state file to create plans and make changes based on the desired and current state of the infrastructure.
26+
- Creates operator confidence
27+
28+
##**Terraform Configuration Files**
29+
- Terraform uses declarative syntax to describe your Infrastructure as Code (IaC) infrastructure
30+
and then persist it in configuration files that can be shared, reviewed, edited, versioned,
31+
preserved, and reused.
32+
- Terraform configuration files can use either of two formats: Terraform domain-specific
33+
language (HashiCorpConfiguration Language format [HCL]), which is the recommended
34+
approach, or JSON format if the files need to be machine-readable.
35+
- Configuration files that use the HCL format end with the .tf file extension;
36+
- Those using JSON format end with the .tf.json file extension.
37+
- The Terraform format is human-readable, while the JSON format is machine readable

01 - Introduction/ReadME.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Terraform & AWS CLI Installation
2+
3+
## A) Prerequisites
4+
- Install Terraform CLI
5+
- Install AWS CLI
6+
- Install VS Code Editor - recommended for this course
7+
- Install HashiCorp Terraform plugin for VS Code - recommended
8+
9+
10+
## B) MACOS - Terraform Install
11+
- [Download Terraform MAC](https://www.terraform.io/downloads.html)
12+
- [Install CLI](https://learn.hashicorp.com/tutorials/terraform/install-cli)
13+
- Unzip the package
14+
```
15+
# Copy binary zip file to a folder
16+
mkdir /Users/<YOUR-USER>/Documents/terraform-install
17+
COPY Package to "terraform-install" folder
18+
19+
# Unzip
20+
unzip <PACKAGE-NAME>
21+
unzip terraform_1.0.10_darwin_amd64.zip
22+
23+
# Copy terraform binary to /usr/local/bin
24+
echo $PATH
25+
mv terraform /usr/local/bin
26+
27+
# Verify Version
28+
terraform version
29+
30+
# To Uninstall Terraform (NOT REQUIRED)
31+
rm -rf /usr/local/bin/terraform
32+
```
33+
34+
## C) MACOS - Install VSCode Editor and terraform plugin
35+
- [Microsoft Visual Studio Code Editor](https://code.visualstudio.com/download)
36+
- [Hashicorp Terraform Plugin for VS Code](https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform)
37+
38+
39+
### D) MACOS - Install AWS CLI
40+
- [AWS CLI Install](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)
41+
- [Install AWS CLI - MAC](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-mac.html#cliv2-mac-install-cmd)
42+
43+
```
44+
# Install AWS CLI V2
45+
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
46+
sudo installer -pkg AWSCLIV2.pkg -target /
47+
which aws
48+
aws --version
49+
50+
# Uninstall AWS CLI V2 (NOT REQUIRED)
51+
which aws
52+
ls -l /usr/local/bin/aws
53+
sudo rm /usr/local/bin/aws
54+
sudo rm /usr/local/bin/aws_completer
55+
sudo rm -rf /usr/local/aws-cli
56+
```
57+
58+
59+
## E) MACOS - Configure AWS Credentials
60+
- **Pre-requisite:** Should have AWS Account.
61+
- [Create an AWS Account](https://portal.aws.amazon.com/billing/signup?nc2=h_ct&src=header_signup&redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#/start)
62+
63+
- **Role**:
64+
-If your terraform server is in the cloud, then create a role and attach the role to your server.
65+
66+
67+
- Generate Security Credential s using AWS Management Console
68+
- Go to Services -> IAM -> Users -> "Your-Admin-User" -> Security Credentials -> Create Access Key
69+
- Configure AWS credentials using SSH Terminal on your local desktop
70+
71+
# **Configure AWS Credentials in command line**
72+
```
73+
$ aws configure
74+
AWS Access Key ID [None]: AKIASUF7DEFKSIAWMZ7K
75+
AWS Secret Access Key [None]: WL9G9Tl8lGm7w9t7B3NEDny1+w3N/K5F3HWtdFH/
76+
Default region name [None]: us-west-2
77+
Default output format [None]: json
78+
79+
# Verify if we are able list S3 buckets
80+
aws s3 ls
81+
```
82+
- Verify the AWS Credentials Profile
83+
```
84+
cat $HOME/.aws/credentials
85+
```
86+
#**Command to reset your AWS credentials incase of a credentials error**:
87+
88+
$ for var in AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_SECURITY_TOKEN ; do eval unset $var ; done
89+
90+
## F) Windows OS - Terraform & AWS CLI Install
91+
- [Download Terraform](https://www.terraform.io/downloads.html)
92+
- [Install CLI](https://learn.hashicorp.com/tutorials/terraform/install-cli)
93+
- Unzip the package
94+
- Create new folder `binaries`
95+
- Copy the `terraform.exe` to a `binaries`
96+
- Set PATH in windows
97+
**How to set the windows path: Windows 8/10**
98+
In Search, search for and then select:
99+
System (Control Panel)
100+
Click the Advanced system settings link.
101+
Click Environment Variables.
102+
In the section System Variables find the PATH environment variable and select it.
103+
Click Edit. If the PATH environment variable does not exist, click New.
104+
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable.
105+
Click OK. Close all remaining windows by clicking OK.
106+
107+
- Install [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)
108+
109+
## Terraform install on windows using a packet manager
110+
-Install terraform on windows using the windows package manager(Use powershell and install as administrator).
111+
**$ choco install terraform**
112+
113+
## G) Linux OS - Terraform & AWS CLI Install
114+
- [Download Terraform](https://www.terraform.io/downloads.html)
115+
- [Linux OS - Terraform Install](https://learn.hashicorp.com/tutorials/terraform/install-cli)
116+
117+
# Install Terraform on Ubuntu:
118+
$sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
119+
$curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
120+
$sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
121+
$sudo apt-get update && sudo apt-get install terraform
122+
123+
# Install Terraform on RHEL:
124+
**Install aws cli**
125+
sudo yum update -y
126+
sudo yum install curl unzip wget -y
127+
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
128+
unzip awscliv2.zip
129+
sudo ./aws/install
130+
131+
**Install Terraform**
132+
a) *Download binary*
133+
sudo yum update -y
134+
sudo yum install wget unzip -y
135+
sudo wget https://releases.hashicorp.com/terraform/1.4.4/terraform_1.1.4_linux_amd64.zip
136+
sudo unzip terraform_1.1.4_linux_amd64.zip -d /usr/local/bin
137+
terraform -v
138+
139+
b) *Install from hashicorp repo*
140+
sudo yum install -y yum-utils
141+
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
142+
sudo yum -y install terraform

02 - Command basics/Commands.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## **Terraform basic commands**
2+
3+
#**terraform init**
4+
• initializes a working directory containing Terraform configuration files.
5+
• performs
6+
- backend initialization
7+
- storage for terraform state file.
8+
- modules installation,
9+
- downloaded from terraform registry to local path
10+
- provider(s) plugins installation,
11+
- the plugins are downloaded in the sub-directory of the present working directory at the path of .terraform/plugins
12+
• supports -upgrade to update all previously installed plugins to the newest version that complies with the configuration’s version constraints
13+
• is safe to run multiple times, to bring the working directory up to date with changes in the configuration
14+
• does not delete the existing configuration or state
15+
16+
# **terraform validate**
17+
• validates syntactically for format and correctness.
18+
• is used to validate/check the syntax of the Terraform files.
19+
• verifies whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state.
20+
• A syntax check is done on all the terraform files in the directory, and will display an error if any of the files doesn’t validate.
21+
22+
# **terraform plan**
23+
• create a execution plan
24+
• traverses each vertex and requests each provider using parallelism
25+
• calculates the difference between the last-known state and
26+
the current state and presents this difference as the output of the terraform plan operation to user in their terminal
27+
• does not modify the infrastructure or state.
28+
• allows a user to see which actions Terraform will perform prior to making any changes to reach the desired state
29+
• will scan all *.tf  files in the directory and create the plan
30+
• will perform refresh for each resource and might hit rate limiting issues as it calls provider APIs
31+
• all resources refresh can be disabled or avoided using
32+
-refresh=false or
33+
target=xxxx or
34+
break resources into different directories.
35+
• supports -out to save the plan
36+
37+
#**terraform apply**
38+
• apply changes to reach the desired state.
39+
• scans the current directory for the configuration and applies the changes appropriately.
40+
• can be provided with a explicit plan, saved as out from terraform plan
41+
• If no explicit plan file is given on the command line, terraform apply will create a new plan automatically
42+
and prompt for approval to apply it
43+
• will modify the infrastructure and the state.
44+
• if a resource successfully creates but fails during provisioning,
45+
- Terraform will error and mark the resource as “tainted”.
46+
- A resource that is tainted has been physically created, but can’t be considered safe to use since provisioning failed.
47+
- Terraform also does not automatically roll back and destroy the resource during the apply when the failure happens, because that would go against the execution plan: the execution plan would’ve said a resource will be created, but does not say it will ever be deleted.
48+
• does not import any resource.
49+
• supports -auto-approve to apply the changes without asking for a confirmation
50+
• supports -target to apply a specific module
51+
52+
#**terraform refresh**
53+
• used to reconcile the state Terraform knows about (via its state file) with the real-world infrastructure
54+
• does not modify infrastructure, but does modify the state file
55+
destroy
56+
• destroy the infrastructure and all resources
57+
• modifies both state and infrastructure
58+
• terraform destroy -target can be used to destroy targeted resources
59+
• terraform plan -destroy allows creation of destroy plan
60+
61+
#**terraform import**
62+
• helps import already-existing external resources, not managed by Terraform, into Terraform state and allow it to manage those resources
63+
• Terraform is not able to auto-generate configurations for those imported modules, for now, and requires you to first write the resource definition in Terraform and then import this resource
64+
65+
#**terraform taint**
66+
• marks a Terraform-managed resource as tainted, forcing it to be destroyed and recreated on the next apply.
67+
• will not modify infrastructure, but does modify the state file in order to mark a resource as tainted. Infrastructure and state are changed in next apply.
68+
• can be used to taint a resource within a module
69+
70+
#**terraform fmt**
71+
• format to lint the code into a standard format
72+
73+
#**terraform console**
74+
• command provides an interactive console for evaluating expressions.

02 - Command basics/Credentials.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The AWS provider offers a flexible means of providing credentials for authentication. The following methods are supported, in this order, and explained below:
2+
3+
- **Static credentials**
4+
provider "aws" {
5+
region = "us-west-2"
6+
access_key = "my-access-key"
7+
secret_key = "my-secret-key"
8+
}
9+
10+
- **Environment variables**
11+
$ export AWS_ACCESS_KEY_ID="accesskey"
12+
$ export AWS_SECRET_ACCESS_KEY="secretkey"
13+
$ export AWS_DEFAULT_REGION="us-west-2"
14+
15+
- **Shared credentials/configuration file**
16+
provider "aws" {
17+
region = "us-west-2"
18+
shared_credentials_file = "/Users/tf_user/.aws/creds"
19+
profile = "dev"
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Solution for project 1
2+
1) Create a new folder called: Project 1
3+
2) Create a VPC named "FirstVPC"
4+
3) CIDR range : 192.168.0.0/24
5+
=================================================================
6+
7+
# Terraform Settings Block
8+
terraform {
9+
required_version = "~> 1.0"
10+
required_providers {
11+
aws = {
12+
source = "hashicorp/aws"
13+
version = "~> 3.0"
14+
# Optional but recommended in production
15+
}
16+
}
17+
}
18+
19+
# Provider Block
20+
provider "aws" {
21+
profile = "default"
22+
region = "us-west-2"
23+
}
24+
25+
# Resource Block
26+
27+
resource "aws_vpc" "project1vpc" {
28+
cidr_block = "192.168.0.0/24"
29+
tags = {
30+
Name = "FirstVPC"
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Terraform Settings Block
2+
terraform {
3+
required_version = "~> 1.0"
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 3.0"
8+
# Optional but recommended in production
9+
}
10+
}
11+
}
12+
13+
# Provider Block
14+
provider "aws" {
15+
profile = "default"
16+
region = "us-west-2"
17+
}
18+
19+
# Resource Block
20+
21+
resource "aws_vpc" "project1vpc" {
22+
cidr_block = "192.168.0.0/24"
23+
tags = {
24+
Name = "FirstVPC"
25+
}
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# A simple project
2+
1) Create a new folder /directory called: Project 1
3+
2) Create a VPC named "FirstVPC"
4+
3) CIDR range : 192.168.0.0/24

0 commit comments

Comments
 (0)