In starting my journey to embrace Infrastructure as Code, I want to understand how to operate AWS remotely and via CLI versus the AWS Console. Knowing that most of the time this will be accomplished via automation tools like CloudFormation, there still will be an operational need for point in time administration of instances and services.
I figured I would start very simply and create an AWS EC2 instance via CLI. Instead of using the AWS CLI, I instead opted for AWS PowerShell for it’s scripting ability down the road. In this blog entry I’ll walk through the installation of the AWS PowerShell Module as well as the commands to setup your profile, query a public AMI, instantiate the instance, then stop and terminate it.
First let’s start by installing the AWS PowerShell Module. To install the module simply run the following:
Install-Module -Name AWSPowerShell
To validate it installed, query the version using the following command:
Get-AWSPowerShellVersion
Alright, now that the AWS PowerShell is installed and verified, let’s go ahead and set it up. To do this, you will need the keys for the user which will be executing the command. If you do not already have your Access Key ID and Secret Access Key available from when you created your user, navigate to the AWS Console and add one.
Once added, go back into PowerShell and setup your default keys and settings using the following command:
aws configure
Now you have the ability to issue commands through the AWS PowerShell which will be executed based on the privileges of the user/keys you inputted.
To create an AWS EC2 instance, you must first identify the AMI (Amazon Machine Image) you want to leverage for the deployment. You can query a list of the Windows AMIs by issuing the following command:
Get-EC2ImagebyName
I know I want a Windows 2016 Base image so I can even select that name specifically so I can reference the AMI ID for creation. Note the AMI ID of the instance as we will use that to create the instance.
Get-EC2ImagebyName -Name WINDOWS_2016_BASE
Now it’s time to create the instance itself. There are a lot of options and switches for this command but since this is an introductory post, I’ve chosen to simplify it as much as possible. We need to use the New-EC2Instance command paired with the AMI ID:
New-EC2Instance -ImageId ami-06bee8e1000e44ca4
Once you have executed this command, the instance is being created. Reference the ReservationID above and go to the console to check it out and validate it is running
Here in the EC2 section of the AWS Console, you can select the instance which is initializing and scroll down to correlate what is in the console matches what you had in PowerShell.
Now that we have created the instance, we also can Stop and Terminate the instance using the Instance ID, To get the Instance ID, run the following:
Get-EC2InstanceStatus
Now that we have the Instance ID for the machine we want to Stop and Terminate, we can issue those commands:
Stop-EC2Instance -InstanceID i-01b750a6e7dd03399
Remove-EC2Instance -InstanceID i-01b750a6e7dd03399
We can verify the instance was terminated by checking the console:
That’s it for now. Granted this walk-through is just a piece of the process needed to compose infrastructure in AWS but it helps demonstrate the power of addressing infrastructure as code as these commands can be grouped into scripts and launched by events.