Launching, configuring, connecting & using an EC2 Instance | AWS in Action

Launching, configuring, connecting & using an EC2 Instance | AWS in Action


Launching, configuring, connecting & using an EC2 Instance | AWS in Action

Learn how to launch, configure and use an EC2 instance. We’ll cover AMIs and instance types and connect to the instance via “EC2 Instance Connect”. A basic web server will serve as the demo project for this video.

Demo project code: https://github.com/academind/aws-demos
AWS Cloud Practitioner Course: https://acad.link/aws-cp
Tutorial - Set up NodeJS on EC2: https://docs.aws.amazon.com/sdk-for-j
More on Linux permissions: https://linuxfoundation.org/blog/clas

Join our Academind Community on Discord: https://academind.com/community

Check out all our other courses: https://academind.com/courses

00:00 Introduction
01:23 Demo Project Code
02:57 Launching an EC2 Instance
05:46 Connecting via EC2 Instance Connect
07:17 Getting the Project Code via Git (incl. Git Installation)
08:51 Installing \u0026 Using NodeJS
10:38 Creating Files \u0026 Managing Permissions via Linux Commands
13:53 Running the NodeJS Web Server
14:10 Port Forwarding (80 to 3000)
15:46 Viewing \u0026 Using the Website
17:44 Running the Webserver in the Background
19:10 Terminating the EC2 Instance

----------

• Go to https://www.academind.com and subscribe to our newsletter to stay updated and to get exclusive content \u0026 discounts
• Follow @maxedapps and @academind_real on Twitter
• Follow @academind_real on Instagram: https://www.instagram.com/academind_real
• Join our Facebook community on https://www.facebook.com/academindcha

See you in the videos!

----------

Academind is your source for online education in the areas of web development, frontend web development, backend web development, programming, coding and data science! No matter if you are looking for a tutorial, a course, a crash course, an introduction, an online tutorial or any related video, we try our best to offer you the content you are looking for. Our topics include Angular, React, Vue, Html, CSS, JavaScript, TypeScript, Redux, Nuxt.js, RxJs, Bootstrap, Laravel, Node.js, Progressive Web Apps (PWA), Ionic, React Native, Regular Expressions (RegEx), Stencil, Power BI, Amazon Web Services (AWS), Firebase or other topics, make sure to have a look at this channel or at academind.com to find the learning resource of your choice!


Content

0.12 -> Now in this video,
1.11 -> we'll get some hands-on experience with EC2,
4.53 -> we'll start an EC2 instance,
6.3 -> And most importantly, we're going to connect to it
9.03 -> and then install extra software, change some permissions,
13.14 -> some settings so that you see what can be done with EC2
17.67 -> and so that you get a real example for working with EC2,
21.99 -> in this case, to run a web server,
24.57 -> which serves this website,
27 -> which allows us to enter and save and view data
31.29 -> which is based on this code
33.48 -> which you'll, of course, find attached
35.37 -> to this video with help of AWS EC2.
39.48 -> Now we'll use EC2
40.77 -> and start a virtual server in this video.
43.83 -> And you should definitely
45.12 -> watch all the other EC2 videos first
47.94 -> because I do expect that you know what EC2 is
50.94 -> in general and what core EC2 features
53.79 -> like AMIs or instance types are all about.
57.54 -> Now, one quick note, if you don't know what AWS EC2 is
61.8 -> and which core features it has and how it generally works
66.03 -> or if you are not really familiar with AWS at all,
69.63 -> you might wanna check out my AWS cloud practitioner course
73.8 -> which teaches you AWS from the ground up
76.71 -> and even helps you pass your first AWS certification,
80.64 -> in case you're interested.
82.98 -> Now, the code for the website, (text rustling)
84.75 -> which we're going to deploy, is attached to this video.
87.63 -> It's stored on GitHub
89.46 -> and there it's in this dynamic website basic folder.
93.39 -> Now this code is written in JavaScript
96 -> and it uses NodeJS to spin up a web server
99.48 -> and the express library to handle incoming requests
103.32 -> and listen for requests on various routes.
107.28 -> As part of this application,
108.84 -> we're also using a file: data storage.json,
112.23 -> which is expected to be in this folder
114.99 -> in the root file system of your device.
118.32 -> And we use that file to store data
120.63 -> and to retrieve data.
122.1 -> In another video, we'll also use a database.
124.71 -> Here, we'll use that file though.
126.81 -> Now that's the application, the website code,
129.96 -> and there's one important note regarding this file here
133.4 -> in that path, because we're using that file
136.62 -> in that path for data storage and retrieval,
140.13 -> you will not be able to run this website locally
142.74 -> on your system unless you create that file
146.04 -> and fill it with the right content
148.35 -> in that /demo/data path.
152.55 -> Now we will create that path and file as part of this video
156.6 -> because I wanna show you how you could do things like that
159.63 -> on an EC2 instance because we'll also have to work
163.5 -> with some Linux permissions when it comes to that,
166.5 -> but I just wanted to let you know that running this website
169.53 -> on your local system could fail for that reason.
172.74 -> But the code isn't too important right now anyways.
175.26 -> Instead we wanna deploy that code.
177.3 -> So here I am in my AWS account.
179.79 -> And of course, we wanna go to the EC2 service page
182.7 -> to launch a new EC2 instance.
185.22 -> Now, in this video, I do expect that you know
187.41 -> what EC2 is generally about
189.39 -> and what all these different settings here are all about.
193.32 -> If you don't know what these things are all about,
196.23 -> you might wanna check out my cloud practitioner course
199.26 -> where I do explain AWS
201.42 -> and also this service from the ground up.
204.54 -> This video here is there
205.95 -> to help you get some hands-on experience with AWS.
210.36 -> So here we're launching a new instance as the AMI,
213.75 -> which defines our base operating system and software.
217.29 -> I'll choose Amazon Linux and I'll keep the default settings.
221.58 -> Very important it's free tier eligible,
223.65 -> which means that if we're in the first 12 months
226.29 -> after creating this account
228.06 -> and we don't exceed certain quotas,
230.22 -> we can start and run this server for free.
233.34 -> But you should check out the AWS pricing pages
236.28 -> for all the details.
237.75 -> And with that, we can choose a hardware profile.
240.78 -> That's what the instance type is all about.
243.36 -> With it, we choose a hardware profile.
245.97 -> There's a long list of hardware profiles we could use,
249.87 -> but for this demo, I'll choose this T2 micro instance
253.41 -> which is also free tier eligible,
255.48 -> which is, of course, not very powerful,
258 -> but good enough for this demo.
260.94 -> Now I won't add a key pair
262.44 -> because I won't connect to this instance
264.93 -> from my local laptop.
267.15 -> We would need a key pair for that
269.43 -> if we would want to connect from our local machine
272.64 -> via SSH to that instance once it's up and running,
275.88 -> but I won't connect from my local laptop
278.55 -> so we don't need that.
280.71 -> And for the network settings,
281.97 -> we can keep the defaults here, especially regarding the VPC.
285.75 -> However, I will edit the security group
288.33 -> that's being created for us.
290.28 -> Besides allowing SSH traffic,
292.86 -> which I will allow even though I don't plan
295.62 -> on connecting from my local laptop to the instance
298.95 -> but I will still connect to the instance in a different way.
301.95 -> Hence, I'll keep that rule.
303.87 -> But besides that rule, I'll also allow HTTP traffic
307.29 -> because we are going to run a web server
309.87 -> on that instance after all.
312.69 -> Now we can keep the default storage here.
314.61 -> We don't need more.
315.72 -> And we also don't need to edit any advanced details.
320.37 -> Instead that is all we need to do.
321.99 -> And with that, we can launch this instance.
325.68 -> Now it takes a couple of seconds
327.36 -> for the instance to be up and running.
329.49 -> We can monitor it here.
331.17 -> And once it is up and running, I'll connect to it,
334.02 -> even though I don't have a key pair,
335.85 -> and then we'll add all the software
338.07 -> and settings on that instance
339.75 -> that we need to add to finally run our web server on it.
346.92 -> Now, my instance here is up and running,
349.77 -> and therefore, we can select it and click connect.
352.65 -> Now we could connect with a local SSH client
355.98 -> from our local computer to that instance
359.19 -> if we had a key pair.
360.63 -> Then we could follow these steps here
362.43 -> to establish a connection.
364.2 -> But here in this case,
365.25 -> I don't have a key pair and I don't need one
367.77 -> because we can also use
368.988 -> (text rustling) EC2 Instance Connect.
371.13 -> This essentially allows us to connect
373.11 -> to this instance via AWS own servers.
376.59 -> So different servers than the one we started here.
380.79 -> We can keep these default settings and click connect.
382.95 -> And since we have SSH enabled on that instance
386.49 -> with help of that default security group rule
389.55 -> which we kept, since we have that enabled,
392.37 -> we can establish this connection here in the browser.
396.18 -> Now, technically the connection is now not
398.37 -> between our local computer and that instance,
400.98 -> but instead, it is between one of AWS computers somewhere
405.03 -> in one of their data centers and our instance.
408.3 -> But since we're connected to the AWS cloud
410.97 -> through the management console,
413.008 -> AWS gives us access to that terminal session
416.97 -> which it established between its computer
420.21 -> and the instance through the browser here.
423.24 -> So therefore, here we got this terminal
425.49 -> and we can run commands here
427.02 -> which will execute on our virtual instance.
430.92 -> So this terminal here is connected
433.14 -> to our EC2 instance, our virtual server.
437.13 -> So in order to run our web server here,
439.83 -> our node express web server on that instance,
442.98 -> we must clone this code,
444.78 -> which is on GitHub, onto that instance.
448.32 -> Now we do this with git clone, but if we try this,
451.05 -> we'll see that Git is not installed.
453.75 -> Therefore, we must install it.
455.31 -> And thankfully, since we picked Amazon Linux,
458.01 -> that's super simple
459.21 -> because that comes with a package manager.
461.7 -> Well, I guess all Linux distributions come
464.52 -> with a package manager, but nonetheless,
467.07 -> we can now run sudo yum update
470.43 -> to make sure the package manager
472.11 -> has all the latest references
474.09 -> to packages we can install.
475.86 -> And then we can install Git
477.39 -> by running sudo yum install git.
479.91 -> And this now downloads and installs the Git tool
482.94 -> onto this instance.
484.92 -> It can take a couple of seconds
486.09 -> but thereafter, Git will be available.
489.78 -> With that done, we can grab this cloning URL
494.73 -> and now we can run git clone and use this URL.
500.04 -> If we do that, the code is downloaded.
502.11 -> And now we got this AWS demos folder here
505.62 -> into which we can navigate.
509.49 -> In there, we find this dynamic website basic folder,
513.03 -> maybe also a couple of other folders
514.77 -> when you're watching this,
515.76 -> but we need the dynamic website basic folder now.
519.18 -> And in there,
520.29 -> we find all the code files that make up our website.
524.64 -> Now, in order to run that code
526.32 -> and spin up that node web server, we need NodeJS installed.
531 -> And just like Git, Node is not installed.
533.94 -> This tool does not exist here yet.
536.49 -> So we must install NodeJS.
539.58 -> Now this can be done by executing a couple of commands,
543.51 -> and actually, it's such a common thing to do
545.7 -> that AWS has an official guide
547.59 -> on how to install NodeJS on an EC2 instance
551.49 -> and you'll find a link to that guide attached to this video.
555.12 -> In the end, it's a couple of commands which we must execute.
558.06 -> First, a command to download
560.04 -> and install the NVM tool, the Node version manager tool,
564.21 -> which helps us install NodeJS.
567.93 -> Then, as a next step, we must activate that tool, basically.
573.6 -> And then we can install the LTS,
575.52 -> the long term stability version of NodeJS
578.22 -> or any other version we want to
580.2 -> with help of the NVM install command.
582.99 -> In this case, nvm install --lts to install that LTS,
587.07 -> that long term stability version.
590.37 -> Now that NodeJS is installed,
592.5 -> we can come back here into our project folder,
595.44 -> I actually never left it, and can run npm install
598.22 -> to install all the dependencies this code needs.
603.09 -> And once these dependencies have been installed,
605.58 -> we can run the web server by running node app.js,
608.43 -> but we won't do that yet.
611.07 -> Remember that I mentioned that this line
613.14 -> of code can be problematic
614.73 -> if you try to run the code locally
617.34 -> because we expect to have a /demo data folder
620.43 -> with a data storage JSON file inside of it.
623.49 -> And that folder is not inside of our project folder
626.31 -> but, instead, in our root folder in our system.
629.19 -> Therefore, as a first step,
630.51 -> we must make sure that that folder
632.4 -> and the data storage JSON file exists.
635.85 -> To do this, we can create that folder
638.19 -> with mkdir -p /demo/data.
643.77 -> Dash P makes sure that all the missing folders are created.
647.76 -> And we actually must add sudo here
649.5 -> since we otherwise don't have the right permissions.
653.07 -> Now we also need to run this command thereafter
655.95 -> to create this data storage.json file
659.13 -> with the appropriate content inside of it,
661.53 -> with that JSON content.
664.62 -> So once we execute this, that file will be created
667.5 -> in that demo/data folder, which we created before.
671.64 -> And now with that file in that folder set up,
674.61 -> we will be able to run our web server,
677.1 -> our NodeJS application, at least almost.
680.85 -> Right now, our application would actually fail
684.3 -> if we tried to run it
685.65 -> and then interact with it because the Node server
688.86 -> that we're going to start,
689.97 -> the Node application that we're going to start needs
692.85 -> to interact with that file here.
695.64 -> Now, since we connected
696.72 -> to this instance (text rustling)
697.68 -> with the EC2 user, this user does not
700.95 -> out-of-the-box have the appropriate permissions
703.47 -> to interact with that file.
705 -> That's why we had to add sudo all the time.
708.3 -> Now there would be different ways
709.62 -> of creating that file and running that server
712.2 -> as a root user, for example,
714.27 -> and we'll do it differently in other videos,
717.06 -> but here what we can do
718.56 -> and what we should do is we should make sure
720.84 -> that this user has appropriate permissions.
724.5 -> For that we can run sudo groupadd www
727.86 -> to add a new user group, essentially.
730.89 -> And then we add the EC2 user to that group
733.98 -> with the user mod command.
736.98 -> And after doing this, we log out with help of exit
740.31 -> because we need to reload this session here.
743.01 -> So we need to connect
744.84 -> to the server again through EC2 instance connect
748.05 -> so that these changes take effect.
751.47 -> And then thereafter, we'd pass ownership
753.9 -> of that /demo/data folder
756.57 -> by running the change ownership command like this,
759.63 -> actually with help of sudo since we need permission
763.14 -> to change that ownership.
764.76 -> We then change the permissions of the demo folder such
767.49 -> that the EC2 user is able to write into it
771.21 -> and not just read from it
773.61 -> because the Node application
775.32 -> which will be executed by the EC2 user in the end,
778.65 -> since we will start it, must be able to also write
781.95 -> to the file in that demo data folder
784.41 -> and this command makes sure
785.64 -> that we have appropriate write permissions.
788.4 -> And then thereafter, we execute these two commands.
791.52 -> The find commands here to also change the permissions
794.79 -> on all the sub-directories and files
797.34 -> to make sure that this user
799.08 -> and therefore the node application that will run through
801.72 -> that user has appropriate write permissions.
805.2 -> Now all these commands can be super confusing.
807.66 -> You don't need to be a Linux administrator
810.24 -> to work with AWS.
811.77 -> We're doing this all in a very basic way here.
814.47 -> In other videos, we'll choose a different route,
817.11 -> but with these commands executed like this,
819.66 -> we make sure that now we took all the steps needed
822.36 -> to run this Node application
824.43 -> on this EC2 instance and run it such that it is able
828.21 -> to interact with that data storage file,
830.97 -> which sits in that root folder.
833.55 -> Therefore, what we can do now is we can again navigate
836.64 -> into this AWS demos folder
838.86 -> and then there into this dynamic website basic folder.
842.76 -> And in there, we can now run Node app JS
846.06 -> to run this Node server.
849 -> And it's up and running.
851.31 -> But like this, it's not too useful, actually.
855.06 -> One problem we'll face with this application is
858.45 -> that it listens on port 3000,
861.72 -> but this port is not open on this instance.
866.13 -> Instead, we only opened the ports 22 and 80.
869.37 -> These are the only ports where we accept traffic.
873.18 -> Now we could have opened 3000,
875.79 -> but 80 is to default HTTP port.
879.15 -> Now, why don't we just change the port here to 80
883.23 -> and then redeploy that code?
886.35 -> We could do this, but as a non-root user,
889.02 -> we're actually not allowed to listen on port 80
891.69 -> on Linux systems.
893.58 -> And in general,
894.413 -> you might not wanna have your Node server listen on port 80.
898.65 -> Instead, the more elegant solution here is
901.11 -> to simply redirect traffic that reaches this instance here
905.1 -> on port 80 to port 3000 internally.
909.69 -> And that can be done if we cancel this Node server
913.11 -> with control C, that can be done
915.84 -> by running this command: the iptables command.
919.59 -> This command adds an entry
921.63 -> in the local internal IP table of that instance.
925.86 -> So the internal routing table that redirects traffic
929.64 -> that's coming in on port 80 to port 3000.
933.81 -> So other visitors, external users still send a request
938.7 -> to port 80 on that instance,
941.25 -> but internally, it will trigger port 3000
944.49 -> if we execute it like this.
946.35 -> With that done, if we now rerun Node app JS,
949.95 -> we will be able to send requests to that instance.
953.64 -> Now for that, we need to inspect the instance details here
956.85 -> and then we need either the public IP address,
959.61 -> which was assigned automatically, or the public DNS address.
963.78 -> So a domain name that was assigned automatically by AWS.
968.73 -> We can use this and send a request to that domain or IP.
972.78 -> Now it might take a while initially,
974.52 -> but thereafter, we'll see this website here.
977.13 -> And here, we can share some statements
978.99 -> which is always a good idea in the internet.
983.07 -> And once such a statement was shared, we can also visit it,
986.61 -> and of course, share our opinion,
988.14 -> which is even better in the internet
990.42 -> because people have only great opinions there.
992.88 -> And of course, they always communicate
994.56 -> in a super friendly way, right?
1001.76 -> So that's this website up and running
1003.83 -> with help of AWS and this EC2 instance.
1007.04 -> Now, as you can tell, we had to execute a couple
1009.83 -> of commands on that EC2 instance.
1012.38 -> Starting the instance was quite straightforward
1014.9 -> but then we had to go through various configuration steps.
1018.56 -> Now that was just one way of getting the code
1021.74 -> onto that instance and running that server there.
1024.44 -> It's not the only way.
1026.03 -> And in other videos, we'll see other ways
1028.31 -> and how we can simplify this process.
1030.74 -> But actually, even though it might have looked a bit complex
1034.13 -> and it might be overwhelming
1035.66 -> to see all these Linux commands here,
1037.97 -> it shows one key advantage of EC2 actually.
1041.54 -> We can control everything.
1043.88 -> We have our own computer in the cloud
1046.28 -> and we can, therefore, configure it
1048.26 -> and install software on it just as we need to.
1051.71 -> We got full control over the software on the system,
1055.01 -> over the configuration, the users, the permissions,
1058.01 -> and that is what makes EC2 so powerful
1061.79 -> because it's your computer in the cloud in the end.
1065.51 -> Now to conclude this video,
1066.74 -> there's just one thing I wanna change.
1068.84 -> I started this server here by running Node app JS.
1071.96 -> But the problem with that is that
1073.55 -> if I now close my connection here and I try to reload this,
1076.97 -> the website is gone because I canceled this process
1080.69 -> which I started by running Node app JS.
1083.51 -> Of course, that's not how it should be.
1085.64 -> Therefore, I'll reconnect.
1087.62 -> And I can go back into my folder here
1090.14 -> and run Node app JS again, right?
1092.6 -> But I now have to keep my computer up
1094.85 -> and running as long as the website should be up and running.
1098.36 -> That's, of course, not ideal.
1099.86 -> And therefore, a better solution instead of doing that is
1103.34 -> to install an extra tool
1104.69 -> which will run that server in the background.
1106.94 -> For example, the PM2 tool
1108.8 -> which we can install with the npm install pm2@latest -g
1112.67 -> to install it globally.
1114.68 -> This will install this tool on this machine
1116.99 -> and PM2, in the end, is just a tool that we can use
1120.14 -> to start our node server, but in the background
1123.47 -> so that we can safely close this terminal here,
1127.04 -> this session.
1129.29 -> Once it is installed, we can run pm2 start app js.
1132.92 -> And now this starts the node server in the background.
1136.16 -> We can type other commands here
1137.99 -> and we can close this session.
1140.75 -> And despite it being closed,
1142.19 -> if I reload this website, it's still there.
1144.89 -> Our data is, of course, also still there
1146.75 -> because we did not terminate the instance in between.
1149.93 -> Speaking of that though,
1151.19 -> now with the demo finished,
1152.69 -> we can, of course, terminate that instance
1154.46 -> to make sure we're not paying
1155.84 -> for any resources we don't need
1157.79 -> even though here we're inside of the free tier,
1159.89 -> but still, and we can recreate it whenever we need to.
1163.07 -> Of course, our data would be lost,
1164.72 -> but we'll take care about that in another video.

Source: https://www.youtube.com/watch?v=74e1asRdR7k