|
| 1 | + |
| 2 | +provider "aws" { |
| 3 | + profile = "default" |
| 4 | + region = var.aws_region |
| 5 | +} |
| 6 | + |
| 7 | +resource "aws_vpc" "some_custom_vpc" { |
| 8 | + cidr_block = "10.0.0.0/16" |
| 9 | + |
| 10 | + tags = { |
| 11 | + Name = "Some Custom VPC" |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +resource "aws_subnet" "some_public_subnet" { |
| 16 | + vpc_id = aws_vpc.some_custom_vpc.id |
| 17 | + cidr_block = "10.0.1.0/24" |
| 18 | + availability_zone = var.aws_availability_zone |
| 19 | + |
| 20 | + tags = { |
| 21 | + Name = "Some Public Subnet" |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +resource "aws_subnet" "some_private_subnet" { |
| 26 | + vpc_id = aws_vpc.some_custom_vpc.id |
| 27 | + cidr_block = "10.0.2.0/24" |
| 28 | + availability_zone = "eu-west-2a" |
| 29 | + |
| 30 | + tags = { |
| 31 | + Name = "Some Private Subnet" |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +resource "aws_internet_gateway" "some_ig" { |
| 36 | + vpc_id = aws_vpc.some_custom_vpc.id |
| 37 | + |
| 38 | + tags = { |
| 39 | + Name = "Some Internet Gateway" |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +resource "aws_route_table" "public_rt" { |
| 44 | + vpc_id = aws_vpc.some_custom_vpc.id |
| 45 | + |
| 46 | + route { |
| 47 | + cidr_block = "0.0.0.0/0" |
| 48 | + gateway_id = aws_internet_gateway.some_ig.id |
| 49 | + } |
| 50 | + |
| 51 | + route { |
| 52 | + ipv6_cidr_block = "::/0" |
| 53 | + gateway_id = aws_internet_gateway.some_ig.id |
| 54 | + } |
| 55 | + |
| 56 | + tags = { |
| 57 | + Name = "Public Route Table" |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +resource "aws_route_table_association" "public_1_rt_a" { |
| 62 | + subnet_id = aws_subnet.some_public_subnet.id |
| 63 | + route_table_id = aws_route_table.public_rt.id |
| 64 | +} |
| 65 | + |
| 66 | +resource "aws_security_group" "web_sg" { |
| 67 | + name = "HTTP and SSH" |
| 68 | + vpc_id = aws_vpc.some_custom_vpc.id |
| 69 | + |
| 70 | + ingress { |
| 71 | + from_port = 80 |
| 72 | + to_port = 80 |
| 73 | + protocol = "tcp" |
| 74 | + cidr_blocks = ["0.0.0.0/0"] |
| 75 | + } |
| 76 | + ingress { |
| 77 | + from_port = 22 |
| 78 | + to_port = 22 |
| 79 | + protocol = "tcp" |
| 80 | + cidr_blocks = ["0.0.0.0/0"] |
| 81 | + } |
| 82 | + |
| 83 | + egress { |
| 84 | + from_port = 0 |
| 85 | + to_port = 0 |
| 86 | + protocol = -1 |
| 87 | + cidr_blocks = ["0.0.0.0/0"] |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +resource "aws_instance" "web_instance" { |
| 92 | + ami = var.aws_ami |
| 93 | + instance_type = var.aws_instance_type |
| 94 | + key_name = "aws_for_discord" |
| 95 | + |
| 96 | + subnet_id = aws_subnet.some_public_subnet.id |
| 97 | + vpc_security_group_ids = [aws_security_group.web_sg.id] |
| 98 | + associate_public_ip_address = true |
| 99 | + |
| 100 | + user_data = <<-EOF |
| 101 | + #!/bin/bash -ex |
| 102 | +
|
| 103 | + # Install Node.js and npm |
| 104 | + sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg |
| 105 | + curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg |
| 106 | + NODE_MAJOR=20 |
| 107 | + echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list |
| 108 | + sudo apt-get update && sudo apt-get install nodejs -y |
| 109 | +
|
| 110 | + # Clone the repo |
| 111 | + sudo git clone https://github.com/fac30/discord-chatbot--Oleg-Loza.git /home/ubuntu/discord-chatbot--Oleg-Loza |
| 112 | + cd /home/ubuntu/discord-chatbot--Oleg-Loza |
| 113 | +
|
| 114 | + # Install PM2 |
| 115 | + sudo npm install -g pm2 |
| 116 | +
|
| 117 | + # Install other dependencies |
| 118 | + sudo npm install |
| 119 | +
|
| 120 | + # Start your application using PM2 |
| 121 | + #pm2 start ./discord-chatbot--Oleg-Loza |
| 122 | +
|
| 123 | + # Save PM2 startup configuration |
| 124 | + #pm2 startup systemd -u ubuntu --hp /home/ubuntu |
| 125 | +
|
| 126 | + # Save the current PM2 process list |
| 127 | + # pm2 save |
| 128 | +
|
| 129 | + # Ensure PM2 restarts on system reboot |
| 130 | + sudo systemctl enable pm2-ubuntu |
| 131 | +
|
| 132 | + EOF |
| 133 | + |
| 134 | + tags = { |
| 135 | + "Name" : "ec2_in_london_for_discord" |
| 136 | + } |
| 137 | +} |
| 138 | + |
0 commit comments