AWS EC2 - part 2

Creating an EC2 instance on AWS involves several steps. Here is a detailed guide to help you through the process:

1. Log in to AWS Management Console:

- Go to [AWS Management Console](https://aws.amazon.com/console/).

- Log in with your AWS credentials.

2. Navigate to EC2 Dashboard:

- From the AWS Management Console, select "Services" at the top left.

- Under the "Compute" category, click on "EC2".

3. Launch Instance:

- On the EC2 Dashboard, click the "Launch Instance" button.

4. Choose an Amazon Machine Image (AMI):

- Select an AMI based on your needs. Amazon provides various AMIs with different operating systems such as Amazon Linux, Ubuntu, Windows, etc.

- Click “Select” next to your chosen AMI.

5. Choose an Instance Type:

- Choose an instance type based on the CPU, memory, and network performance you need.

- For beginners, the "t2.micro" type is a good option as it is part of the free tier.

- Click “Next: Configure Instance Details”.

6. Configure Instance Details:

- Configure settings like the number of instances, network settings, IAM roles, etc.

- For most basic uses, the default settings are sufficient.

- Click “Next: Add Storage”.

7. Add Storage:

- Specify the size and type of storage (EBS) for your instance.

- The default size is typically sufficient for basic use.

- Click “Next: Add Tags”.

8. Add Tags:

- (Optional) Add tags to your instance to help you manage and identify your resources.

- For example, you can add a tag with a key of “Name” and a value of “MyFirstInstance”.

- Click Next: Configure Security Group”.

9. Configure Security Group:

- A security group acts as a virtual firewall to control traffic to your instance.

- Create a new security group or select an existing one.

- Add rules to allow specific types of traffic (e.g., SSH for Linux, RDP for Windows).

- For SSH access, add a rule that allows TCP port 22 from your IP address.

- Click “Review and Launch”.

10. Review and Launch:

- Review your instance configuration.

- Click the “Launch” button.

11. Select or Create a Key Pair:

- You will be prompted to select an existing key pair or create a new one.

- If you create a new key pair, download the private key file (.pem) and keep it secure. You will need this key to access your instance.

- Acknowledge that you have access to the private key file and then click “Launch Instances”.

12. Access Your Instance:

- Once your instance is running, you can access it using SSH (for Linux) or RDP (for Windows).

- For SSH access to a Linux instance, use the following command:

```sh

ssh -i /path/to/your-key-pair.pem ec2-user@your-instance-public-dns

```

- Replace `/path/to/your-key-pair.pem` with the path to your key pair file and `your-instance-public-dns` with the public DNS name of your instance.

By following these steps, you should be able to successfully create and access an EC2 instance on AWS.

Amazon Machine Image

An Amazon Machine Image (AMI) is a template that contains the software configuration (operating system, application server, and applications) required to launch an instance in AWS. Here's a detailed explanation of AMIs in AWS:

Key Components of an AMI

1. Root Volume Template:

- Contains the operating system, application server, and applications.

- The root volume is typically an Amazon Elastic Block Store (EBS) volume or an instance store volume.

2. Launch Permissions:

- Controls which AWS accounts can use the AMI to launch instances.

3. Block Device Mapping:

- Specifies the volumes to attach to the instance when it is launched.

Types of AMIs

1. Amazon-provided AMIs:

- Provided by AWS, these include the most common operating systems like Amazon Linux, Ubuntu, Windows Server, etc.

2. Marketplace AMIs:

- Provided by third-party vendors and available in the AWS Marketplace.

- Often include additional software or configurations.

3. Community AMIs:

- Shared by other AWS users and may include custom configurations and software.

4. Custom AMIs:

- Created by users to meet specific needs. Users can customize an instance and then create an AMI from it to use as a template for launching new instances.

Creating an AMI

1. From an Existing Instance:

- You can create an AMI from an existing EC2 instance. This captures the configuration and data on that instance at a point in time.

- To create an AMI, go to the EC2 Dashboard, select the instance, and choose “Create Image” from the Actions menu.

2. From a Snapshot:

- You can also create an AMI from EBS snapshots. This is useful for creating backups or restoring data.

Benefits of Using AMIs

1. Scalability

- Quickly launch multiple instances with the same configuration, ensuring consistency across instances.

2. Flexibility:

- Customize AMIs to meet specific needs, including pre-installing software and configurations.

3. Efficiency:

- Save time by using pre-configured AMIs for common use cases.

4. Disaster Recovery:

- Use AMIs to back up configurations and data, facilitating quick recovery in case of failures.

Using an AMI

1. Selecting an AMI:

- During the instance launch process, you will be prompted to select an AMI. This determines the initial software state of the instance.

2. Launching Instances:

- Once an AMI is selected, you can proceed with configuring and launching instances.

Managing AMIs

1. Copying AMIs:

- You can copy an AMI to different regions to improve availability and disaster recovery.

2. Deregistering AMIs:

- If an AMI is no longer needed, you can deregister it to prevent further use. Note that deregistering an AMI does not delete the associated snapshots.

By understanding and effectively using AMIs, you can streamline the process of launching and managing EC2 instances in AWS, ensuring consistency, scalability, and reliability in your cloud infrastructure.

User Data

In AWS, user data is a feature that allows you to pass instructions to an EC2 instance at launch time. These instructions can be used to automate the configuration of your instance or to run scripts that install software, update packages, and perform other tasks.

What is User Data?

User data is a script that is executed automatically by the EC2 instance during the boot process. It is typically used for automation purposes, such as installing software or configuring the instance.

Types of User Data

1. Shell Scripts:

- These are the most common type of user data.

- They are executed as root and can perform any task that can be scripted in a shell environment.

- Example:

```bash

#!/bin/bash

yum update -y

yum install -y httpd

systemctl start httpd

systemctl enable httpd

```

2. Cloud-Init Directives:

- Cloud-init is a standard for initializing cloud instances with configurations.

- It supports different configuration formats like YAML.

- Example:

```yaml

#cloud-config

packages:

- httpd

runcmd:

- systemctl start httpd

- systemctl enable httpd

```

How to Add User Data

User data can be added when you launch an EC2 instance. Here are the steps:

1. Launch Instance:

- In the AWS Management Console, navigate to the EC2 Dashboard.

- Click on "Launch Instance".

2. Choose an AMI:

- Select the Amazon Machine Image (AMI) that you want to use.

3. Choose an Instance Type:

- Select the instance type based on your requirements.

4. Configure Instance Details:

- In the "Configure Instance Details" step, locate the "User Data" section.

- Enter your script or cloud-init directives in the "User Data" text box.

- Ensure the script starts with the appropriate shebang (`#!/bin/bash` for shell scripts, `#cloud-config` for cloud-init).

5. Add Storage:

- Configure the storage options as needed.

6. Add Tags:

- (Optional) Add tags to your instance for easier management and identification.

7. Configure Security Group:

- Set up the security group to control access to your instance.

8. Review and Launch:

- Review your settings and click "Launch".

Accessing and Viewing User Data

You can view and manage user data for an existing instance using the AWS Management Console, AWS CLI, or SDKs.

- AWS Management Console:

- Go to the EC2 Dashboard.

- Select the instance.

- Under "Actions", choose "Instance Settings" and then "View/Change User Data".

- AWS CLI:

- Use the following command to retrieve user data:

```sh

aws ec2 describe-instance-attribute --instance-id i-1234567890abcdef0 --attribute userData

```

Practical Use Cases for User Data

1. Automated Software Installation:

- Install and configure software packages automatically when the instance boots.

2. Configuration Management:

- Apply configuration settings or deploy configuration management tools like Ansible, Puppet, or Chef.

3.Startup Tasks:

- Run any startup tasks such as downloading data, connecting to databases, or starting services.

4. Dynamic Configuration:

- Use user data to retrieve and apply dynamic configurations based on instance metadata or other data sources.

Best Practices

1. Idempotency:

- Ensure that the user data script is idempotent, meaning it can be run multiple times without causing issues.

2. Security:

- Avoid including sensitive information (like passwords or secret keys) in user data scripts.

3. Logging:

- Implement logging within your scripts to help with debugging and monitoring.

4. Modularization:

- Keep scripts modular and well-organized for easier maintenance and updates.

By leveraging user data effectively, you can automate many aspects of instance setup and configuration, improving efficiency and consistency in your AWS environment.

How to connect to AWS EC2 machines

Using SSH (Linux/Unix) and RDP (Windows)

1. Ensure You Have the Key Pair

- Download the `.pem` file for the key pair created when launching the instance.

2. Set Permissions for Your Key Pair File:

```sh

chmod 400 /path/to/your-key-pair.pem

```

3. Obtain Public DNS or IP Address:

- Find the Public DNS (IPv4) or Public IP address from the EC2 Dashboard.

4. Connect Using SSH (for Linux/Unix):

```sh

ssh -i /path/to/your-key-pair.pem ec2-user@your-instance-public-dns

```

5. Connect Using RDP(for Windows):

- Use the decrypted password obtained through the AWS Management Console to connect via RDP client.

Using AWS Systems Manager (SSM)

1. Prerequisites:

- Ensure the instance has the SSM agent installed and running.

- Attach an IAM role to the instance with the necessary SSM permissions (e.g., `AmazonSSMManagedInstanceCore`).

2. Enable SSM Session Manager:

- Go to the EC2 Dashboard.

- Select the instance.

- Click on “Connect”.

- Choose “Session Manager” and click “Connect”.

This method does not require SSH access or key pairs, enhancing security by removing the need to open port 22.

Using EC2 Instance Connect

1. Prerequisites:

- Supported for Amazon Linux 2 or Ubuntu instances.

- Ensure the instance is in a public subnet and the security group allows SSH access.

2. Connect Using EC2 Instance Connect:

- Go to the EC2 Dashboard.

- Select the instance.

- Click on “Connect”.

- Choose “EC2 Instance Connect” and click “Connect”.

This method uses temporary SSH keys generated by AWS, avoiding the need to manage SSH keys.

Using a Bastion Host

1. Set Up a Bastion Host:

- Launch an EC2 instance in the public subnet configured as a bastion host.

- Configure its security group to allow SSH from your IP and outbound access to the private subnet.

2. Connect to the Bastion Host:

- SSH into the bastion host using its public IP:

```sh

ssh -i /path/to/your-key-pair.pem ec2-user@bastion-host-public-ip

```

3. Connect to Private Instances:

- From the bastion host, SSH into the private instance:

```sh

ssh -i /path/to/your-key-pair.pem ec2-user@private-instance-private-ip

```

Additional Tips

- Security Groups:

- Ensure security groups allow necessary inbound traffic (e.g., SSH on port 22, RDP on port 3389).

- Restrict access to specific IPs for better security.

- IAM Roles and Policies:

- For SSM, attach IAM roles with `AmazonSSMManagedInstanceCore`.

- Ensure the bastion host has the right policies to access other instances.

- Network Configuration:

- Verify instances are in the correct subnets and routing is set up properly.

By incorporating these advanced methods, you can enhance the security and flexibility of accessing your EC2 instances.

When connecting to EC2 instances, you may encounter various errors. Here are some common errors and their solutions:

### SSH Connection Errors (Linux/Unix)

1. Permission Denied (Public Key)

- Cause: Incorrect permissions on the key pair file or wrong username.

- Solution: Ensure the key pair file has correct permissions:

```sh

chmod 400 /path/to/your-key-pair.pem

```

Ensure you are using the correct username (`ec2-user` for Amazon Linux, `ubuntu` for Ubuntu, etc.).

2. Connection Timed Out

- Cause: Security group settings, instance not running, or network issues.

- Solution:

- Check that the instance is running.

- Verify the security group allows inbound traffic on port 22.

- Ensure your local firewall does not block outbound traffic on port 22.

- Confirm the instance is in a public subnet with a public IP address.

3. Unprotected Private Key File

- Cause: Key pair file has incorrect permissions.

- Solution:

```sh

chmod 400 /path/to/your-key-pair.pem

```

4. SSH Key Not Working

- Cause: Using the wrong key pair.

- Solution: Verify you are using the correct key pair associated with the instance.

RDP Connection Errors (Windows)

1. Incorrect Password

- Cause: Wrong password or user.

- Solution: Ensure you decrypted the password correctly in the AWS Management Console and use "Administrator" as the username.

2. Connection Timed Out

- Cause: Security group settings, instance not running, or network issues.

- Solution:

- Check that the instance is running.

- Verify the security group allows inbound traffic on port 3389.

- Ensure your local firewall does not block outbound traffic on port 3389.

- Confirm the instance is in a public subnet with a public IP address.

AWS Systems Manager (SSM) Errors

1. Instance Not Appearing in SSM Console

- Cause: SSM agent not installed/running or missing IAM role.

- Solution:

- Ensure the SSM agent is installed and running on the instance.

- Attach an IAM role to the instance with `AmazonSSMManagedInstanceCore` policy.

2. SSM Session Initialization Failure

- Cause: Network issues or SSM agent not able to reach SSM endpoints.

- Solution: Ensure the instance can reach SSM endpoints over the internet or through a VPC endpoint.

EC2 Instance Connect Errors

1. EC2 Instance Connect Not Supported

- Cause: Unsupported instance type or AMI.

- Solution: Use Amazon Linux 2 or Ubuntu AMIs that support EC2 Instance Connect.

2. Connection Timed Out

- Cause: Security group settings.

- Solution: Ensure the security group allows inbound SSH traffic.

Bastion Host Connection Errors

1. Cannot Connect to Bastion Host

- Cause: Incorrect security group settings or bastion host not running.

- Solution: Ensure the security group allows inbound SSH traffic from your IP and that the bastion host is running.

2. Cannot Connect from Bastion Host to Private Instance

- Cause: Incorrect security group settings or routing issues.

- Solution: Ensure the private instance’s security group allows SSH traffic from the bastion host and that proper routing is configured.

General Tips for Troubleshooting

1. Check Instance State:

- Ensure the instance is in a "running" state.

2. Verify Security Group Settings:

- Check inbound rules for the correct ports (22 for SSH, 3389 for RDP).

3. Check Network ACLs and Route Tables:

- Ensure the subnet has proper network ACLs and the route table directs traffic correctly.

4. Review System Logs:

- Use the EC2 instance's system logs for debugging (accessible via the AWS Management Console).

5. Reboot the Instance:

- Sometimes, a simple reboot can resolve connectivity issues.

By systematically addressing these common errors, you can effectively troubleshoot and resolve connectivity issues with your EC2 instances.

0
Subscribe to my newsletter

Read articles from Roopa Rani Mathew directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Roopa Rani Mathew
Roopa Rani Mathew