diff --git a/Days/day56.md b/Days/day56.md index e69de29..154a334 100644 --- a/Days/day56.md +++ b/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.
+ +For instance: An Infrastructure for a car mainly includes Wheels, Engine, Steering, Clutch, brake, Accelerator and so on.
+ +In similar way, for creating a cloud infrastructure, there is also some of its main building blocks,
+ + +# What is terraform?
+ +Terraform is an infrastructure as code (IaC) tool that allows you to build, change and version infrastructure safely and efficiently.
+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.
+- 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
+ +- 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.
+ +- 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.
+``` + 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.
+ +#### 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" +} +``` diff --git a/Tools/8.IaC/Tarraform/README.md b/Tools/8.IaC/Tarraform/README.md new file mode 100644 index 0000000..e62be90 --- /dev/null +++ b/Tools/8.IaC/Tarraform/README.md @@ -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
+* cd test
+* go mod init ""
+* **MODULE_NAME would be github.com//**
+* go mod init github.com/imnitin28/terra-form-test-techhub
+* go run + +-------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +go mod init "" would create go.mod file into test folder.
+* 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. + diff --git a/Tools/8.IaC/Tarraform/awsResource/instance.tf b/Tools/8.IaC/Tarraform/awsResource/instance.tf new file mode 100644 index 0000000..5a9db2d --- /dev/null +++ b/Tools/8.IaC/Tarraform/awsResource/instance.tf @@ -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 = < index.html +nohup busybox httpd -f -p 8080 & +EOF +} \ No newline at end of file diff --git a/Tools/8.IaC/Tarraform/awsResource/output.tf b/Tools/8.IaC/Tarraform/awsResource/output.tf new file mode 100644 index 0000000..752bb05 --- /dev/null +++ b/Tools/8.IaC/Tarraform/awsResource/output.tf @@ -0,0 +1,4 @@ +# Output the instance's public IP address. +output "public_ip" { + value = aws_instance.example.public_ip +} \ No newline at end of file diff --git a/Tools/8.IaC/Tarraform/awsResource/provider.tf b/Tools/8.IaC/Tarraform/awsResource/provider.tf new file mode 100644 index 0000000..696b517 --- /dev/null +++ b/Tools/8.IaC/Tarraform/awsResource/provider.tf @@ -0,0 +1,6 @@ +provider "aws" { + access_key = var.AWS_ACCESS_KEY + secret_key = var.AWS_SECRET_KEY + region = var.AWS_REGION +} + diff --git a/Tools/8.IaC/Tarraform/awsResource/securitygroup.tf b/Tools/8.IaC/Tarraform/awsResource/securitygroup.tf new file mode 100644 index 0000000..d562dee --- /dev/null +++ b/Tools/8.IaC/Tarraform/awsResource/securitygroup.tf @@ -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"] + } +} \ No newline at end of file diff --git a/Tools/8.IaC/Tarraform/awsResource/terraform.tfvars b/Tools/8.IaC/Tarraform/awsResource/terraform.tfvars new file mode 100644 index 0000000..dcc3f3b --- /dev/null +++ b/Tools/8.IaC/Tarraform/awsResource/terraform.tfvars @@ -0,0 +1,3 @@ +AWS_ACCESS_KEY = "XXXX" +AWS_SECRET_KEY = "XXXXXXXX" +AWS_REGION="ap-south-1" diff --git a/Tools/8.IaC/Tarraform/awsResource/vars.tf b/Tools/8.IaC/Tarraform/awsResource/vars.tf new file mode 100644 index 0000000..9316302 --- /dev/null +++ b/Tools/8.IaC/Tarraform/awsResource/vars.tf @@ -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" + } +} + diff --git a/Tools/8.IaC/Tarraform/awsResource/versions.tf b/Tools/8.IaC/Tarraform/awsResource/versions.tf new file mode 100644 index 0000000..0ac1e24 --- /dev/null +++ b/Tools/8.IaC/Tarraform/awsResource/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 0.12.26" +} \ No newline at end of file diff --git a/Tools/8.IaC/Tarraform/test/terraform_test.go b/Tools/8.IaC/Tarraform/test/terraform_test.go new file mode 100644 index 0000000..3b800c0 --- /dev/null +++ b/Tools/8.IaC/Tarraform/test/terraform_test.go @@ -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) +}