When provisioning and decommissioning Amazon servers in the cloud, we can use the console or we can use code. We’re programmers, right? Well let’s follow DevOps principles and embrace our coding capabilities to deploy AWS EC2 infrastructure in a scalable way.
Table of Contents:
- Assumptions (aka ensuring you have everything you need)
- That code!
- Additional resources
Assumptions before jumping in:
- You have Node.js installed. Download at the Node.js website.
- You have created a shared credentials file. For more information, see loading credentials in Node.js.
- You have created a key pair. For details, see working with Amazon EC2 Key Pairs. You use the name of the key pair in this example.
- You have the
ImageIdof the AMI you want to launch. For details, see getting an AMI ID. You will use the
ImageIdin this example.
- You have the
SubnetIdfor the subnet you want to launch the instance into. Check out this article on listing aws subnets for more information. You will need a
OK, now that we are all set to start building, let’s get to it!
Install the AWS SDK
npm install aws-sdk
That was easy! Onward and upward, now the cool parts!
Launching an AWS EC2 Instance
First let’s launch an EC2 instance. Here is the full block of code that will get you there!
OK, let’s take a moment to break down some of what we are doing. I will cut the code into chunks and explain each section. Starting from the top…
First is the setup. What we initially do is load the amazon sdk i.e.
aws-sdk. Next we set our region. As you probably know, there is a wide array of AWS regions. I chose the west coast (Oregon), but there are numerous regions you can choose from. We also create a new
ec2 service object which we utilize later in the code. Cool? Cool!
Next we setup the instance parameters. This is information specific to our EC2 instance(s).
There are a few things I’d like to point out here. First is the
ImageId, this is a unique identifier that is particular to the region that you can obtain from AWS, which specifies the server instance you want to use. Second is the
InstanceType. As we are targeting “free-tier” this is set to
Note: be sure to check your region and setup to ensure you are using free-tier. Do not send your bills here!
KeyName is the key pair you have setup.
MinCount is the minimum number of instances to launch, while
MaxCount is (yup, you guessed it) the maximum number of servers to launch. Both of these must be set to 1 or greater. For more details, go here.
You will notice I added the
TagSpecifications to set a tag, which lets me know I created this instance via code. I find this helpful when checking that my code worked!
Finally, I deploy the instance by calling
runInstances with the specified
params and pass in a callback function to handle the response.
Do note that you can also utilize promises as well instead of callbacks, as seen here. My examples follow the standard callback syntax utilized in the AWS API documentation.
Now that all the details are outlined, let’s launch! Type the following in the command line to run the example:
This will output a lot of information from the creation of this instance. To continue on and stop (then terminate) this instance, be sure to grab the
InstanceId of the EC2 server we just created!
Stopping an AWS EC2 Instance
Great, so we created an instance, now what? Well, one thing we can do is stop said instance (or instances). As you can see, most of the code is similar to deploying an instance.
The key difference (outside of the call to
stopInstances) is the
params object, which takes an array of
InstanceIds to stop. So, to stop the instance we started, we must add the
InstanceId into the
Once you have entered the instance id, type the following to stop the instance:
Boom, instance stopped!
Terminating an AWS EC2 Instance
So, we stopped the instance, we are done, right? Well, not quite. Launching an instance provisions our server, but the server is still there when stopped. To fully decommission, we need to terminate the instance.
As you can see, most of this code is pretty similar to our previous examples and astute readers are already figuring ways to refactor the code to make it more maintainable, economic, and efficient.
Similar to when we stop the instance, we add the target instance id to the params object.
Once we are ready to terminate, we type the following:
And just like that, we have destroyed our instance.
In this article, we have utilized Node.js to create, stop, and terminate Amazon EC2 instances. We are now that much closer to automating the deployment of our servers, versioning our infrastructure, embracing DevOps principles, and leveraging IAC to scale and accelerate our organization. Ain’t no stopping us, let’s keep pushing!
Check out the additional resources below and happy coding!