How To Create An AWS EC2 Instance Using AWS CLI
- Mohammad Abu Mattar
- Cloud Computing
- 30 Oct, 2022
- 05 Mins read
Introduction
We will learn how to create an AWS EC2 instance using AWS CLI in this tutorial. We will also discover how to set up an AWS EC2 instance so that it functions with the Apache web server. We will also discover how to set up an AWS EC2 instance so that it functions with WordPress.
Prerequisites
To follow along with this tutorial, you will need:
- An AWS account.
- An AWS IAM user with the following permissions:
- AmazonEC2FullAccess
- AmazonVPCFullAccess
- An AWS CLI installed on your computer.
- An SSH client installed on your computer.
Create an AWS VPC
Before we create an AWS EC2 instance using AWS CLI, we need to create a VPC. We will also create a public and private subnet, an internet gateway, and a route table.
Create an VPC
To create a VPC, run the following command:
Modify your custom VPC and enable DNS hostname support, and DNS support
To modify your custom VPC and enable DNS hostname support, and DNS support, run the following command:
Create a Public Subnet
To create a public subnet, run the following command:
Create a Private Subnet
To create a private subnet, run the following command:
Enable Auto-assign Public IP on the subnet
To enable auto-assign public IP on the subnet, run the following command:
Create an Internet Gateway
To create an internet gateway, run the following command:
Create an NAT gateway
To create an NAT gateway, run the following command:
Attach the Internet gateway to your VPC
To attach the Internet gateway to your VPC, run the following command:
Create a custom route table
To create a route table, run the following command:
Create a custom route table association
To create a custom route table association, run the following command:
Associate the subnet with route table, making it a public subnet
To associate the subnet with route table, making it a public subnet, run the following command:
Associate the NAT gateway with the route table, making it a private subnet
To associate the NAT gateway with the route table, making it a private subnet, run the following command:
Create a security group
To create a security group, run the following command:
Add a rule to the security group
To add a rule to the security group, run the following command:
Create an AWS EC2 instance
Get the latest AMI ID
To get the latest AMI ID, run the following command:
Create a key pair
To create a key pair, run the following command:
Create an EC2 instance
Before creating an EC2 instance, you need to create a user data script. That script will be automation previous three blog posts:
- How to Install Apache Web Server on Amazon Linux 2
- How to Install PHP and MariaDB on Amazon Linux 2
- How To Create An AWS EC2 Instance Using AWS CLI
You can find the user data script in the GitHub repository
To create an EC2 instance, run the following command:
Check the status of the EC2 instance
To check the status of the EC2 instance, run the following command:
Get the public ip address of your instance
To get the public ip address of your instance, run the following command:
SSH into the EC2 instance
To SSH into the EC2 instance, run the following command:
Show the WordPress website
Conclusion
In this blog post, you learned how to create a VPC, a public subnet, a private subnet, a NAT gateway, a route table, a security group, and an EC2 instance. You also learned how to SSH into the EC2 instance.