Skip to content

Commit

Permalink
Add domain name
Browse files Browse the repository at this point in the history
Add ssl cert.
Add DNS.
Remove ingress port 80.
Add ingress port 443
  • Loading branch information
kmesiab committed Mar 6, 2024
1 parent ee2fb64 commit 32bd188
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ RUN python -m nltk.downloader vader_lexicon punkt
# Make port 80 available to the world outside this container
EXPOSE 80

# Run gunicorn and bind it to port 80
CMD ["gunicorn", "--bind", "0.0.0.0:80", "main:app"]
# Run gunicorn and bind it to port 443
CMD ["gunicorn", "--bind", "0.0.0.0:443", "main:app"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ docker-build:

docker-run:
@echo "Running the Docker container..."
@docker run -p 80:80 mood-marker-api:latest
@docker run -p 80:443 mood-marker-api:latest

ecr-deploy: ecr-auth ecr-build-push

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@ cd terraform && tf init && tf apply
```

See the ./terraform/README.md for more information on the
AWS infrastructure deployment.
AWS infrastructure deployment.
13 changes: 9 additions & 4 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# Terraform

## ECR Registry
This API runs in an AWS ECS cluster with an internet gateway
and an application load balancer. It lives in one VPC
with two subnets in two availability zones.

`mood-marker-api`
## Getting Started

Navigate to the `./terraform` folder and run

```bash
462498369025.dkr.ecr.us-west-2.amazonaws.com/mood-marker-api
tf init
tf plan
```

## Resources
## Resources To Be Created

1. Load Balancer
2. Internet Gateway
Expand Down
2 changes: 1 addition & 1 deletion terraform/ecr.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ resource "aws_ecr_repository" "ecr_repository" {

output "repository_url" {
value = aws_ecr_repository.ecr_repository.repository_url
}
}
13 changes: 1 addition & 12 deletions terraform/gateway.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ resource "aws_internet_gateway" "mood_marker_api_igw" {

resource "aws_lb_target_group" "mood_maker_api_target_group" {
name = "mood-maker-api-tg"
port = 80
port = 443
protocol = "HTTP"
vpc_id = aws_vpc.mood_marker_api_vpc.id
target_type = "ip"
Expand All @@ -21,14 +21,3 @@ resource "aws_lb" "mood_maker_api_lb" {
security_groups = [aws_security_group.mood_marker_api_sg.id]
subnets = [aws_subnet.mood_marker_api_subnet_1.id, aws_subnet.mood_marker_api_subnet_2.id]
}

resource "aws_lb_listener" "mood_maker_api_listener" {
load_balancer_arn = aws_lb.mood_maker_api_lb.arn
port = 80
protocol = "HTTP"

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.mood_maker_api_target_group.arn
}
}
6 changes: 5 additions & 1 deletion terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ terraform {
}
}

provider aws {
region = var.region
}

resource "aws_ecs_cluster" "mood_marker_api_cluster" {
name = "mood-marker-api-cluster"
}
Expand Down Expand Up @@ -60,7 +64,7 @@ resource "aws_ecs_service" "mood_marker_api_service" {
load_balancer {
target_group_arn = aws_lb_target_group.mood_maker_api_target_group.arn
container_name = "mood-marker-api"
container_port = 80
container_port = 443
}

depends_on = [
Expand Down
6 changes: 5 additions & 1 deletion terraform/output.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
output "app_url" {
output "load_balancer_url" {
value = aws_lb.mood_maker_api_lb.dns_name
}

output "api_url" {
value = "https://langtool.net"
}
45 changes: 45 additions & 0 deletions terraform/route53.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
resource "aws_acm_certificate" "langtool_cert" {
domain_name = "langtool.net"
validation_method = "DNS"

lifecycle {
create_before_destroy = true
}
}

resource "aws_route53_record" "langtool_cert_validation" {
name = tolist(aws_acm_certificate.langtool_cert.domain_validation_options)[0].resource_record_name
type = tolist(aws_acm_certificate.langtool_cert.domain_validation_options)[0].resource_record_type
zone_id = var.route_53_hosted_zone_id
records = [tolist(aws_acm_certificate.langtool_cert.domain_validation_options)[0].resource_record_value]
ttl = 60
}

resource "aws_acm_certificate_validation" "langtool_cert_validation" {
certificate_arn = aws_acm_certificate.langtool_cert.arn
validation_record_fqdns = [aws_route53_record.langtool_cert_validation.fqdn]
}

resource "aws_lb_listener" "langtool_https_listener" {
load_balancer_arn = aws_lb.mood_maker_api_lb.arn
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = aws_acm_certificate.langtool_cert.arn

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.mood_maker_api_target_group.arn
}
}

resource "aws_route53_record" "langtool_lb" {
zone_id = var.route_53_hosted_zone_id
name = "langtool.net"
type = "A"
alias {
name = aws_lb.mood_maker_api_lb.dns_name
zone_id = aws_lb.mood_maker_api_lb.zone_id
evaluate_target_health = true
}
}
4 changes: 2 additions & 2 deletions terraform/task.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ resource "aws_ecs_task_definition" "mood_marker_api_task" {
essential = true
portMappings = [
{
containerPort = 80
hostPort = 80
containerPort = 443
hostPort = 443
protocol = "tcp"
},
]
Expand Down
5 changes: 5 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ variable "region" {
variable "deployment_bucket_name" {
type = string
default = "mood-marker-api-lambda-deployments"
}

variable "route_53_hosted_zone_id" {
type = string
default = "Z00098522Y9LE2926BSFR"
}
4 changes: 2 additions & 2 deletions terraform/vpc.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ resource "aws_security_group" "mood_marker_api_sg" {
vpc_id = aws_vpc.mood_marker_api_vpc.id

ingress {
from_port = 80
to_port = 80
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
Expand Down

0 comments on commit 32bd188

Please sign in to comment.