Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform for Frontend #43

Merged
merged 8 commits into from
Jun 2, 2024
Merged
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
2 changes: 2 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ RUN poetry install --no-root

COPY brewbucks brewbucks

EXPOSE 80

CMD ["poetry", "run", "flask", "--app", "brewbucks", "run", "--host", "0.0.0.0", "--port", "80"]
63 changes: 63 additions & 0 deletions ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,49 @@ resource "aws_ecs_cluster" "brewbucks" {
name = "brewbucks"
}

resource "aws_ecs_task_definition" "brewbucks-frontend" {
family = "brewbucks-frontend"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = 1024
memory = 2048
execution_role_arn = data.aws_iam_role.lab.arn
depends_on = [docker_image.brewbucks-frontend]

container_definitions = <<DEFINITION
[
{
"image": "${docker_registry_image.brewbucks-frontend.name}",
"cpu": 1024,
"memory": 2048,
"name": "brewbucks-frontend",
"networkMode": "awsvpc",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80
}
],
"environment": [
{
"name": "BASE_URL",
"value": "http://${aws_lb.brewbucks.dns_name}"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/brewbucks/frontend",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs",
"awslogs-create-group": "true"
}
}
}
]
DEFINITION
}

resource "aws_ecs_task_definition" "brewbucks" {
family = "brewbucks"
network_mode = "awsvpc"
Expand Down Expand Up @@ -45,6 +88,26 @@ resource "aws_ecs_task_definition" "brewbucks" {
DEFINITION
}

resource "aws_ecs_service" "brewbucks-frontend" {
name = "brewbucks-frontend"
cluster = aws_ecs_cluster.brewbucks.id
task_definition = aws_ecs_task_definition.brewbucks-frontend.arn
desired_count = 1
launch_type = "FARGATE"
depends_on = [aws_ecs_task_definition.brewbucks-frontend]

network_configuration {
subnets = data.aws_subnets.private.ids
security_groups = [aws_security_group.brewbucks.id]
assign_public_ip = true
}
load_balancer {
target_group_arn = aws_lb_target_group.brewbucks-frontend.arn
container_name = "brewbucks-frontend"
container_port = 80
}

}

resource "aws_ecs_service" "brewbucks" {
name = "brewbucks"
Expand Down
10 changes: 10 additions & 0 deletions front-end/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:20-alpine as build
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
RUN npm install -g serve
EXPOSE 80
CMD [ "serve" , "-s" , "dist" , "-p" , "80"]
# CMD [ "npm" , "run" , "preview" ]
8 changes: 8 additions & 0 deletions front-end/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import react from '@vitejs/plugin-react-swc'

// https://vitejs.dev/config/
export default defineConfig({
base:"/",
server: {
port: 80,
strictPort: true,
host: true,
watch: {
usePolling: true
}
},
preview: {
port: 80,
strictPort: true,
},
plugins: [react({
include: "**/*.jsx",
})]
Expand Down
15 changes: 15 additions & 0 deletions images.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ resource "docker_registry_image" "brewbucks" {
resource "aws_ecr_repository" "brewbucks" {
name = "brewbucks"
}

resource "docker_registry_image" "brewbucks-frontend" {
name = docker_image.brewbucks-frontend.name
}

resource "docker_image" "brewbucks-frontend" {
name = "${aws_ecr_repository.brewbucks-frontend.repository_url}:latest"
build {
context = "./front-end/."
}
}

resource "aws_ecr_repository" "brewbucks-frontend" {
name = "brewbucks-frontend"
}
72 changes: 55 additions & 17 deletions loadbalancer.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
resource "aws_lb_target_group" "brewbucks" {
name = "brewbucks"
port = 80
protocol = "HTTP"
vpc_id = aws_security_group.brewbucks.vpc_id
target_type = "ip"

health_check {
path = "/api/v1/health"
port = "80"
protocol = "HTTP"
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
interval = 10
}
resource "aws_lb_target_group" "brewbucks" {
name = "brewbucks"
port = 80
protocol = "HTTP"
vpc_id = aws_security_group.brewbucks.vpc_id
target_type = "ip"

health_check {
path = "/api/v1/health"
port = "80"
protocol = "HTTP"
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
interval = 10
}
}

resource "aws_lb_target_group" "brewbucks-frontend" {
name = "brewbucks-frontend"
port = 80
protocol = "HTTP"
vpc_id = aws_security_group.brewbucks.vpc_id
target_type = "ip"

health_check {
path = "/"
port = "80"
protocol = "HTTP"
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
interval = 10
}
}

resource "aws_lb" "brewbucks-frontend" {
name = "brewbucks-frontend"
internal = false
load_balancer_type = "application"
subnets = data.aws_subnets.private.ids
security_groups = [aws_security_group.brewbucks.id]
}

resource "aws_lb_listener" "brewbucks-frontend" {
load_balancer_arn = aws_lb.brewbucks-frontend.arn
port = "80"
protocol = "HTTP"

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.brewbucks-frontend.arn
}

}

resource "aws_lb" "brewbucks" {
Expand All @@ -33,4 +71,4 @@ resource "aws_lb_listener" "brewbucks" {
type = "forward"
target_group_arn = aws_lb_target_group.brewbucks.arn
}
}
}
Loading