Launching an AWS EC2 Instance via AWS CLI

Larry Johnson
4 min readJul 20, 2023

--

In a previous article, we went thru how to launch an AWS EC2 Instance via the console in a web browser. Here is how you can do the same thing but from the AWS CLI.

Prerequisites

  • An AWS account
  • AWS CLI installed and configured on your computer
  • Established VPC
  1. Our Command

For this project we’ll be utilizing one command. Don’t worry thru the article you’ll understand what each value means.

aws ec2 run-instances --image-id <value> --instance-type <value> --key-name <value> --subnet-id <value> --security-group-ids <value> --user-data file://apache.sh

2. Setting our Values
Find your— image-id <value>: We’ll find this value by browsing to the EC2 console. We’ll be using N. Virginia (US-East-1) as our region to deploy. Select “AMI catalog” on the left side under images you’ll be brought to the collection of images that can be used.

Select your desired AMI image that you would like to use. Copy the AMI-ID this will be your image-id that will be utilized for this instance. We’re going to be using “ami-06ca3ca175f37dd66”.

— instance-type <value>: Stay in this same screen click on select which will bring you to a launch template. We’re not utlizing the console we’ll be doing all this over CLI. Scroll down to instance type and select the size of the instance you would like. We’re going to be using “t2.micro”.

— security-group-ids <value>: Moving over to our next value this will be located in your VPC console. Scroll down on the left side to find “Security Groups”. Find your desired security group that you would like to use. This will take the place of <value >. This one will be “sg-0abced459539a2fec”.

— subnet-id <value>: Staying in the same console click on “Subnets”. Click your subnet and copy the ID that will be at the bottom of the screen. Subnet ID will be “subnet-05a2b93707b6332c7”.

— key-name <value>: Your key pair will be located in your EC2 console under Network & Security under Key Pairs. Copy the name of the key pair. Key pair name is “week5”.

— user-data <value>: Will be the script that you created on your local system to deploy the services that you want on this instance. In this case we’ll be using a bash script that install Apache.

#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
systemctl start httpd
systemctl enable httpd

3. Running our command

Time to run our script once we have filled in those values. You’ll enter the following script in our AWS CLI. *Reminder your values including AMI will differ from mines.

aws ec2 run-instances --image-id ami-06ca3ca175f37dd66 --instance-type t2.micro --security-group-ids sg-0abced459539a2fec --subnet-id subnet-05a2b93707b6332c7 --key-name week5 --user-data file://apache.sh

Once the command is ran you should receive an ouput. If your differ refer to the error code and correct the issue it states.

Confirm your instance is running by going back to your EC2 console, it may take a minute for the instance to be deployed.

http://yourip

You’ll see the Apache Test Page once you input your IP Address.

If you’ve made it here thanks for reading and feel free to connect with me on LinkedIn.

--

--