From ddc636a943d500b86d238b295b641627ebe6dfc2 Mon Sep 17 00:00:00 2001 From: nagarjuna Date: Thu, 16 Mar 2023 17:00:28 +0530 Subject: [PATCH] terraform added --- spring-petclinic.service | 20 ++++++++ terraforminfra/data.tf | 10 ++++ terraforminfra/dev.tfvars | 12 +++++ terraforminfra/instance.tf | 91 +++++++++++++++++++++++++++++++++ terraforminfra/network.tf | 58 +++++++++++++++++++++ terraforminfra/provider.tf | 13 +++++ terraforminfra/securitygroup.tf | 42 +++++++++++++++ terraforminfra/variable.tf | 43 ++++++++++++++++ 8 files changed, 289 insertions(+) create mode 100644 spring-petclinic.service create mode 100644 terraforminfra/data.tf create mode 100644 terraforminfra/dev.tfvars create mode 100644 terraforminfra/instance.tf create mode 100644 terraforminfra/network.tf create mode 100644 terraforminfra/provider.tf create mode 100644 terraforminfra/securitygroup.tf create mode 100644 terraforminfra/variable.tf diff --git a/spring-petclinic.service b/spring-petclinic.service new file mode 100644 index 000000000..8812a8e06 --- /dev/null +++ b/spring-petclinic.service @@ -0,0 +1,20 @@ +[Unit] +Description=springpetclinic java application +[Service] +User=ansible +# The configuration file application.properties should be here: + +#change this to your workspace +WorkingDirectory=/home/ansible/ + +#path to executable. +#executable is a bash script which calls jar file +ExecStart=/usr/bin/java -jar spring-petclinic-2.7.3.jar + +SuccessExitStatus=143 +TimeoutStopSec=10 +Restart=on-failure +RestartSec=5 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/terraforminfra/data.tf b/terraforminfra/data.tf new file mode 100644 index 000000000..afe3182ac --- /dev/null +++ b/terraforminfra/data.tf @@ -0,0 +1,10 @@ +data "aws_key_pair" "mykey" { + filter { + name = "key-name" + values = ["newkey"] + } +} + + + + diff --git a/terraforminfra/dev.tfvars b/terraforminfra/dev.tfvars new file mode 100644 index 000000000..1f953b235 --- /dev/null +++ b/terraforminfra/dev.tfvars @@ -0,0 +1,12 @@ +myregion = "ap-south-1" +my_vpc = "192.168.0.0/16" +myvpctag = "myvpctag" +mypubsubnet = "192.168.0.0/24" +publicsubnettag = "publicsubnettag" +myintgwtag = "myintgwtag" +mycidr_block = "0.0.0.0/0" +instance_type = "t2.large" +ami_id = "ami-0f8ca728008ff5af4" + + + diff --git a/terraforminfra/instance.tf b/terraforminfra/instance.tf new file mode 100644 index 000000000..a0a0e35cb --- /dev/null +++ b/terraforminfra/instance.tf @@ -0,0 +1,91 @@ +resource "aws_instance" "jenkin" { + ami = var.ami_id + associate_public_ip_address = true + instance_type = var.instance_type + key_name = data.aws_key_pair.mykey.key_name + vpc_security_group_ids = [aws_security_group.myrsgroup.id] + subnet_id = aws_subnet.my_subnet.id + availability_zone = "ap-south-1a" + + tags = { + "Name" = "jenkins" + } +} + resource "null_resource" "jenkinnull" { + triggers = { + cluster_instance_ids = 1.2 + } + connection { + type = "ssh" + user = "ubuntu" + host = aws_instance.jenkin.public_ip + private_key = file("~/.ssh/id_rsa") + } + + +provisioner "remote-exec" { + +inline = [ +"sudo apt-get update", +"sudo apt-get install openjdk-11-jdk -y", +"sudo apt-get install git -y", +"sudo apt-get install wget -y", +"sudo apt install curl -y", +"sleep 2m", +"curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null", +"echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null", +"sudo apt install jenkins -y", +"sudo apt-get update", + ] + } +} + + + +resource "aws_instance" "node1" { + ami = var.ami_id + associate_public_ip_address = true + instance_type = var.instance_type + key_name = data.aws_key_pair.mykey.key_name + vpc_security_group_ids = [aws_security_group.myrsgroup.id] + subnet_id = aws_subnet.my_subnet.id + availability_zone = "ap-south-1a" + + tags = { + "Name" = "node1" + } + +} +resource "null_resource" "node1null" { + triggers = { + cluster_instance_ids = 1.2 + } + connection { + type = "ssh" + user = "ubuntu" + host = aws_instance.node1.public_ip + private_key = file("~/.ssh/id_rsa") + } + + +provisioner "remote-exec" { + +inline = [ +"sudo apt-get update", +"sudo apt-get install openjdk-11-jdk -y", +"sudo apt-get install git -y", +"sudo apt-get install wget -y", +"sudo apt install curl -y", +"sudo apt install maven -y", +"sudo apt install software-properties-common -y", +"sudo add-apt-repository --yes --update ppa:ansible/ansible", +"sudo apt install ansible -y", +"sudo apt-get update", + ] + } +} + + + + + diff --git a/terraforminfra/network.tf b/terraforminfra/network.tf new file mode 100644 index 000000000..001d64339 --- /dev/null +++ b/terraforminfra/network.tf @@ -0,0 +1,58 @@ +resource "aws_vpc" "myvpc" { + cidr_block = var.my_vpc + instance_tenancy = "default" + + tags = { + Name = var.myvpctag + } +} + +resource "aws_subnet" "my_subnet" { + vpc_id = aws_vpc.myvpc.id + cidr_block = var.mypubsubnet + availability_zone = "ap-south-1a" + tags = { + Name = var.publicsubnettag + } +} + +resource "aws_internet_gateway" "myintgw" { + vpc_id = aws_vpc.myvpc.id + + tags = { + Name = var.myintgwtag + } +} + +resource "aws_route_table" "mypubroute" { + vpc_id = aws_vpc.myvpc.id + + route { + cidr_block = var.mycidr_block + gateway_id = aws_internet_gateway.myintgw.id + } + + tags = { + Name = "pubroutetag" + } +} + +resource "aws_route_table_association" "pubassociation" { + subnet_id = aws_subnet.my_subnet.id + route_table_id = aws_route_table.mypubroute.id +} + +resource "aws_network_interface" "mynetworkinterface" { + subnet_id = aws_subnet.my_subnet.id + + tags = { + Name = "public_network_interface" + } +} + + + + + + + diff --git a/terraforminfra/provider.tf b/terraforminfra/provider.tf new file mode 100644 index 000000000..cbef3956e --- /dev/null +++ b/terraforminfra/provider.tf @@ -0,0 +1,13 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 4.0" + } + } +} + +# Configure the AWS Provider +provider "aws" { + region = var.myregion +} diff --git a/terraforminfra/securitygroup.tf b/terraforminfra/securitygroup.tf new file mode 100644 index 000000000..9ebe8ebde --- /dev/null +++ b/terraforminfra/securitygroup.tf @@ -0,0 +1,42 @@ +resource "aws_security_group" "myrsgroup" { + name = "myresourcegroup" + vpc_id = aws_vpc.myvpc.id + + ingress { + description = "myVPC" + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + + } + ingress { + description = "myVPC" + from_port = 8080 + to_port = 8080 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + + } + ingress { + description = "myVPC" + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + + } + + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + ipv6_cidr_blocks = ["::/0"] + } + + tags = { + Name = "myresourcegrouptag" + } +} \ No newline at end of file diff --git a/terraforminfra/variable.tf b/terraforminfra/variable.tf new file mode 100644 index 000000000..85688a5ac --- /dev/null +++ b/terraforminfra/variable.tf @@ -0,0 +1,43 @@ +variable "myregion" { + type = string + default = "ap-south-1" +} + +variable "my_vpc" { + type = string +} + +variable "myvpctag" { + type = string +} + +variable "mypubsubnet" { + type = string +} + +variable "publicsubnettag" { + type = string +} + +variable "myintgwtag" { + type = string +} + +variable "mycidr_block" { + type = string +} + +variable "resource_version" { + type = string + default = "1.0" +} + + +variable "instance_type" { + type = string +} + +variable "ami_id" { + type = string +} +