-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserdata.sh
97 lines (76 loc) · 2.4 KB
/
userdata.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
# Update and Grab Node + Git + awslogs
yum update -y
curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install -y nodejs git awslogs
# Configure CloudWatch Logs and AWS CLI
cat >/etc/awslogs/awscli.conf <<-EOF
[default]
region = ${aws_region}
[plugins]
cwlogs = cwlogs
EOF
# Set up the various CloudWatch Log Groups and Streams
cat >/etc/awslogs/awslogs.conf <<-EOF
[general]
state_file = /var/lib/awslogs/agent-state
# Global Messages
[/var/log/messages]
file = /var/log/messages
log_group_name = ${log_group_name}
log_stream_name = /var/log/messages
datetime_format = %b %d %H:%M:%S
# SSH logs
[/var/log/secure]
file = /var/log/secure
log_group_name = ${log_group_name}
log_stream_name = /var/log/secure
datetime_format = %b %d %H:%M:%S
# Cloud Init Logs (results of User Data Scripts)
[/var/log/cloud-init.log]
file = /var/log/cloud-init.log
log_group_name = ${log_group_name}
log_stream_name = /var/log/cloud-init.log
datetime_format = %b %d %H:%M:%S
[/var/log/cloud-init-output.log]
file = /var/log/cloud-init-output.log
log_group_name = ${log_group_name}
log_stream_name = /var/log/cloud-init-output.log
datetime_format = %b %d %H:%M:%S
# Nodejs Messages
[/var/log/nodejs.log]
file = /var/log/nodejs.log
log_group_name = ${log_group_name}
log_stream_name = /var/log/nodejs.log
datetime_format = %b %d %H:%M:%S
# Nodejs Error Messages
[/var/log/nodejserr.log]
file = /var/log/nodejserr.log
log_group_name = ${log_group_name}
log_stream_name = /var/log/nodejserr.log
datetime_format = %b %d %H:%M:%S
EOF
# Create the Node Log File
cat >/var/log/nodejs.log <<-EOF
[Nodejs Logs]
EOF
# Create the Node Error Log File
cat >/var/log/nodejserr.log <<-EOF
[Nodejs Error Logs]
EOF
# Give non-root default user "ec2-user" permissions to write to the log files
chown ec2-user:ec2-user /var/log/nodejs.log /var/log/nodejserr.log
chmod 644 /var/log/nodejs.log /var/log/nodejserr.log
# Start AWS Logs and Ensure Running
systemctl start awslogsd
systemctl enable awslogsd.service
# Make a directory to clone the application code to
mkdir -p /home/ec2-user/app && cd /home/ec2-user/app
# Clone the Git Repo
git clone https://github.com/jcolemorrison/ec2-lb-api.git .
# Install Dependencies
npm install
# Redirect Port 3000 to Port 80 Traffic
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000
# Run Node as the ec2-user
su ec2-user -c "node . > /var/log/nodejs.log 2> /var/log/nodejserr.log"