How to Set Up AWS Load Balancer for Vapor Server-Side Swift with HTTPS

AWS load balancer architecture diagram for Vapor Server-Side Swift app

Overview

loadBalancer overview.png
 

Create AWS Certificate

To support HTTPS, We need to create a certificate. I’ll use an AWS certificate.

https1.png
https2.png
Add your domain name. e.g., *.yourDomain.com

Add your domain name. e.g., *.yourDomain.com

That’s it. Let’s create Load Balancer

That’s it. Let’s create Load Balancer

 

Create AWS Load Balancer

The load balancer connects to the ECS service through a fixed DNS. If you have your domain address, you can register Load Balancer’s DNS as CNAME and use it.

1.png
First, select Load Balancing from the list of EC2 services.

First, select Load Balancing from the list of EC2 services.

3.png
4.png
This part is essential. Let’s add HTTPS.

This part is essential. Let’s add HTTPS.

Please set up the VPC of the ECS created in the previous post.

Please set up the VPC of the ECS created in the previous post.

I choose the certificate which is made on AWS.

I choose the certificate which is made on AWS.

Leave only 0.0.0/0 in Source and delete the '::/0'. '::/0' is used by ipv6.

Leave only 0.0.0/0 in Source and delete the ‘::/0’. ‘::/0’ is used by ipv6.

The vapor app uses 8080 as the default port. Let's set the routing port to 8080.

The vapor app uses 8080 as the default port. Let’s set the routing port to 8080.

A health check is a path that periodically checks whether the server is operating correctly. Health Check is straightforward to set up in the Vapor app.

A health check is a path that periodically checks whether the server is operating correctly. Health Check is straightforward to set up in the Vapor app.

//Set HealthCheck
app.get("health") { req in
return "OK"
}
Whether the server is running correctly every 120 seconds, that is, every 2 minutes.

Whether the server is running correctly every 120 seconds, that is, every 2 minutes.

12.png
The setting is done. Simple, isn't it?

The setting is done. Simple, isn’t it?

Don't forget to check if the Target Group of Load Balancer is also adequately created.

Don’t forget to check if the Target Group of Load Balancer is also adequately created.

 

Create ECS Service

Let’s create a new service that runs on ECS Container. I’ll set a load balancer for it.

1.png
2.png
3.png
4.png
5.png
6.png
7.png
8.png
9.png
11.png
12.png
13.png
The tasks are pending because our server can't access the RDS. I'll resolve it by setting the inbound rule for RDS.

The tasks are pending because our server can’t access the RDS. I’ll resolve it by setting the inbound rule for RDS.

 

Set Up RDS Inbound Rules

15.png
16.png
17.png
18.png
Now the tasks that run on ECS are working fine.

Now the tasks that run on ECS are working fine.

 

Set Up the Load Balancer Forward Rule

To access your server, You should set up the ‘forward rule’ on LoadBalancer. Do you remember our load balancer listens to two-port? HTTP and HTTPS.

Let’s set up the ‘forward rule.’

Check your Load Balancer. You can see there is two Listener ID.

Check your Load Balancer. You can see there is two Listener ID.

Set HTTPS, Forward to ECS Clusters load balancing target group.

Set HTTPS, Forward to ECS Clusters load balancing target group.

Set HTTP, Forward to ECS Clusters load balancing target group. (Yes, It’s the same as HTTPS)

Set HTTP, Forward to ECS Clusters load balancing target group. (Yes, It’s the same as HTTPS)

That’s all. Now you can access your Vapor Server by using the DNS name. Copy and Pasted it to your web browser and then check whether it can access or not.

That’s all. Now you can access your Vapor Server by using the DNS name. Copy and Pasted it to your web browser and then check whether it can access or not.

 

Set Up the DNS CNAME on Namecheap

Do you have your domain? The last step is to set up your domain with an AWS load balancer and certificates.

Go to AWS Certificate Manager. Check your CNAME and Value.

Go to AWS Certificate Manager. Check your CNAME and Value.

I'm using NameCheap.com. So If you bought a domain at NameCheap.com, then go to the advance DNS setup at NameCheap.com.You have to register two CNAME records. One is AWS Certificates the other one is Load Balancer's DNS.AWS CertificatesAWS Certifica…

I’m using NameCheap.com. So If you bought a domain at NameCheap.com, then go to the advance DNS setup at NameCheap.com.

You have to register two CNAME records. One is AWS Certificates the other one is Load Balancer’s DNS.

  • AWS Certificates

    • AWS Certificate Manager -> Domain

  • Load Balancer’s DNS

    • EC2 -> Load Balancers -> Description -> DNS Name

 

Conclusion

I introduced how to set up your vapor app using the Load Balancer. There are many steps you have to set up. I don’t think It’s complicated. Once you set it up, then you can easily set up your vapor app next time. I hope you enjoy reading my posts.

Discover more from Shawn

Subscribe now to keep reading and get access to the full archive.

Continue reading