AWS Virtual Machine automation is a powerful tool that can help you create and manage your AWS resources more efficiently. In this article, we will show you how to automate the creation of virtual machines from the command line. First, let’s take a look at some of the basics: -You need to have an AWS account and be logged in. -To create a new virtual machine, use the following command: aws create-vm –name myvm –instance-count 1 The first argument is the name of the virtual machine, and the second argument is its number of cores (1 for now). You can also specify a size for your virtual machine by using the –size argument. For example, if you want to create a 2GB virtual machine, use the following command: aws create-vm –name myvm –instance-count 2 The next step is to set up your environment: -First, set up your environment by running this command: aws config set This will add some default settings for your new virtual machine. For example, you can set up networking and security features by running this command: aws config set networkInterfaces “Ethernet” “Wired” “SecurityGroup” Next, add an IAM user to manage your VM: 2) Create an IAM user for your new VM: aws iam add -user myvm -group MyGroup -role Administrator 3) Set up networking and security features on your VM using these commands: aws network attach myvm “Ethernet” “Wired” “SecurityGroup” aws security attach myvm “Ethernet” “Wired” “SecurityRule”


It’s easy to launch new instances from the EC2 Console, but sometimes you need to automate the task from a shell script. In that case, the AWS CLI has extensive commands for launching instances from the command line.

What You’ll Need

To start, you’ll need the AWS CLI installed and configured with an IAM user that has access to create new instances.

Unless you want to automate the creation of them, you’ll need an existing security group with the rules already configured. While the web interface creates a new group for each instance by default, you can reuse security groups for any number of instances. Keep in mind, though, that a port opened in a security group will open that port on each instance.

If you want to create a new security group from the command line, you can create and open ports with the following commands:

Whichever route you choose, you’ll need to make note of the security group ID. You can find this from the command line by describing the security group:

Which will show info about the group, including the group ID.

You’ll need to make note of the name of the key pair you use to access the instances. For simplicity’s sake, you likely want to use the same key pair, rather than creating a new one for each new instance you create.

You’ll need to know your subnet ID. You’ll want to launch all your instances in the same subnet and definitely in the same availability zone, as there are costs to transferring data between zones. You can find the subnet ID from the AWS EC2 console, or from the command line with jq:

Create Instances from the Command Line

You can launch instances using the run-instances command. This command has a lot of options, but the ones that matter most are:

You’ll have to replace the AMI ID, key pair name, security group ID, and subnet ID with your own values. The final –tag-specification directive assigns the name to the instance, which you’ll want to change as well.

This will launch a basic t2.micro micro instance in your account’s default region. This command will output a lot of JSON data:

You’ll probably want to pipe this to a file and save it for later reference. You can use a utility like jq to work with JSON on the command line, or import it into a Python or Node script for easy access.

Once the instance is created, you’ll be able to access it over SSH from its public IP address, or private IP address if you have an OpenVPN server running in your subnet.

Setting Up an Install Script

AWS allows you to pass an install script to the instance that will be ran at launch. In this script, you can automatically perform any configuration of the machine you need to do, such as installing and configuring programs.

To pass the script from the CLI, you’ll want to use the –user-data option, and pass the file using file://:

This will only run once, and not on every reboot.

Creating a Custom AMI

To create one, you’ll likely want to start from scratch to ensure everything is correct, though you can just use your preconfigured server as a template.

Then, from the AWS EC2 console, right-click on your instance and choose Image > Create Image:

This will automatically create an AMI based on your instance. You can also create AMI based on snapshots, but this wizard will create a snapshot for you to create the AMI with.

You AMI may take a bit to create, but once it’s done you’ll be able to access it in the AMI tab of the EC2 console. You can take the AMI ID and use it to create new instances based off that AMI. You can also use it as the base for an Autoscaling Group launch template, which is covered in our guide to using AMIs.