Skip to content

Instantly share code, notes, and snippets.

@ngohuytrieu
Created January 19, 2024 02:35
Show Gist options
  • Save ngohuytrieu/3e3798d28135ed4fac6d96d9800802c7 to your computer and use it in GitHub Desktop.
Save ngohuytrieu/3e3798d28135ed4fac6d96d9800802c7 to your computer and use it in GitHub Desktop.
Create AWS EC2, SSH to EC2, add domain and setup SSL for domain

Create EC2 instance

Go to https://console.aws.amazon.com/ec2 Click on Launch Instance button to add new EC2 Launch instanece button

Step 1: Choose an Amazon Machine Image (AMI)

Choose the OS type you want then click Select OS type

Step 2: Choose an Instance Type

Choose the instances type you want, choose t2.micro for free then click Next: Configure Instance Details t2.micro

Other steps leave as default

Step 6: Configure Security Group, set Assign a security group

Create your own rules or leave as default Security group Or select an existing security group (if any) Security group Then click Review and Launch

Step 7: Review Instance Launch

Review instance, if ok then launch it by clicking Launch Launch

Step 8: Key pair

Key pair popup show up, you can create new key pair (use of ssh) or select an existing one then click Launch Instance Create pem file Choose pem file

Create new .pem file

Go to https://console.aws.amazon.com/ec2 In the navigation pane, under Network & Security, choose Key Pairs then click Create key pair

Key pairs Create key pair

Enter name for key pair, format select pem file Create key pair

SSH to EC2 instance

Check if macos enable ssh

sudo systemsetup -getremotelogin

The response look like this

Remote Login: On

Set 400 permission to pem file

chmod -R 400 /Users/trieungo/trieungo/cloud.devs.pem

Connect to aws

ssh -i /path/mypemfile.pem ubuntu@xxx.xx.xxx.xxx
# .pem file is key pair created in step 2
# ubuntu is ec2 instance username, default is ubuntu (if you launch ubuntu instance)
# xxx.xx.xxx.xxx is ip of ec2 instance

Create domain using Router 53

Click Services -> Route 53 Route 53

Click Hosted zones Hosted zone

Click Create Hosted zone or select existing one. I’ll go for an existing one this time Create record

Then click Create record

Fill subdomain name, Public IPv4 DNS, of EC2 instance then click Create Create record

Setup SSL for domain

Create target group

Go to Target Groups and click Create target group Target group Create target group

Fill target group name, select VPC, the rest leave as default then click Next Create target group Note: VPC must be the same VPC of your ealier EC2 instance

Select your EC2 instance then click Create target group Select target

Create load balancer and link to target group

Go to Load balancer and click Create load balancer Create load balancer

Select balancer type is application type Balancer type

Fill load balancer, select VPC , security group and link to target group (Protocol choose HTTPS) and select SSL certificate then click create load balancer VPC Security group Target group

Then go to load balancer detail / listener to add 80 for redirect from http - https

Fill port is 80, action select redirect, others leave as default Http to https

Wait until the load balancer status changes to active then you are good to go.

Link router 53 to load balancer

Go to your balancer and get DNS name DNS name

Go to your router 53, record type select CNAME, value replace with DNS name CNAME

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment