Today, we will be test the aws knowledge on services in AWS, as part of the 90 Days of DevOps Challenge.
Task-01
Launch an EC2 instance using the AWS Management Console and connect to it using SSH.
Install a web server on the EC2 instance and deploy a simple web application.
Monitor the EC2 instance using Amazon CloudWatch and troubleshoot any issues that arise
Launching an EC2 instance on AWS, installing a web server, deploying a simple web application, and monitoring with Amazon CloudWatch involves several steps. Here's a step-by-step guide:
Step 1: Launch EC2 Instance
Login to AWS Console:
Go to the AWS Management Console.
Sign in or create an AWS account.
Navigate to EC2:
- In the AWS Management Console, go to the EC2 Dashboard.
Launch an Instance:
Click on "Instances" in the left navigation pane.
Click the "Launch Instance" button.
Choose an Amazon Machine Image (AMI):
- Select an AMI based on your requirements (e.g., Amazon Linux 2 AMI).
Choose an Instance Type:
- Select an instance type based on your needs.
Configure Instance:
- Configure the instance details, including the number of instances, network settings, etc.
Add Storage:
- Configure storage options based on your needs.
Add Tags:
- Add tags to your instance for better organization.
Configure Security Group:
- Configure security group rules to allow inbound traffic on port 22 (SSH) and any other ports your web application needs.
Review and Launch:
Review your settings and click "Launch."
Select an existing key pair or create a new one to connect to your instance.
Step 2: Connect to EC2 Instance
Get Public IP:
- Once the instance is running, note the public IP address.
SSH into the Instance:
Open a terminal on your local machine.
Use the following command to connect to your EC2 instance:
ssh -i "test_aws.pem" ubuntu@ec2-54-198-221-76.compute-1.amazonaws.com
Replace
/path/to/your/key.pem
with the path to your private key andyour-public-ip
with your EC2 instance's public IP address.
Step 3: Install Web Server and Deploy Web Application
Updating Package Manager: On the EC2 instance, update the package manager using the command
sudo apt update -y
Installing Apache Web Server: Install the Apache Web Server on the EC2 instance with the command
sudo apt install apache2 -y
Step 3: Deploy a Simple Web Application
Create a simple HTML file (e.g., index.html):
echo "<html><body><h1>Hello from your EC2 instance!</h1></body></html>" | sudo tee /var/www/html/index.html
Open a web browser and navigate to your EC2 instance's public IP address. You should see the simple web application.
Monitoring EC2 Instance with Amazon CloudWatch
Accessing AWS CloudWatch: Go to the AWS CloudWatch service in the AWS Management Console.
Creating an Alarm: Click on "Create alarm" and select the EC2 metric you want to monitor, e.g. CPU utilization
Configuring Conditions: Set up alarm conditions and notification settings as needed.
Creating the Alarm: Give your alarm a name and create the alarm. This will trigger alerts based on the conditions you've set.
Task-02
Create an Auto Scaling group using the AWS Management Console and configure it to launch EC2 instances in response to changes in demand.
Use Amazon CloudWatch to monitor the performance of the Auto Scaling group and the EC2 instances and troubleshoot any issues that arise.
Use the AWS CLI to view the state of the Auto Scaling group and the EC2 instances and verify that the correct number of instances are running.
Create a Launch Template: Begin by generating a 'Launch Template' from an EC2 instance.
Launch Template Successfully Created: Confirm that the launch template has been successfully created.
-
Commence Auto Scaling Group Creation: Initiate the process by clicking on the "Create Auto Scaling group" button.
-
Select Launch Template: Choose the launch template to be applied to the instances within the group.
Define Instance Type and Configuration: Specify the instance type and configure additional details, such as VPC, subnet, security groups, and storage.
Establish Scaling Policies: Set up scaling policies to govern when new instances should be launched. Configure desired, minimum, and maximum capacity for instances.
-
Choose Target Tracking Scaling Policy: Opt for the 'Target Tracking Scaling Policy' and select 'metric type' as CPU utilization, which will automatically create an alarm.
Finalize Auto Scaling Group Creation: Click on "Create Auto Scaling group" to complete the creation of the auto-scaling group.
-
Auto-Scaling Group Successfully Created: Confirm the successful creation of the auto-scaling group.
Use Amazon CloudWatch to monitor the performance of the Auto Scaling group and the EC2 instances and troubleshoot any issues that arise.
- Set Up Target Tracking Policy in Auto Scaling: Configure a target tracking policy in auto scaling that generates two distinct alarms with unique conditions.
Monitoring Activation for Auto-Scaling Group: Once the alarms are in place, the monitoring of the specified metric for the Auto Scaling group begins. If the metric surpasses the set threshold, the alarm triggers and executes the predefined action.
Enable Auto Scaling Group Metric Monitoring: Navigate to the auto-scaling group previously created, access the "Monitoring" tab, and activate "Auto Scaling Group Metric."
Create Alarm via CloudWatch Dashboard: Alternatively, you can craft an alarm through the CloudWatch dashboard.
Initiate Alarm Creation: Click on the "Create Alarm" button to initiate the alarm setup.
Select EC2 as the Target: Navigate to the "EC2" section and choose 'By Auto Scaling Group.'
Choose Metric for Monitoring: Pick the metric you wish to monitor, searching for it by name.
-
Metric Selection and Alarm Setup: Select the desired metric and proceed to create an alarm.
Monitoring and Triggering: Following alarm creation, continuous monitoring of the specified metric begins. If conditions are met, the configured action is triggered.
Manage and Update Alarms: Monitor the alarm status in the CloudWatch console. Manage and update the alarm settings as required to ensure optimal functionality.
Use the AWS CLI to view the state of the Auto Scaling group and the EC2 instances and verify that the correct number of instances are running.
AWS CLI Installation: Begin by installing the AWS CLI on your local machine using the following command:
sudo apt-get install awscli
AWS CLI Configuration: Configure the AWS CLI to establish the necessary connections and settings.
Check Auto Scaling Group State: Utilize the command below to inspect the current state of the Auto Scaling group and the instances it manages:
aws autoscaling describe-auto-scaling-groups
Auto Scaling Group Instance Overview: Confirm that the Auto Scaling group has launched the specified number of instances (in this case, 2) by checking the desired capacity setting:
aws ec2 describe-instances
Instance Verification: Validate that the correct number of instances are not only running but also in a healthy and operational state.
You can also do stress test to check for auto-scaling and to check that our trigger is functioning.
For that use commands:
sudo apt-get install stress
stress --cpu $(nproc) --timeout 3600s