├── Ansible ├── README.md └── deploy_nginx_apache2.yaml ├── Docker ├── Docker-I Assignment-1_Completed.pdf ├── Docker-I_Assignment-2_Completed.pdf ├── Docker-I_Assignment-3_Completed.pdf ├── Docker-I_Assignment_4_Completed.pdf ├── Docker-I_Assignment_5_Completed.pdf └── Dockerfile ├── Jenkins ├── Assignment_1_Jenkins_Completed.pdf ├── Assignment_2_Jenkins.pdf ├── Assignment_3_Jenkins.pdf └── ReadMe.md ├── Kubernetes ├── CASE_STUDY_Introduction_to_Kuberentes.pdf ├── Kubernetes_3_Node_Cluster.pdf ├── apache-deployment.yaml ├── custom-deployment.yaml ├── ingress.yaml └── kubeadm-cluster.md ├── README.md └── Terraform ├── AWS_Infra ├── README.md ├── main.tf └── var.tf ├── Terraform_Assignment-1.pdf ├── Terraform_Assignment4.pdf ├── Terraform_Assignment5.pdf ├── Terraform_Assignment_2.pdf ├── Terraform_Assignment_3.pdf ├── Terraform_CASE_STUDY.pdf ├── assignment1 ├── main.tf └── var.tf ├── assignment2 └── main.tf ├── assignment3 └── main.tf ├── assignment4 └── main.tf └── main.tf /Ansible/README.md: -------------------------------------------------------------------------------- 1 | ## Ansible Tasks ## 2 | - Use the sample website present in the GitHub repository, the link for which is mentioned below. 3 | - https://github.com/Sameer-8080/Website-PRT-ORG.git 4 | - Clone this repository in the Ansible master machine to use for the next task. 5 | - Create the Ansible script to install nginx in one of the Slaves and apache2 in the other, and replace the original index.html files for both the machines with the website in the GitHub link that will have been cloned in the previous step. 6 | -------------------------------------------------------------------------------- /Ansible/deploy_nginx_apache2.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Clone website repository 3 | hosts: ansible-master 4 | become: yes 5 | tasks: 6 | - name: Clone website repository 7 | git: 8 | repo: https://github.com/Sameer-8080/Website-PRT-ORG.git 9 | dest: /tmp/website 10 | 11 | - name: Install and configure web servers 12 | hosts: nginx:apache 13 | become: yes 14 | tasks: 15 | - name: Install nginx on nginx server 16 | apt: 17 | name: nginx 18 | state: present 19 | when: "'nginx' in inventory_hostname" 20 | 21 | - name: Install apache2 on apache server 22 | apt: 23 | name: apache2 24 | state: present 25 | when: "'apache' in inventory_hostname" 26 | 27 | - name: Copy website files to nginx server 28 | synchronize: 29 | src: /tmp/website/ 30 | dest: /var/www/html/ 31 | when: "'nginx' in inventory_hostname" 32 | notify: Restart Nginx 33 | 34 | - name: Copy website files to apache server 35 | synchronize: 36 | src: /tmp/website/ 37 | dest: /var/www/html/ 38 | when: "'apache' in inventory_hostname" 39 | notify: Restart Apache 40 | 41 | handlers: 42 | - name: Restart Nginx 43 | service: 44 | name: nginx 45 | state: restarted 46 | 47 | - name: Restart Apache 48 | service: 49 | name: apache2 50 | state: restarted 51 | -------------------------------------------------------------------------------- /Docker/Docker-I Assignment-1_Completed.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Docker/Docker-I Assignment-1_Completed.pdf -------------------------------------------------------------------------------- /Docker/Docker-I_Assignment-2_Completed.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Docker/Docker-I_Assignment-2_Completed.pdf -------------------------------------------------------------------------------- /Docker/Docker-I_Assignment-3_Completed.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Docker/Docker-I_Assignment-3_Completed.pdf -------------------------------------------------------------------------------- /Docker/Docker-I_Assignment_4_Completed.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Docker/Docker-I_Assignment_4_Completed.pdf -------------------------------------------------------------------------------- /Docker/Docker-I_Assignment_5_Completed.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Docker/Docker-I_Assignment_5_Completed.pdf -------------------------------------------------------------------------------- /Docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu 2 | RUN apt-get update 3 | RUN DEBAIN_FRONTEND="noninteractive" apt-get install tzdata -y 4 | RUN apt-get install apache2 -y 5 | COPY index.html /var/www/html/ 6 | ENTRYPOINT apachectl -D FOREGROUND 7 | -------------------------------------------------------------------------------- /Jenkins/Assignment_1_Jenkins_Completed.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Jenkins/Assignment_1_Jenkins_Completed.pdf -------------------------------------------------------------------------------- /Jenkins/Assignment_2_Jenkins.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Jenkins/Assignment_2_Jenkins.pdf -------------------------------------------------------------------------------- /Jenkins/Assignment_3_Jenkins.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Jenkins/Assignment_3_Jenkins.pdf -------------------------------------------------------------------------------- /Jenkins/ReadMe.md: -------------------------------------------------------------------------------- 1 | Jenkins 2 | -------------------------------------------------------------------------------- /Kubernetes/CASE_STUDY_Introduction_to_Kuberentes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Kubernetes/CASE_STUDY_Introduction_to_Kuberentes.pdf -------------------------------------------------------------------------------- /Kubernetes/Kubernetes_3_Node_Cluster.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Kubernetes/Kubernetes_3_Node_Cluster.pdf -------------------------------------------------------------------------------- /Kubernetes/apache-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: apache-deployment 5 | labels: 6 | app: apache 7 | spec: 8 | replicas: 2 9 | selector: 10 | matchLabels: 11 | app: apache 12 | template: 13 | metadata: 14 | labels: 15 | app: apache 16 | spec: 17 | containers: 18 | - name: apache 19 | image: ubuntu/apache2 20 | ports: 21 | - containerPort: 80 22 | -------------------------------------------------------------------------------- /Kubernetes/custom-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: custom-deployment 5 | labels: 6 | app: custom 7 | spec: 8 | replicas: 2 9 | selector: 10 | matchLabels: 11 | app: custom 12 | template: 13 | metadata: 14 | labels: 15 | app: custom 16 | spec: 17 | containers: 18 | - name: custom 19 | image: arkitcoin/k8scasestudy 20 | ports: 21 | - containerPort: 80 22 | -------------------------------------------------------------------------------- /Kubernetes/ingress.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: networking.k8s.io/v1 2 | kind: Ingress 3 | metadata: 4 | name: ingress 5 | annotations: 6 | nginx.ingress.kubernetes.io/rewrite-targeti: / 7 | spec: 8 | ingressClassName: nginx 9 | rules: 10 | - http: 11 | paths: 12 | - path: /apache 13 | pathType: Prefix 14 | backend: 15 | service: 16 | name: apache-deployment 17 | port: 18 | number: 80 19 | - path: /custom 20 | pathType: Prefix 21 | backend: 22 | service: 23 | name: custom-deployment 24 | port: 25 | number: 80 26 | -------------------------------------------------------------------------------- /Kubernetes/kubeadm-cluster.md: -------------------------------------------------------------------------------- 1 | # 3-Node Production Like Kubernetes Cluster Setup 2 | 3 | - [Cluster Setup Video](https://youtu.be/e4NY6a-TgyE) 4 | 5 | ## Prerequisites 6 | 7 | - 3 virtual machines (VMs) up and running in the same subnet / VPC. We are using EC2 instances on the AWS Cloud for this how-to guide. One of the VMs need to have 2 vCPUs and 4 GB RAM, which means at least a `t2.medium` instance type. Rest of the VMs can be `t2.micro` instances. 8 | - **Ubuntu 22.04 LTS** OS on all machines. 9 | - Familiarity with the [Kubernetes Components](https://kubernetes.io/docs/concepts/overview/components/). 10 | - On-Primises should have three VMs or Three Physical servers with communication enabled between them (Network) 11 | 12 | ## Part A - Controller and Worker Nodes 13 | 14 | 1. Configure Network Prerequisites 15 | 16 | This step configures IPv4 forwarding and lets iptables see bridged traffic. The `overlay` module is needed by Docker to work with the overlay network driver. The `br_netfilter` module is needed by Kubernetes to enable network filtering and NAT (Network Address Translation). 17 | 18 | Refer to [this Kubernetes documentation](https://kubernetes.io/docs/setup/production-environment/container-runtimes/#forwarding-ipv4-and-letting-iptables-see-bridged-traffic/) for more information. 19 | 20 | 1. 21 | ``` 22 | cat < /dev/null 102 | ``` 103 | 1. Update the `apt` package index, again: 104 | ``` 105 | sudo apt-get update 106 | ``` 107 | 1. Install Docker (including `containerd`): 108 | ``` 109 | sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin 110 | ``` 111 | 1. Make daemon file for docker to aviod service errors later 112 | ``` 113 | sudo mkdir /etc/docker 114 | cat < /etc/containerd/config.toml 149 | ``` 150 | 1. Open `config.toml` in a text editor: 151 | ``` 152 | vi /etc/containerd/config.toml 153 | ``` 154 | 1. Change the value of `SystemdCgroup` from `false` to `true` (it should be visible around line number 125 in `config.toml`): 155 | ``` 156 | SystemdCgroup = true 157 | ``` 158 | 1. Restart `containerd` 159 | ``` 160 | systemctl restart containerd 161 | ``` 162 | 1. Exit the `sudo` mode: 163 | ``` 164 | exit 165 | ``` 166 | Ensure to run these steps to configure the container runtime on **all** nodes. 167 | 168 | 1. Install `kubeadm`, `kubelet`, and `kubectl` 169 | 170 | In this step, we install these packages on **all** nodes: 171 | * `kubeadm`: The command to bootstrap the cluster 172 | * `kubelet`: An agent that runs on all nodes in the cluster and does things like starting pods and containers 173 | * `kubectl`: The command line utility to talk to the cluster 174 | 175 | Refer to [this Kubernetes documentation](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#installing-kubeadm-kubelet-and-kubectl) for more information. 176 | 177 | Since we are using Ubuntu 22.04 LTS, we will use instructions for Debian-based distributions. 178 | 179 | 1. Update the `apt` package index, install the required packages, and get the official GPG keys for the Kubernetes package repositories: 180 | ``` 181 | sudo apt-get update 182 | sudo apt-get install -y apt-transport-https ca-certificates curl 183 | curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg 184 | ``` 185 | 1. Add the Kubernetes repository to the `apt` sources: 186 | ``` 187 | echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list 188 | ``` 189 | 1. Update the `apt` package index, again: 190 | ``` 191 | sudo apt-get update 192 | ``` 193 | 1. Install `kubelet`, `kubeadm`, and `kubectl`: 194 | ``` 195 | sudo apt-get install -y kubelet kubeadm kubectl 196 | ``` 197 | 1. Pin their version (avoids automatic updates): 198 | ``` 199 | sudo apt-mark hold kubelet kubeadm kubectl 200 | ``` 201 | Ensure to install `kubeadm`, `kubelet`, and `kubectl` on **all** nodes. 202 | 203 | 204 | ## Part B - Controller Node ONLY 205 | 206 | Run these commands only on the VM designated as the controller (master) node. 207 | 208 | 1. (RUN AS ROOT) Initiate API server: 209 | 210 | 1. 211 | ``` 212 | sudo -i 213 | ``` 214 | 1. Initiate the API server. Remember to change the `` with the actual private IP address of the controller VM: 215 | ``` 216 | kubeadm init --apiserver-advertise-address= --pod-network-cidr=10.244.0.0/16 217 | ``` 218 | 1. Exit the sudo mode 219 | ``` 220 | exit 221 | ``` 222 | 1. (RUN AS NORMAL USER) Add a user for kube config: 223 | 224 | 1. 225 | ``` 226 | mkdir -p $HOME/.kube 227 | sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config 228 | sudo chown $(id -u):$(id -g) $HOME/.kube/config 229 | ``` 230 | 1. (RUN AS NORMAL USER) Deploy Weave network: 231 | ``` 232 | kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml 233 | ``` 234 | 1. (RUN AS ROOT) Create cluster join command: 235 | 236 | 1. 237 | ``` 238 | sudo -i 239 | ``` 240 | 1. 241 | ``` 242 | kubeadm token create --print-join-command 243 | ``` 244 | ## Part C - Worker Nodes ONLY 245 | 246 | Copy the output of the cluster join command from the previous step and run on the VMs designated as the worker nodes. 247 | 248 | (EXAMPLE COMMAND - DO NOT USE): 249 | ``` 250 | sudo kubeadm join 192.168.175.100:6443 --token jno9md.v9u1snltrwkv3vix --discovery-token-ca-cert-hash sha256:74b58d0e840e43a7051dcc4d7388b836dbd187c732fbfd3008051d10a14e271a 251 | ``` 252 | The Kubernetes cluster is now configured. 253 | 254 | 1. Now check the status of the nodes and cluster (RUN as NORMAL User) 255 | ``` 256 | kubectl get nodes 257 | ``` 258 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DevOps 2 | DevOps, Docker, Kubernetes, IaC and More 3 | -------------------------------------------------------------------------------- /Terraform/AWS_Infra/README.md: -------------------------------------------------------------------------------- 1 | ## Creating AWS VPC, and Creating EC2 instances in it ## 2 | - Create a Public VPC and create the required components for the same. 3 | - The required components being the Subnet, the Internet Gateway and the Route Table. 4 | - Create 3 Virtual Machines in total in the same Subnet for the Public VPC that you are creating. 5 | 6 | - *Note: Update the Region, CIDR Range, AMI-ID and KEYPAIR values before executing this Terraform Code* 7 | -------------------------------------------------------------------------------- /Terraform/AWS_Infra/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-east-2" 3 | secret_key = var.secret 4 | access_key = var.access 5 | } 6 | 7 | resource "aws_vpc" "public_vpc" { 8 | cidr_block = "10.0.0.0/16" 9 | } 10 | 11 | resource "aws_internet_gateway" "public_igw" { 12 | vpc_id = aws_vpc.public_vpc.id 13 | } 14 | 15 | resource "aws_subnet" "public_subnet" { 16 | vpc_id = aws_vpc.public_vpc.id 17 | cidr_block = "10.0.1.0/24" 18 | availability_zone = "us-east-2a" 19 | } 20 | 21 | resource "aws_route_table" "public_route_table" { 22 | vpc_id = aws_vpc.public_vpc.id 23 | 24 | route { 25 | cidr_block = "0.0.0.0/0" 26 | gateway_id = aws_internet_gateway.public_igw.id 27 | } 28 | } 29 | 30 | resource "aws_route_table_association" "public_route_association" { 31 | subnet_id = aws_subnet.public_subnet.id 32 | route_table_id = aws_route_table.public_route_table.id 33 | } 34 | 35 | resource "aws_instance" "public_vm" { 36 | count = 3 37 | ami = "ami-05fb0b8c1424f266b" 38 | instance_type = "t2.micro" 39 | subnet_id = aws_subnet.public_subnet.id 40 | key_name = "CHANGE_KEY_PAIR" 41 | 42 | tags = { 43 | Name = "PublicVM-${count.index + 1}" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Terraform/AWS_Infra/var.tf: -------------------------------------------------------------------------------- 1 | variable "access" { 2 | type = string 3 | default = "AKIDKK" 4 | } 5 | 6 | variable "secret" { 7 | type = string 8 | default ="mkdhaderigewbvdf" 9 | } 10 | -------------------------------------------------------------------------------- /Terraform/Terraform_Assignment-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Terraform/Terraform_Assignment-1.pdf -------------------------------------------------------------------------------- /Terraform/Terraform_Assignment4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Terraform/Terraform_Assignment4.pdf -------------------------------------------------------------------------------- /Terraform/Terraform_Assignment5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Terraform/Terraform_Assignment5.pdf -------------------------------------------------------------------------------- /Terraform/Terraform_Assignment_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Terraform/Terraform_Assignment_2.pdf -------------------------------------------------------------------------------- /Terraform/Terraform_Assignment_3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Terraform/Terraform_Assignment_3.pdf -------------------------------------------------------------------------------- /Terraform/Terraform_CASE_STUDY.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techarkit/DevOps/88fdf4a521d0b009a69e0dc140b685d81a71869c/Terraform/Terraform_CASE_STUDY.pdf -------------------------------------------------------------------------------- /Terraform/assignment1/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-east-2" 3 | secret_key = var.secret 4 | access_key = var.access 5 | } 6 | resource "aws_instance" "Ins1" { 7 | ami = "ami-05fb0b8c1424f266b" 8 | instance_type = "t2.micro" 9 | key_name = "keypair_ravi" 10 | tags = { 11 | Name = "Assignment-1" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Terraform/assignment1/var.tf: -------------------------------------------------------------------------------- 1 | variable "access" { 2 | type = string 3 | default = "AKIDKK" 4 | } 5 | 6 | variable "secret" { 7 | type = string 8 | default ="mkdhaderigewbvdf" 9 | } 10 | -------------------------------------------------------------------------------- /Terraform/assignment2/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-east-2" 3 | secret_key = var.secret 4 | access_key = var.access 5 | } 6 | resource "aws_instance" "Ins2" { 7 | ami = "ami-05fb0b8c1424f266b" 8 | instance_type = "t2.micro" 9 | key_name = "keypair_ravi" 10 | tags = { 11 | Name = "Assignment-2" 12 | } 13 | } 14 | resource "aws_eip" "Elastic-IP" { 15 | vpc = true 16 | } 17 | resource "aws_eip_association" "ElasticIP-Assocn" { 18 | instance_id = aws_instance.Ins2.id 19 | allocation_id = aws_eip.Elastic-IP.id 20 | } 21 | -------------------------------------------------------------------------------- /Terraform/assignment3/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | alias = "Ohio" 3 | region = "us-east-2" 4 | secret_key = var.secret 5 | access_key = var.access 6 | } 7 | provider "aws" { 8 | alias = "NV" 9 | region = "us-east-1" 10 | secret_key = var.secret 11 | access_key = var.access 12 | } 13 | resource "aws_instance" "Ins3-1" { 14 | provider = aws.Ohio 15 | ami = "ami-05fb0b8c1424f266b" 16 | instance_type = "t2.micro" 17 | key_name = "keypair_ravi" 18 | tags = { 19 | Name = "Hello-Ohio" 20 | } 21 | } 22 | resource "aws_instance" "Ins3-2" { 23 | provider = aws.NV 24 | ami = "ami-0c7217cdde317cfec" 25 | instance_type = "t2.micro" 26 | key_name = "MyKeyPair-Ravi" 27 | tags = { 28 | Name = "Hello-Virginia" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Terraform/assignment4/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | alias = "Ohio" 3 | region = "us-east-2" 4 | secret_key = var.secret 5 | access_key = var.access 6 | } 7 | provider "aws" { 8 | alias = "NV" 9 | region = "us-east-1" 10 | secret_key = var.secret 11 | access_key = var.access 12 | } 13 | resource "aws_instance" "Ins3-1" { 14 | provider = aws.Ohio 15 | ami = "ami-05fb0b8c1424f266b" 16 | instance_type = "t2.micro" 17 | key_name = "keypair_ravi" 18 | tags = { 19 | Name = "Hello-Ohio" 20 | } 21 | } 22 | resource "aws_instance" "Ins3-2" { 23 | provider = aws.NV 24 | ami = "ami-0c7217cdde317cfec" 25 | instance_type = "t2.micro" 26 | key_name = "MyKeyPair-Ravi" 27 | tags = { 28 | Name = "Hello-Virginia" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Terraform/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-east-1" 3 | secret_key = var.secret 4 | access_key = var.access 5 | } 6 | 7 | # Create VPC 8 | resource "aws_vpc" "main" { 9 | cidr_block = "10.0.0.0/16" 10 | enable_dns_support = true 11 | enable_dns_hostnames = true 12 | tags = { 13 | Name = "my-vpc" 14 | } 15 | } 16 | 17 | # Create Internet Gateway 18 | resource "aws_internet_gateway" "main" { 19 | vpc_id = aws_vpc.main.id 20 | tags = { 21 | Name = "my-igw" 22 | } 23 | } 24 | 25 | # Create Subnets 26 | resource "aws_subnet" "subnet1" { 27 | vpc_id = aws_vpc.main.id 28 | cidr_block = "10.0.1.0/24" 29 | availability_zone = "us-east-1a" 30 | map_public_ip_on_launch = true 31 | tags = { 32 | Name = "subnet-1" 33 | } 34 | } 35 | 36 | resource "aws_subnet" "subnet2" { 37 | vpc_id = aws_vpc.main.id 38 | cidr_block = "10.0.2.0/24" 39 | availability_zone = "us-east-1b" 40 | map_public_ip_on_launch = true 41 | tags = { 42 | Name = "subnet-2" 43 | } 44 | } 45 | 46 | # Create Security Group 47 | resource "aws_security_group" "instance_sg" { 48 | vpc_id = aws_vpc.main.id 49 | egress { 50 | from_port = 0 51 | to_port = 0 52 | protocol = "-1" 53 | cidr_blocks = ["0.0.0.0/0"] 54 | } 55 | ingress { 56 | from_port = 22 57 | to_port = 22 58 | protocol = "tcp" 59 | cidr_blocks = ["0.0.0.0/0"] 60 | } 61 | ingress { 62 | from_port = 80 63 | to_port = 80 64 | protocol = "tcp" 65 | cidr_blocks = ["0.0.0.0/0"] 66 | } 67 | tags = { 68 | Name = "instance-sg" 69 | } 70 | } 71 | 72 | # Create Network Interfaces 73 | resource "aws_network_interface" "nic1" { 74 | subnet_id = aws_subnet.subnet1.id 75 | security_group_ids = [aws_security_group.instance_sg.id] 76 | } 77 | 78 | resource "aws_network_interface" "nic2" { 79 | subnet_id = aws_subnet.subnet2.id 80 | security_group_ids = [aws_security_group.instance_sg.id] 81 | } 82 | 83 | # Create Instances 84 | resource "aws_instance" "instance1" { 85 | ami = "ami-0c7217cdde317cfec" 86 | instance_type = "t2.micro" 87 | key_name = "MyKeyPair-Ravi" 88 | 89 | network_interface_ids = [aws_network_interface.nic1.id] 90 | tags = { 91 | Name = "instance-1" 92 | } 93 | } 94 | 95 | resource "aws_instance" "instance2" { 96 | ami = "ami-0c7217cdde317cfec" 97 | instance_type = "t2.micro" 98 | key_name = "MyKeyPair-Ravi" 99 | 100 | network_interface_ids = [aws_network_interface.nic2.id] 101 | tags = { 102 | Name = "instance-2" 103 | } 104 | } 105 | --------------------------------------------------------------------------------