diff --git a/.gitignore b/.gitignore index 549e00a2..0a3ab395 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ build/ ### VS Code ### .vscode/ +terraform/bankapp-automate-key diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..12d929aa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# ----Satge 1---- +FROM maven:3.8.3-openjdk-17 as builder + +WORKDIR /src + +COPY . /src + +RUN mvn clean install -DskipTests=true + +# ---- Stage 2 ----- + +FROM openjdk:17-alpine + +COPY --from=builder /src/target/*.jar /src/target/bankapp.jar + +EXPOSE 8080 + +CMD ["java","-jar","/src/target/bankapp.jar"] + diff --git a/terraform/bankapp-automate-key.pub b/terraform/bankapp-automate-key.pub new file mode 100644 index 00000000..4901d473 --- /dev/null +++ b/terraform/bankapp-automate-key.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKWjgO1hLW2Y5bRfZcaPOCtmRos00BA7/zdnLwrDhsVT nilesh@DESKTOP-AL0D2SO diff --git a/terraform/ec2.tf b/terraform/ec2.tf new file mode 100644 index 00000000..5fe8b7ad --- /dev/null +++ b/terraform/ec2.tf @@ -0,0 +1,94 @@ + +data "aws_ami" "os_image" { + owners = ["099720109477"] + most_recent = true + filter { + name = "state" + values = ["available"] + } + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/*amd64*"] + } +} + +resource "aws_key_pair" "deployer" { + key_name = "bankapp-automate-key" + public_key = file("bankapp-automate-key.pub") +} + +resource "aws_default_vpc" "default" { + +} + +resource "aws_security_group" "allow_user_to_connect" { + name = "allow TLS" + description = "Allow user to connect" + vpc_id = aws_default_vpc.default.id + ingress { + description = "port 22 allow ssh" + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + description = " allow all outgoing traffic " + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + description = "port 80 allow http" + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + description = "port 443 allow https" + from_port = 443 + to_port = 443 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + tags = { + Name = "bankapp-security" + } +} + +resource "aws_instance" "testinstance" { + ami = data.aws_ami.os_image.id + instance_type = var.my_enviroment == "prd" ? "t2.medium" : "t2.micro" + key_name = aws_key_pair.deployer.key_name + security_groups = [aws_security_group.allow_user_to_connect.name] + user_data = file("${path.module}/script.sh") + tags = { + Name = "Bankapp-Automation-Server" + } + root_block_device { + volume_size = 20 + volume_type = "gp3" + } + connection { + type = "ssh" + user = "ubuntu" + private_key = file("terra-key") + host = self.public_ip + } + + provisioner "remote-exec" { + inline = [ + "sudo apt update -y", + "sudo apt install -y apache2", + "sudo systemctl start apache2", + "sudo systemctl enable apache2", + "echo 'Hello from Terraform Provisioners!' | sudo tee /var/www/html/index.html" + ] + } +} diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 00000000..e69de29b diff --git a/terraform/outputs.tf b/terraform/outputs.tf new file mode 100644 index 00000000..86a92273 --- /dev/null +++ b/terraform/outputs.tf @@ -0,0 +1,7 @@ +output "arn" { + value = aws_instance.testinstance.arn +} + +output "public_ip" { + value = aws_instance.testinstance.public_ip +} \ No newline at end of file diff --git a/terraform/terraform.tf b/terraform/terraform.tf new file mode 100644 index 00000000..9dc6778e --- /dev/null +++ b/terraform/terraform.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "6.23.0" + } + } +} + +provider "aws" { + region = var.aws_region +} \ No newline at end of file diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 00000000..9eafaad6 --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,19 @@ +variable "aws_region" { + description = "AWS region where resources will be provisioned" + default = "eu-west-1" +} + +variable "ami_id" { + description = "AMI ID for the EC2 instance" + default = "ami-085f9c64a9b75eed5" +} + +variable "instance_type" { + description = "Instance type for the EC2 instance" + default = "t2.micro" +} + +variable "my_enviroment" { + description = "Instance type for the EC2 instance" + default = "dev" +} \ No newline at end of file