Using (AMZ) Amazon CLI to launch an EC2 instance, with an Apache server

Gary Young, Junior
4 min readMay 15, 2023

--

Artwork by Gary Young, Jr.

This article will include how to automate a data script to launch an EC2 instance, by using AWS CLI to launch the instance.

Running a CMMD on an EC2 instance at launch

To refresh on how to create and connect to a instance click here. To associate a script upon turnover of the instance, use the “User data” field, which is located under the “Advanced details” section of your instance your spinning up.

This is your script to install, update, start, & enable your Apache web server

Next, AWS Command Line Interface (CLI)

AWS has a command line interface (CLI) that allows users to interact with AWS services using commands through their computer’s terminal. To utilize this tool, lets download the AWS CLI. The directions on how to download the CLI slightly differ from Windows, Mac, and Linux operating systems, and detailed installation instructions can be found by a friendly google search.

Configure the CLI, then set up secret access key information, your AWS region, and output format. Here are the following commands.

SSH’d into the instance

To configure a AWS Access Key ID, click on Users, then [click*] on the user [name]. Scroll down, you will see security credentials [click*] it .Then scroll down to Access Keys [click*] here you will be creating your security access keys.

Scroll down to see ACCESS KEYS.
[ec2-user@ip.address ~]$ aws configure
You will get prompted to an AWS Access Key ID; Secret Access Key; Default Region Name; and HIT ENTER for Default Output Format.

Now you have a configured AWS CLI, we’ll be utilizing it to create an EC2 instance with a data script that updates packages, installs Apache, then start the Apache service. After you have installed the AWS CLI, Set your IAM permissions to allow the user access the EC2. To add permission, navigate to the IAM console, then click “Users,” on the left side tab. Select the user you want to apply the permission, click “the user name.” Once your under permissions add “AmazonEC2FullAccess” and “AmazonS3FullAccess”. By default, IAM users do not have permission to create or modify EC2 instances.

The user now has access to work with the EC2, you need to create a key pair if you don’t already have one, and a security group. To create a key pair through the console, navigate to EC2, then select “Key Pairs” on the left menu, and “Create key pair” and save it as an RSA and .pem file. Create a security group through the CLI, using the following command:

aws ec2 create-security-group --group-name <name> --description "created from awscli"
security group being created via AWS CLI
aws ec2 authorize-security-group-ingress \
--group-name <name> \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0
configuring inbound traffic
aws ec2 authorize-security-group-egress --group-id <sg-id> --ip-permissions IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=0.0.0.0/0}]'
configuring outbound traffic

Launching an EC2 Instance Using the AWS CLI

The command we will use to launch an instance is:

aws ec2 run-instances --image-id <ami_id> --instance-type t2.micro
--security-group-ids <security_group_id>
--subnet-id <subnet_id>
--key-name <keyname>
--user-data file://<user_date_file_name>

This commands requires several pieces of information to get the instance up and going. We will need the following:

  • Amazon machine image ID <ami-xxxxx>
  • Instance type <t2.micro>
  • Security Group IDs <sg-xxxxx>
  • Subnet <subnet-xxxxx>
  • Key name <keypair> no .pem
  • User data file <file://filename>
Once you run the command
To stop the instance, if you so choose too.

Lets check our work. If you arrive at this page. 👏

--

--