Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ build/

### VS Code ###
.vscode/
terraform/bankapp-automate-key
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

1 change: 1 addition & 0 deletions terraform/bankapp-automate-key.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKWjgO1hLW2Y5bRfZcaPOCtmRos00BA7/zdnLwrDhsVT nilesh@DESKTOP-AL0D2SO
94 changes: 94 additions & 0 deletions terraform/ec2.tf
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
Empty file added terraform/main.tf
Empty file.
7 changes: 7 additions & 0 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "arn" {
value = aws_instance.testinstance.arn
}

output "public_ip" {
value = aws_instance.testinstance.public_ip
}
12 changes: 12 additions & 0 deletions terraform/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.23.0"
}
}
}

provider "aws" {
region = var.aws_region
}
19 changes: 19 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -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"
}