Skip to content

Commit

Permalink
feat: feat: trying to implement aws
Browse files Browse the repository at this point in the history
  • Loading branch information
plh97 committed Jun 25, 2023
1 parent 7620a67 commit 841a46c
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 38 deletions.
16 changes: 8 additions & 8 deletions ansible/cicd.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- hosts: vultr
- hosts: aws
user: root
gather_facts: False
tasks:
Expand All @@ -15,36 +15,36 @@
- name: Delete content & directory
file:
state: absent
path: /root/chatroom/
path: ~/chatroom/
- name: pull latest code on develop branch
git:
repo: https://github.com/plh2/chatroom.git
dest: /root/chatroom
dest: ~/chatroom
force: true
version: develop

- name: copy cert key file
copy:
src: /etc/letsencrypt/live/chat.plhh.xyz/privkey.pem
dest: /root/chatroom/nginx/cert/privkey.pem
dest: ~/chatroom/nginx/cert/privkey.pem
remote_src: yes
- name: copy cert key file
copy:
src: /etc/letsencrypt/live/chat.plhh.xyz/fullchain.pem
dest: /root/chatroom/nginx/cert/fullchain.pem
dest: ~/chatroom/nginx/cert/fullchain.pem
remote_src: yes

- name: install dependences
command:
cmd: yarn run bootstrap
chdir: /root/chatroom
chdir: ~/chatroom

- name: build
command:
chdir: /root/chatroom
chdir: ~/chatroom
cmd: npm run build

- name: deploy
command:
chdir: /root/chatroom
chdir: ~/chatroom
cmd: npm run deploy
6 changes: 1 addition & 5 deletions ansible/hosts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
vultr
# Host vultr
# HostName 45.77.172.10
# User root
# IdentityFile ~/.ssh/qwe.pem
aws
23 changes: 13 additions & 10 deletions ansible/init.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
---
- hosts: vultr
user: root
gather_facts: False
- hosts: aws
# user: ubuntu
# gather_facts: False
remote_user: ubuntu
become: yes
become_method: sudo
tasks:
- name: Get running containers
docker_host_info:
Expand All @@ -16,11 +19,11 @@
- name: Delete content & directory
file:
state: absent
path: /root/chatroom/
path: ~/chatroom/
- name: Clone Repository
git:
repo: https://github.com/plh2/chatroom.git
dest: /root/chatroom
dest: ~/chatroom
clone: yes
update: yes
version: develop
Expand All @@ -38,23 +41,23 @@
- name: copy cert key file
copy:
src: /etc/letsencrypt/live/chat.plhh.xyz/privkey.pem
dest: /root/chatroom/nginx/cert/privkey.pem
dest: ~/chatroom/nginx/cert/privkey.pem
remote_src: yes
- name: copy cert key file
copy:
src: /etc/letsencrypt/live/chat.plhh.xyz/fullchain.pem
dest: /root/chatroom/nginx/cert/fullchain.pem
dest: ~/chatroom/nginx/cert/fullchain.pem
remote_src: yes
- name: bootstrap
command:
cmd: yarn run bootstrap
chdir: /root/chatroom
chdir: ~/chatroom
- name: build
command:
chdir: /root/chatroom
chdir: ~/chatroom
cmd: npm run build
- name: deploy
command:
chdir: /root/chatroom
chdir: ~/chatroom
cmd: npm run deploy
...
3 changes: 2 additions & 1 deletion packages/frontend/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.eslintrc.cjs
.eslintrc.cjs
.history
27 changes: 27 additions & 0 deletions terraform-aws/dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
resource "aws_route53_zone" "main" {
name = var.domain_name
}

resource "aws_eip" "lb" {
instance = aws_instance.app_server.id
domain = "vpc"
}

resource "aws_route53_record" "chat" {
zone_id = aws_route53_zone.main.zone_id
name = var.domain_name
# records = [aws_instance.app_server.public_dns]
records = [aws_eip.lb.public_ip]
ttl = 600
type = "A"
}

# resource "aws_route53_record" "ns" {
# allow_overwrite = true
# name = var.domain_name
# ttl = 172800
# type = "NS"
# zone_id = aws_route53_zone.main.zone_id

# records = aws_route53_zone.main.name_servers
# }
10 changes: 10 additions & 0 deletions terraform-aws/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "aws_instance" "app_server" {
# ami = "ami-09dac21d1664bc313" # official one
ami = "ami-0ce99c18a68a1b23b" # customized one
instance_type = "t2.micro"
key_name = var.key_name

tags = {
Name = "Chat Room"
}
}
4 changes: 4 additions & 0 deletions terraform-aws/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "instance_ip_addr" {
# sensitive = true
value = aws_instance.app_server.public_ip
}
13 changes: 13 additions & 0 deletions terraform-aws/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5"
}
}
required_version = ">= 1.2.0"
}

provider "aws" {
region = "ap-southeast-1"
}
25 changes: 25 additions & 0 deletions terraform-aws/ssh.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
variable "key_name" {
default = "aws-key"
}

resource "tls_private_key" "example" {
algorithm = "RSA"
rsa_bits = 4096
}

resource "aws_key_pair" "my_ssh_key" {
key_name = var.key_name
public_key = tls_private_key.example.public_key_openssh
}

resource "local_sensitive_file" "pem_file" {
filename = pathexpand("~/.ssh/${var.key_name}.pem")
file_permission = "600"
directory_permission = "700"
content = tls_private_key.example.private_key_pem
}

output "private_key" {
value = tls_private_key.example.private_key_pem
sensitive = true
}
36 changes: 36 additions & 0 deletions terraform-aws/ubuntu22.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
variable "aws_api_key" {
type = string
default = "${env("TF_VAR_AWS_API_KEY")}"
sensitive = true
}

source "amazon-ebs" "basic-example" {
access_key = "${env("AWS_SECRET_ACCESS_KEY")}"
secret_key = "${env("AWS_ACCESS_KEY_ID")}"
region = "ap-southeast-1"
}


packer {
required_plugins {
amazon = {
version = ">= 1.2.6"
source = "github.com/hashicorp/amazon"
}
}
}

source "amazon-ebs" "ubuntu" {
ami_name = "packer-linux-aws"
instance_type = "t2.micro"
region = "ap-southeast-1"
source_ami = "ami-09dac21d1664bc313"
ssh_username = "ubuntu"
}

build {
sources = ["source.amazon-ebs.ubuntu"]
provisioner "shell" {
script = "ubuntu22.sh"
}
}
66 changes: 66 additions & 0 deletions terraform-aws/ubuntu22.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash

# update
sudo apt-get update && sudo apt-get -y upgrade

sh -c "$(curl -fsSL https://get.docker.com)"
dockerd

# certbot
# sudo apt-get install -y certbot
# sudo certbot certonly --standalone --agree-tos --redirect -m pengliheng111@gmail.com -d chat.plhh.xyz --non-interactive


sudo apt-get install -y gcc g++ make

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
sudo apt-get install -y npm
sudo npm install yarn lerna nodemon -g

# made port can be visited in public network
# sudo apt-get install ufw
# ufw --force enable
# ufw allow 80/tcp
# ufw allow 443/tcp
# ufw allow 443/udp
# ufw allow 3000:9999/tcp
# ufw disable



# # install zsh
# sudo apt-get install -y zsh
# # sudo rm -rf /root/.oh-my-zsh
# sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
# sudo chsh -s $(which zsh)

# # add 2 zsh plugin
# git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

# # setup zsh config

# echo 'alias public_ip="dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com"

# export ZSH="$HOME/.oh-my-zsh"

# ZSH_THEME="bira"

# plugins=(
# ansible
# docker
# emoji
# git
# git-flow
# terraform
# github
# yarn
# aws
# npm
# nvm
# zsh-autosuggestions
# zsh-syntax-highlighting
# )

# source $ZSH/oh-my-zsh.sh' > ~/.zshrc
14 changes: 14 additions & 0 deletions terraform-aws/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# route 53 variables
variable "domain_name" {
# default = "plhh.xyz"
# default = "plhh.link"
default = "plh.ninja"
description = "domain name"
type = string
}

variable "record_name" {
default = "www"
description = "sub domain name"
type = string
}
2 changes: 1 addition & 1 deletion terraform/dns.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ resource "vultr_dns_record" "chat_record" {
domain = vultr_dns_domain.my_domain.id
name = "chat"
data = vultr_instance.instance.main_ip
ttl = 600
ttl = 600
type = "A"
}
2 changes: 1 addition & 1 deletion terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "vultr_instance" "instance" {
plan = "vc2-1c-1gb"
region = "sgp"
snapshot_id = "9ecbaba3-dd63-4350-a0e1-04e6dce56476"
snapshot_id = "6d664ee7-8182-4127-85a6-3e802538f281"
firewall_group_id = "dd776525-5e19-42e1-b55a-ad6da1cf6a4b"
hostname = "vultr.guest"
label = "chat room instance"
Expand Down
4 changes: 2 additions & 2 deletions terraform/ubuntu22.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ packer {
required_plugins {
vultr = {
version = ">=v2.3.2"
source = "github.com/vultr/vultr"
source = "github.com/vultr/vultr"
}
}
}
Expand All @@ -20,7 +20,7 @@ source "vultr" "ubuntu22" {
region_id = "sgp"
snapshot_description = "ubuntu 22 ${formatdate("YYYY-MM-DD hh:mm", timestamp())}"
ssh_username = "root"
state_timeout = "25m"
state_timeout = "10m"
}

build {
Expand Down
Loading

0 comments on commit 841a46c

Please sign in to comment.