Update Structure and added IaC codes
This commit is contained in:
parent
7f430b7b4b
commit
4ffbf04269
135
Days/day56.md
135
Days/day56.md
@ -0,0 +1,135 @@
|
|||||||
|
## Understanding Infrastructure as a Code aka IaC and Terraform Basics
|
||||||
|
|
||||||
|
An Infrastructure in common language would be considered as the final output obtained after processing multiple different kinds of inputs. </br>
|
||||||
|
|
||||||
|
For instance: An Infrastructure for a car mainly includes Wheels, Engine, Steering, Clutch, brake, Accelerator and so on. </br>
|
||||||
|
|
||||||
|
In similar way, for creating a cloud infrastructure, there is also some of its main building blocks,</br>
|
||||||
|
|
||||||
|
|
||||||
|
# What is terraform? </br>
|
||||||
|
|
||||||
|
Terraform is an infrastructure as code (IaC) tool that allows you to build, change and version infrastructure safely and efficiently. </br>
|
||||||
|
This includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. </br>
|
||||||
|
- Terraform can manage both existing service providers and custom in-house solutions.
|
||||||
|
- For writing configuration in terraform we use HCL.
|
||||||
|
- Hashicorp Configuration language (HCL)
|
||||||
|
- HCL is a toolkit for creating structured configuration languages that are both human and machine-friendly, for use with command-line tools.
|
||||||
|
- Although intended to be generally useful, it is primarily targeted towards devops tools, servers, etc.
|
||||||
|
|
||||||
|
#### A simple HCL code would look something like this,
|
||||||
|
|
||||||
|
```
|
||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
aws = {
|
||||||
|
source = "hashicorp/aws"
|
||||||
|
version = "~> 3.27"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
required_version = ">= 0.14.9"
|
||||||
|
}
|
||||||
|
provider "aws" {
|
||||||
|
profile = "default"
|
||||||
|
region = "us-west-2"
|
||||||
|
}
|
||||||
|
resource "aws_instance" "app_server" {
|
||||||
|
ami = "ami-830c94e3"
|
||||||
|
instance_type = "t2.micro"
|
||||||
|
|
||||||
|
tags = {
|
||||||
|
Name = "MyFirstTerraformConfiguration"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Key Components of Cloud Infrastructure
|
||||||
|
- Compute
|
||||||
|
|
||||||
|
Compute services are for running diverse workloads on the AWS Compute platform.
|
||||||
|
Most importantly it helps to lower the infrastructure costs and accelerate innovation on the world’s most reliable, secure, and capable cloud.
|
||||||
|
|
||||||
|
We are mainly going to use EC2 services in our upcoming blogs.
|
||||||
|
|
||||||
|
- EC2
|
||||||
|
|
||||||
|
Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale computing easier for developers.
|
||||||
|
resource "aws_instance" "example" {
|
||||||
|
ami = var.AMIS[var.AWS_REGION]
|
||||||
|
instance_type = "t2.micro"
|
||||||
|
}
|
||||||
|
|
||||||
|
- Network and Content Delivery
|
||||||
|
Use cases for Network and Content Delivery </br>
|
||||||
|
|
||||||
|
- Networking foundations : It helps us to quickly set up, secure, and monitor our network.
|
||||||
|
- Application networking : Provides traditional and modern applications with improved security, availability, performance, and streamlined monitoring.
|
||||||
|
- Edge networking : Deliver your data with single-digit millisecond latency.
|
||||||
|
- Hybrid connectivity : Create fast, secure, and reliable connections between your on-premises and AWS networks.
|
||||||
|
|
||||||
|
|
||||||
|
We are mainly going to use VPC services in our upcoming blogs. </br>
|
||||||
|
|
||||||
|
- VPC
|
||||||
|
VPC stands for Virtual Private Cloud, using VPC we define and provision a logically isolated network for our AWS resources.
|
||||||
|
```
|
||||||
|
resource "aws_vpc" "main" {
|
||||||
|
cidr_block = "10.0.0.0/16"
|
||||||
|
instance_tenancy = "default"
|
||||||
|
enable_dns_support = "true"
|
||||||
|
enable_dns_hostnames = "true"
|
||||||
|
enable_classiclink = "false"
|
||||||
|
tags = {
|
||||||
|
Name = "main"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- Storage
|
||||||
|
|
||||||
|
Storage is a complete range of services for us to store, access, govern, and analyse our data to reduce costs, increase agility, and accelerate innovation.
|
||||||
|
With using cloud storage we can reduce the storage costs.
|
||||||
|
|
||||||
|
- S3
|
||||||
|
|
||||||
|
Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. </br>
|
||||||
|
```
|
||||||
|
resource "aws_s3_bucket" "b" {
|
||||||
|
bucket = "nitin1455"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- Security, Identity and Compliance
|
||||||
|
Security, Identity and Compliance are for securing our workloads and applications in the cloud. </br>
|
||||||
|
|
||||||
|
#### Some of it’s use cases are :
|
||||||
|
|
||||||
|
- Data protection
|
||||||
|
- Identity & access management
|
||||||
|
- Network & application protection
|
||||||
|
- Threat detection & continuous monitoring
|
||||||
|
- Compliance & data privacy
|
||||||
|
|
||||||
|
- IAM
|
||||||
|
|
||||||
|
IAM is for securely managing access to services and resources.
|
||||||
|
|
||||||
|
Using IAM, we can create and manage AWS users and groups, and use permissions to allow and deny their access to AWS resources.
|
||||||
|
```
|
||||||
|
resource "aws_iam_group" "administrators" {
|
||||||
|
name = "administrators"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```
|
||||||
|
resource "aws_iam_policy_attachment" "administrators-attach" {
|
||||||
|
name = "administrators-attach"
|
||||||
|
groups = [aws_iam_group.administrators.name]
|
||||||
|
policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
# user
|
||||||
|
```
|
||||||
|
resource "aws_iam_user" "admin1" {
|
||||||
|
name = "admin1"
|
||||||
|
}
|
||||||
|
```
|
25
Tools/8.IaC/Tarraform/README.md
Normal file
25
Tools/8.IaC/Tarraform/README.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# TerratestWithTerraform
|
||||||
|
|
||||||
|
Terratest is a Go library developed at Gruntwork, that makes it easier to write automated tests for our infrastructure code. It provides a variety of helper functions and patterns for common infrastructure testing tasks but here we will be discussing about Testing Terraform code.
|
||||||
|
|
||||||
|
# To Run this application
|
||||||
|
* git clone https://github.com/imnitin28/terra-form-test-techhub.git <br />
|
||||||
|
* cd test <br />
|
||||||
|
* go mod init "<MODULE_NAME>" <br />
|
||||||
|
* **MODULE_NAME would be github.com/<YOUR_USERNAME>/<YOUR_REPO_NAME>** <br />
|
||||||
|
* go mod init github.com/imnitin28/terra-form-test-techhub <br />
|
||||||
|
* go run
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
go mod init "<MODULE_NAME>" would create go.mod file into test folder. <br />
|
||||||
|
* The go.mod file is the root of dependency management in GoLang.
|
||||||
|
* All the modules which are needed or to be used in the project are maintained here in go.mod file.
|
||||||
|
* It creates entry for all the packages we are going to use/import in our project.
|
||||||
|
* It reduces effort for getting each dependencies manually.
|
||||||
|
|
||||||
|
On running **go test** for the first time you would get go.sum file created.
|
||||||
|
* go.sum file is created when **go test** or **go build** is executed for the first time.
|
||||||
|
* It installs all the packages with specific version(latest)
|
||||||
|
* we do not need to edit or modify this file.
|
||||||
|
|
12
Tools/8.IaC/Tarraform/awsResource/instance.tf
Normal file
12
Tools/8.IaC/Tarraform/awsResource/instance.tf
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
resource "aws_instance" "example" {
|
||||||
|
ami = var.AMIS[var.AWS_REGION]
|
||||||
|
instance_type = "t2.micro"
|
||||||
|
vpc_security_group_ids = [aws_security_group.instance.id]
|
||||||
|
|
||||||
|
# When the instance boots, start a web server on port 8080 that responds with "Hello, World!".
|
||||||
|
user_data = <<EOF
|
||||||
|
#!/bin/bash
|
||||||
|
echo "Hello, World!" > index.html
|
||||||
|
nohup busybox httpd -f -p 8080 &
|
||||||
|
EOF
|
||||||
|
}
|
4
Tools/8.IaC/Tarraform/awsResource/output.tf
Normal file
4
Tools/8.IaC/Tarraform/awsResource/output.tf
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# Output the instance's public IP address.
|
||||||
|
output "public_ip" {
|
||||||
|
value = aws_instance.example.public_ip
|
||||||
|
}
|
6
Tools/8.IaC/Tarraform/awsResource/provider.tf
Normal file
6
Tools/8.IaC/Tarraform/awsResource/provider.tf
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
provider "aws" {
|
||||||
|
access_key = var.AWS_ACCESS_KEY
|
||||||
|
secret_key = var.AWS_SECRET_KEY
|
||||||
|
region = var.AWS_REGION
|
||||||
|
}
|
||||||
|
|
9
Tools/8.IaC/Tarraform/awsResource/securitygroup.tf
Normal file
9
Tools/8.IaC/Tarraform/awsResource/securitygroup.tf
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Allow the instance to receive requests on port 8080.
|
||||||
|
resource "aws_security_group" "instance" {
|
||||||
|
ingress {
|
||||||
|
from_port = 8080
|
||||||
|
to_port = 8080
|
||||||
|
protocol = "tcp"
|
||||||
|
cidr_blocks = ["0.0.0.0/0"]
|
||||||
|
}
|
||||||
|
}
|
3
Tools/8.IaC/Tarraform/awsResource/terraform.tfvars
Normal file
3
Tools/8.IaC/Tarraform/awsResource/terraform.tfvars
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
AWS_ACCESS_KEY = "XXXX"
|
||||||
|
AWS_SECRET_KEY = "XXXXXXXX"
|
||||||
|
AWS_REGION="ap-south-1"
|
17
Tools/8.IaC/Tarraform/awsResource/vars.tf
Normal file
17
Tools/8.IaC/Tarraform/awsResource/vars.tf
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
variable "AWS_ACCESS_KEY" {
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "AWS_SECRET_KEY" {
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "AWS_REGION" {
|
||||||
|
default = "ap-south-1"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "AMIS" {
|
||||||
|
type = map(string)
|
||||||
|
default = {
|
||||||
|
ap-south-1 = "ami-0860c9429baba6ad2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
3
Tools/8.IaC/Tarraform/awsResource/versions.tf
Normal file
3
Tools/8.IaC/Tarraform/awsResource/versions.tf
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
terraform {
|
||||||
|
required_version = ">= 0.12.26"
|
||||||
|
}
|
34
Tools/8.IaC/Tarraform/test/terraform_test.go
Normal file
34
Tools/8.IaC/Tarraform/test/terraform_test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
|
||||||
|
|
||||||
|
"github.com/gruntwork-io/terratest/modules/terraform"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTerraformAwsHelloWorldExample(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
/*Construct the terraform options with default retryable errors to handle the most common retryable errors in terraform testing. */
|
||||||
|
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
|
||||||
|
/* The path to where our Terraform code is located */
|
||||||
|
TerraformDir: "../awsResource",
|
||||||
|
})
|
||||||
|
|
||||||
|
/* At the end of the test, run `terraform destroy` to clean up any resources that were created.*/
|
||||||
|
defer terraform.Destroy(t, terraformOptions)
|
||||||
|
|
||||||
|
/* Run `terraform init` and `terraform apply`. Fail the test if there are any errors. */
|
||||||
|
terraform.InitAndApply(t, terraformOptions)
|
||||||
|
|
||||||
|
/* Run `terraform output` to get the IP of the instance */
|
||||||
|
publicIp := terraform.Output(t, terraformOptions, "public_ip")
|
||||||
|
|
||||||
|
/* Make an HTTP request to the instance and make sure we get back a 200 OK with the body "Hello, World!" */
|
||||||
|
url := fmt.Sprintf("http://%s:8080", publicIp)
|
||||||
|
http_helper.HttpGetWithRetry(t, url, nil, 200, "Happy, Learnings!", 30, 5*time.Second)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user