Unlock the full potential of AWS EC2 deployment by leveraging the power of AWS CLI. This guide takes you through the seamless process of creating and configuring an EC2 instance to host both the Apache web server and WordPress, ensuring a robust and efficient setup.
Essential Prerequisites
Before embarking on this journey, make sure you have the following essentials in place:
- Active AWS Account: Ensure you have an active AWS account to initiate the deployment process.
- IAM User Permissions: Create an IAM user with permissions for AmazonEC2FullAccess and AmazonVPCFullAccess to streamline the deployment.
- AWS CLI on Local Machine: Have AWS CLI installed on your local machine for a smooth and efficient deployment experience.
- SSH Client on Local Machine: Install an SSH client on your local machine to establish secure connections with the EC2 instance.
Setting up the AWS VPC
To kickstart the EC2 instance creation process, the first step involves setting up a Virtual Private Cloud (VPC) with public and private subnets, an internet gateway, and a route table.
Creating a VPC
AWS_VPC=$(aws ec2 create-vpc \ --cidr-block 10.0.0.0/16 \ --query 'Vpc.{VpcId:VpcId}' \ --output text) aws ec2 create-tags \ --resources $AWS_VPC \ --tags Key=Name,Value=DevOpsVPC
Modifying VPC Attributes
aws ec2 modify-vpc-attribute \ --vpc-id $AWS_VPC \ --enable-dns-hostnames "{\"Value\":true}" aws ec2 modify-vpc-attribute \ --vpc-id $AWS_VPC \ --enable-dns-support "{\"Value\":true}"
Creating Public and Private Subnets
# Create a public subnet AWS_PUBLIC_SUBNET=$(aws ec2 create-subnet \ --vpc-id $AWS_VPC \ --cidr-block 10.0.1.0/24 \ --availability-zone us-east-1a \ --query 'Subnet.{SubnetId:SubnetId}' \ --output text) aws ec2 create-tags \ --resources $AWS_PUBLIC_SUBNET \ --tags Key=Name,Value=DevOpsPublicSubnet # Create a private subnet AWS_PRIVATE_SUBNET=$(aws ec2 create-subnet \ --vpc-id $AWS_VPC \ --cidr-block 10.0.2.0/24 \ --availability-zone us-east-1a \ --query 'Subnet.{SubnetId:SubnetId}' \ --output text) aws ec2 create-tags \ --resources $AWS_PRIVATE_SUBNET \ --tags Key=Name,Value=DevOpsPrivateSubnet
Continue the setup by enabling auto-assigning public IP, creating an internet gateway, NAT gateway, and configuring route tables. For brevity, the commands are omitted here.
Creating a Security Group
# Create a security group AWS_SECURITY_GROUP=$(aws ec2 create-security-group \ --group-name DevOpsSG \ --description "DevOps Security Group" \ --vpc-id $AWS_VPC \ --query 'GroupId' \ --output text) aws ec2 create-tags \ --resources $AWS_SECURITY_GROUP \ --tags Key=Name,Value=DevOpsSG # Add rules to the security group aws ec2 authorize-security-group-ingress \ --group-id $AWS_SECURITY_GROUP \ --protocol tcp \ --port 22 \ --cidr 0.0.0.0/0 aws ec2 authorize-security-group-ingress \ --group-id $AWS_SECURITY_GROUP \ --protocol tcp \ --port 80 \ --cidr 0.0.0.0/0
Deploying an AWS EC2 Instance
Before launching the EC2 instance, obtain the latest Amazon Machine Image (AMI) ID and create a key pair.
Obtaining the Latest AMI ID
AWS_AMI=$(aws ec2 describe-images \ --owners 'amazon' \ --filters 'Name=name,Values=amzn2-ami-hvm-2.0.20221004.0-x86_64-gp2' \ 'Name=state,Values=available' \ --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' \ --output 'text')
Creating a Key Pair
aws ec2 create-key-pair \ --key-name DevOpsKeyPair \ --query 'KeyMaterial' \ --output text > DevOpsKeyPair.pem chmod 400 DevOpsKeyPair.pem
Launching the EC2 Instance
AWS_EC2_INSTANCE=$(aws ec2 run-instances \ --image-id $AWS_AMI \ --instance-type t2.micro \ --key-name DevOpsKeyPair \ --monitoring "Enabled=false" \ --security-group-ids $AWS_SECURITY_GROUP \ --subnet-id $AWS_PUBLIC_SUBNET \ --user-data file://install.sh \ --private-ip-address 10.0.1.10 \ --query 'Instances[0].InstanceId' \ --output text) aws ec2 create-tags \ --resources $AWS_EC2_INSTANCE \ --tags "Key=Name,Value=DevOpsInstance"
Verifying and Accessing Your EC2 Instance
Check the status and obtain the public IP address of your EC2 instance.
SSH into the EC2 Instance
AWS_PUBLIC_IP=$(aws ec2 describe-instances \ --instance-ids $AWS_EC2_INSTANCE \ --query 'Reservations[*].Instances[*].[PublicIpAddress]' \ --output text) ssh -i DevOpsKeyPair.pem ec2-user@$AWS_PUBLIC_IP
Congratulations! You’ve successfully created and configured an AWS EC2 instance, equipped with all the necessary components for hosting the Apache web server and WordPress.
Frequently Asked Questions (FAQs)
- Q: What is AWS EC2, and why is the CLI method recommended?
- A: Amazon Elastic Compute Cloud (EC2) is a web service that provides resizable compute capacity in the cloud. Using the AWS Command Line Interface (CLI) streamlines the deployment process, allowing for efficient configuration and management of EC2 instances through command-line commands.
- Q: What prerequisites are necessary before following the guide?
- A: Ensure you have an active AWS account, an IAM user with specific permissions, AWS CLI installed on your local machine, and an SSH client for secure connections.
- Q: Why is setting up a Virtual Private Cloud (VPC) necessary?
- A: A VPC provides a logically isolated section of the AWS Cloud where you can launch resources. It’s a fundamental step for creating a secure and controlled environment for your EC2 instances.
- Q: What are the key components of the VPC setup process outlined in the guide?
- A: The guide covers creating a VPC, modifying VPC attributes for DNS support, creating public and private subnets, and setting up essential components like internet gateways and route tables.
- Q: Why is a Security Group created, and what rules does it include?
- A: The Security Group enhances network security by controlling inbound and outbound traffic. The rules in the Security Group allow SSH access (port 22) and HTTP access (port 80) for effective communication.
- Q: What role does the Amazon Machine Image (AMI) play in EC2 instance creation?
- A: The AMI is a pre-configured template that contains the software configuration, including the operating system and application server. The guide helps you obtain the latest AMI ID for a seamless EC2 instance launch.
- Q: Why create a Key Pair, and how is it used in the EC2 instance creation process?
- A: A Key Pair is essential for secure access to your EC2 instance. The private key generated during Key Pair creation is used to establish an SSH connection to the instance.
- Q: What is the significance of user data in launching an EC2 instance?
- A: User data allows you to run scripts or commands when an EC2 instance starts. In the guide, it’s utilized to execute essential configurations during the instance launch.
- Q: How can I verify the status and access my EC2 instance after deployment?
- A: The guide provides commands to check the instance status and obtain its public IP address. Use SSH with the generated Key Pair to securely access the EC2 instance.
- Q: Can I customize the guide for different instance types or configurations?
- A: Yes, the guide serves as a foundation. You can customize parameters like instance type, subnet, and security group settings based on your specific requirements.
Feel free to reach out if you have additional questions or require further clarification on any aspect of the AWS EC2 Deployment with CLI guide.