Securing Amazon EC2 instances

Abhishek Shukla
7 min readJan 11, 2023

--

Security is always a shared responibility between cloud provider and customer. According to the shared responibility model of AWS, there are two types of security shared betwwen AWS and AWS customer

Shared responisbility model of AWS

Security of the Cloud : Security of cloud means that your cloud provider aka AWS is responsible for protecting the infrastructure that runs AWS services in the AWS Cloud. AWS also provides you with services that you can use securely.

Security in the cloud — Security in the cloud means that customer will be responible for properly configure and connect and use of the various security feature provided by the cloud provider aka AWS.

As a customer your responsibility includes the following areas.

Network access : Controlling network access to your instances, for example, through configuring your VPC and security groups. and firewalls.

Credential Management : Managing the credentials used to connect to your instances like login id and paswword.

Update Management : Managing the guest operating system and software deployed to the guest operating system, including updates and security patches.

Identity and Access control : Configuring the IAM roles that are attached to the instance and the permissions associated with those roles.

In this article we will discuss the how AWS shared responsibility model can be helpfull to secure the Amazon EC2 instances.

What is Amazon EC2

Amazon Elastic Compute Cloud (Amazon EC2) provides on-demand, scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 reduces hardware costs so you can develop and deploy applications faster.

You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage.

You can add capacity (scale up) to handle compute-heavy tasks, such as monthly or yearly processes, or spikes in website traffic. When usage decreases, you can reduce capacity (scale down) again.

The below diagram shows a basic architecture of an Amazon EC2 instance deployed within an Amazon Virtual Private Cloud (VPC).

Image Ref : https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html

The EC2 instance is secured with a security group, which is a virtual firewall that controls incoming and outgoing traffic. A private key is stored on the local computer and a public key is stored on the instance. Both keys are specified as a key pair to prove the identity of the user.

What are the ways to Secure Amazon EC2 instances:

Following are some common best practices that you can follow to secure your Amazon EC2 instances. In AWS there are services like IAM, Gaurd duty, Cloud watch etc that can be used to secure AWS resources including EC2.

Use Strong Authentication:

  • Create strong and unique passwords for your EC2 instances.
  • Consider using key pairs for SSH authentication instead of passwords.

Apply Regular Security Updates:

  • Keep your operating system and software up to date with the latest security patches.
  • Enable automatic updates or implement a regular update schedule.

Configure Security Groups:

  • Use security groups to control inbound and outbound traffic to your EC2 instances.
  • Limit access to only necessary ports and protocols.
  • Regularly review and update your security group rules.

Enable Network Access Control Lists (ACLs):

  • Network ACLs act as a firewall for your subnet and provide an additional layer of security.
  • Restrict traffic by specifying allowed IP addresses, protocols, and ports.

Implement Multi-Factor Authentication (MFA):

  • Enable MFA for privileged accounts, such as the root user and administrative roles.
  • MFA adds an extra layer of security by requiring an additional verification step during login.

Use Private Subnets and Virtual Private Cloud (VPC):

  • Place your EC2 instances within a private subnet to limit their exposure to the internet.
  • Utilize VPCs to isolate and segment your network resources logically.

Encrypt Data:

  • Enable encryption at rest for your EBS volumes using AWS Key Management Service (KMS) or other encryption tools.
  • Consider implementing encryption in transit using SSL/TLS certificates for secure communication.

Monitor and Audit:

  • Implement monitoring tools, such as Amazon CloudWatch, to track and analyze the behavior of your EC2 instances.
  • Set up logging and regularly review logs for any suspicious activities or unauthorized access attempts.

Implement Intrusion Detection and Prevention Systems (IDS/IPS):

  • Deploy IDS/IPS solutions to detect and block potential attacks.
  • Utilize security services provided by AWS, such as Amazon GuardDuty, to monitor your instances for malicious activity.

Regularly Back up Your Data:

  • Create regular backups of your important data to prevent data loss.
  • Consider using Amazon EBS snapshots or other backup mechanisms to protect your data.

Follow the Principle of Least Privilege:

  • Grant permissions to users and roles based on the principle of least privilege.
  • Regularly review and audit user permissions to ensure they have only the necessary access.

Implement Security Automation:

  • Use AWS Identity and Access Management (IAM) to automate security tasks and enforce security policies.
  • Leverage AWS Config and AWS Security Hub to automate security compliance checks and receive notifications about potential security issues.

Enable Security Logging and Monitoring:

  • Enable detailed logging for your EC2 instances, including operating system logs, application logs, and access logs.
  • Utilize AWS CloudTrail to monitor API activity and track changes to your EC2 instances.
  • Configure Amazon CloudWatch Logs to aggregate and analyze your log data.
  1. Use Amazon GuardDuty:
  • Amazon GuardDuty is a threat detection service that uses machine learning and anomaly detection to identify potential security threats.
  • Enable GuardDuty to monitor your EC2 instances for malicious activity, such as unauthorized access attempts or compromised instances.

Implement a Web Application Firewall (WAF):

  • Use AWS WAF or a third-party WAF solution to protect your web applications running on EC2 instances.
  • Configure rules to block common web application attacks, such as SQL injection or cross-site scripting (XSS).

Secure Remote Access:

  • Restrict remote access to your EC2 instances by allowing only specific IP addresses or ranges.
  • Consider using a bastion host or a Virtual Private Network (VPN) to securely access your instances.

Harden the Operating System:

  • Follow security best practices for the operating system running on your EC2 instances.
  • Disable unnecessary services and ports, remove default users and accounts, and apply strict file and directory permissions.

Implement File Integrity Monitoring (FIM):

  • Use FIM tools to monitor critical system files and directories for unauthorized changes.
  • Detect any modifications or tampering attempts on the file system.

Utilize AWS Secrets Manager:

  • Use AWS Secrets Manager to securely store and manage sensitive information, such as database passwords or API keys.
  • Avoid hard-coding credentials within your application or EC2 instance configuration.

Implement Distributed Denial of Service (DDoS) Protection:

  • Use AWS Shield or third-party DDoS protection services to mitigate and protect against DDoS attacks.
  • Configure DDoS protection at the load balancer or AWS Shield level to filter out malicious traffic.

Regularly Perform Security Assessments and Penetration Testing:

  • Conduct periodic security assessments and penetration tests to identify vulnerabilities and weaknesses in your EC2 instances.
  • Use tools like Amazon Inspector or engage with third-party security experts to perform comprehensive assessments.

Implement Security Automation and Orchestration:

  • Use AWS Security Hub to aggregate security findings across multiple accounts and services.
  • Leverage AWS Systems Manager Automation to automate security tasks, such as patch management or configuration updates.

Follow AWS Well-Architected Framework:

  • Adhere to the AWS Well-Architected Framework, which provides best practices for designing and operating secure, high-performing, resilient, and efficient infrastructure on AWS.

It’s important to note that the specific security measures you implement may vary based on your specific use case and requirements. Consider consulting AWS documentation, security best practices, and seeking professional advice to ensure you have a comprehensive security strategy in place for your Amazon EC2 instances.

Implement Host-based Firewalls:

  • Configure host-based firewalls on your EC2 instances to control inbound and outbound traffic at the instance level.
  • For Linux instances, consider using iptables or firewalld. For Windows instances, use the built-in Windows Firewall.

Enable Instance Isolation:

  • Use different security groups or subnets to isolate your EC2 instances based on their roles or sensitivity levels.
  • Separate instances that handle sensitive data or critical functions from those that serve public-facing applications.

Utilize AWS Secrets Manager for Database Credentials:

  • Store and manage database credentials securely using AWS Secrets Manager.
  • Retrieve credentials programmatically when needed, rather than storing them directly in your application code or configuration files.

Implement Security Hardening Frameworks:

  • Follow security hardening frameworks, such as the Center for Internet Security (CIS) benchmarks, to apply industry-recognized security configurations to your EC2 instances.

Implement Data Loss Prevention (DLP) Measures:

  • Implement DLP mechanisms to prevent accidental or unauthorized data exfiltration.
  • Use tools like AWS Data Loss Prevention (DLP) or third-party solutions to monitor and control the movement of sensitive data.

Implement Instance Metadata Protection:

  • Enable instance metadata protection to prevent unauthorized access to sensitive metadata information.
  • Use IAM roles to grant appropriate access permissions to EC2 instances instead of relying on instance metadata.

Implement Endpoint Protection:

  • Install and configure endpoint protection software, such as anti-malware or antivirus solutions, on your EC2 instances.
  • Regularly update the software and schedule regular scans to detect and remove any malicious software.

Enable CloudTrail Logging:

  • Enable AWS CloudTrail to log API calls and activities across your AWS account, including EC2 instances.
  • Store CloudTrail logs in a secure S3 bucket and regularly review them for any suspicious activity or unauthorized actions.

Implement Security Group Flow Logs:

  • Enable VPC Flow Logs or use third-party tools to capture and analyze network traffic in your VPC.
  • Monitor and analyze flow logs to identify any unusual or suspicious network patterns.

Implement Just-in-Time (JIT) Access:

  • Use AWS Systems Manager or third-party solutions to implement JIT access for your EC2 instances.
  • Limit the duration and scope of access to instances by granting temporary and on-demand access when needed.

Summary

Remember that security is a continuous process, and it’s essential to stay vigilant, keep up with security best practices, and regularly review and update your security measures to adapt to evolving threats.

--

--

Abhishek Shukla
Abhishek Shukla

Written by Abhishek Shukla

Having 20+ years of IT experience , Enterprise architect . Azure Solution architect expert, Microsft certified trainer, AWS cloud practioner (CLF-C01)