Deploy NodeJS application on EC2 via launch template - node.js

I built a simple NodeJS application and ran on an EC2 instance.
Everything works fine. I decided to create an AMI (Amazon Linux based) and a launch template to be used by a ASG.
The problem is, I cannot get the application start automatically.
I tried to add the following command through the user_data field but it doesn't work:
node main.js
Any ideas on how to automatically start this application once launched by the ASG?

Typically you would add this to the startup script of the AMI, so once the instance has started it will run the script on boot.
You may want to look at PM2 as well, it's a great tool for things like this and also makes it easy to setup each node instance in cluster mode ( assuming you have an EC2 instance with more than one core )
Some other ways of doing this, albeit not an auto scale but with DigitalOcean they offer a CASS model called 'apps' that basically pushes you app into a container from a git-repo and deploys it, you can then just spin new instances out as needed. Downside is that the bandwidth is a bit small but CND etc can help with that.

Related

AWS CD/CI pipeline with ASG and ELB (bitbucket)

I must first preface by saying I am relatively new to AWS but finding it immensely useful. Let me describe my scenario...
What I have currently
An Auto scaling group (ASG)
An Elastic Load Balancer (ELB)
A CD/CI pipeline using CodeDeploy and Bitbucket
Node/Express app serving a custom API on EC2 instances
VPC and subnets are working well
An AMI with my app code
My question
When the ASG decides to scale in a new EC2 instance using my launch template and AMI, it will use the application code from the AMI. But if I deploy to master at some point, my AMI will not be updated, but instances within the ASG will be updated. What is the best method for ensuring the new instances spawned by the ASG are running the latest code version (master)?
My initial thoughts
I am thinking to include a bash script in the launch config that will pull the latest code from Bitbucket and run any following steps to get my application running (such as "npm install", "npm run start" etc etc). I'm sure someone has a more elegant solution, and I'd love to hear some suggestions.
To anyone coming to this, I did solve my problem. I was correct initially. The "user data" field within the Launch Template was a good place to bootstrap my app once the instance is up and running. It basically clones from the remote repo and performs any necessary steps to launch the app after that.
For example, in the launch configuration for EC2
#cloud-boothook
#!/bin/bash
git clone myremoterepo.git
cd myremoterepo
npm install
npm run start
Also, if you're running a classic load balancer, CodeDeploy will attempt to start a deployment, based on your latest code repo in S3, when your ASG scales up the EC2 instances. So the above solution will be redundant.

How to run nginx and Node.js with EC2 (linux) server startup

How to run nginx and Node.js at server startup?
In order to start Amazon EC2 with AWS Auto Scaling, I must directly connect to EC2 to run nginx and Node.js.
Can this be done for Auto Scaling?
In Amazon EC2, I want to run nginx with Node.js during EC2 startup by Auto Scaling.
EC2 is set up as an Auto Scaling group using images. I want to run EC2 Node.js applications and nginx, which are started by Auto Scaling, together with the EC2 server startup.
For nginx, I can run the executable with chkconfig, but the Node.js application will run as pm2, using the code written in package.json.
How can I run nginx and Node.js with EC2 startup and let the new EC2 -- started with Auto Scaling -- respond properly?
comment reply :
I don't want to run node.js using "node app.js" command.
I want to run node.js by package.json ( script )
ex.
"start": "NODE_ENV=production PORT=3000 pm2 start server.js -i -1"
How can I do this?
Your suggestions are using linux server init script file.
But, I want to set NODE_ENV, PORT and use pm2 command.
solution
I solved the problem.
When Linux booted, I tried to use the script file to automatically run node.js.
I created the script file and made the shell script run automatically after linux booted, but it did not seem to be a good idea.
Alternatively, pm2 startup and ecosystem.config.js can be used to solve problems flexibly.
Thank you for your reply.
This has nothing to do with autoscaling. It most often has to do with the EC2 AMI (Amazon Machine Image) that the autoscaler is launching your EC2 instances with, and possibly also with the "user metadata" that you are passing to the instance when it launches. These are the only two things that impact what an EC2 instance does when and after it starts up, up until it starts communicating with the outside world.
So what you need to do is create an AMI that is set up so that the right things launch when an EC2 instance is launched from that AMI. What you'd do is take the AMI you want to use as a starting point, launch that AMI into an instance, make the necessary changes and installations you want, and then save off a new AMI. Then change your autoscaling group to launch new instances with that new AMI.
You could alternately send a script in your "user metadata" that launches things, but this is rarely what you want to do". Most of the time, you want to have your AMI do the right thing.
It's also possible that you are using some sort of post-boot provisioner, like Chef, Ansible or Chef Habitat. If you are, that's where you'd set all of this stuff up. You'd want that system to do the work you're describing. But if you're doing that, what I have said earlier still applies. For this to work, you'd often have also built a custom AMI that has parts of the provisioning system already built into it, so that that system can connect into it and provision it. It's possible for these systems to start from a generic AMI as well. It depends on the system.

AWS: How to launch multiple of the same instance from python?

I have an AWS Windows Server 2016 VM. This VM has a bunch of libraries/software installed (dependencies).
I'd like to, using python3, launch and deploy multiple clones of this instance. I want to do this so that I can use them almost like batch compute nodes in Azure.
I am not very familiar with AWS, but I did find this tutorial.
Unfortunately, it shows how to launch an instance from the store, not an existing configured one.
How would I do what I want to achieve? Should I create an AMI from my configured VM and then just launch that?
Any up-to-date links and/or advice would be appreciated.
Yes, you can create an AMI from the running instance, then launch N instances from that AMI. You can do both using the AWS console or you could call boto3 create_image() and run_instances(). Alternatively, look at Packer for creating AMIs.
You don't strictly need to create an AMI. You could simply the bootstrap each instance as it launches via a user data script or some form of CM like Ansible.

AWS EC2 AMI launch with user-data

I am trying to launch my own AMI using user-data so that it can run a script and then terminate.
So I launched an Ec2 Windows Base and configure it to have all the tools I need (NodeJS etc) and saved my script to C:\Projects\index.js.
I then saved it as an Image.
So I then used the console to launch an EC2 from my new AMI with the user-data of
node C:\Projects\index.js --uuid=1
</powershell>
If I run that command having RDP into the EC2 it works, so it seems that the userdata did not run when the Image was started.
Having read some of the other questions and answers it could be because the AMI created was made from an Instance that started already. So the userdata did not persist.
Can anyone advise me on how I can launch my AMI with a custom userdata each time? (as the UUID will change)
Thanks
Another solution that worked for me is to run Sysprep with EC2Launch.
The issue is that AWS doesn't reestablish the route to the profile service (169.254.169.254) in your custom AMI. See response by SanjitPatel in this post. So when I tried to use my custom AMI to create spot requests, my new instances were failing to find user data.
Shutting down with Sysprep, essentially forces AWS re-do all setup work on the instance, as if it were run for the first time. So when you create your instance, shut it down with Sysprep and then create your custom AMI, AWS will setup the profile service route correctly for the new instances and execute your user data. This also avoids manually changing Windows Tasks and executing user data on subsequent boots, as persist tag does.
Here is a quick step-by-step:
1.Create an instance using one of the AWS Windows AMIs (Windows Server 2016 Nano Server doesn't support Sysprep) and passing your desired user data (this may be optional, but good to make sure AWS wires setup scripts correctly to handle user data).
2.Customize your instance as needed.
3.Shut down your instance with Sysprep. Just open EC2LaunchSettings application and click "Shutdown with Sysprep".
4.Create your custom AMI from the instance you just shut down.
5.Use your custom AMI to create other instances, passing user data on instance creation. User data will be executed on instance launch. In my case, I used Spot Request screen, which had a User Data text box.
Hope this helps!

Install vFrabric tc server (as bundled with STS) into Linux EC2 instance?

I have been developing a Spring MVC web app using Springsource Tool Suite (STS). STS comes with vFabric tc server developer edition I believe. When I deploy the .war file into my EC2 Linux AMI instance running tomcat6, there are incompatibilities between tomcat and vFabric.
For example, vFabric will accept objectName.getMethod() whereas it will return an error in tomcat6. I have to change it to objectName.method.
Here is my question. So, when making changes now, I don't use STS anymore and every change I have to package and redeploy which is time consuming. What is a good way around this?
I am thinking of installing the vFrabic tc server in my EC2 linux instance. Will that work? If I do that, theoretically, everything I develop in STS should be 100% compatible when deployed, correct?
Second question. How do I install vFrabic tc server in my EC2 instance?
Thanks!
One suggestion would be to develop inside of STS using a Tomcat server instead of tcServer. However, then of course you loose some of the nice capabilities of tcServer.
If no one here can give you a complete answer, I would recommend the SpringSource forums (for STS):
http://forum.springsource.org/forumdisplay.php?32-SpringSource-Tool-Suite
or here for tcServer:
http://forum.springsource.org/forumdisplay.php?64-tc-Server-General

Resources