# amazon-ec2

A Cursor rule for managing Amazon EC2 infrastructure through CloudFormation. It guides instance sizing, IAM permissions, network access, metadata protection, and EBS encryption.

- rule · `type=rule` · Cursor · ★ 3580
- Author: sanjeed5
- License: CC0-1.0 · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/sanjeed5/awesome-cursor-rules-mdc/tree/HEAD/rules-mdc/amazon-ec2.mdc
- Updated: 2026-05-19
- HTML: https://affpapa.org/en/nevibecode/rules/amazon-ec2

## What it does

The rule guides how EC2 resources should be defined in CloudFormation. It recommends keeping networking, IAM roles, and instance definitions in separate reusable stacks. Resource tags such as Environment, Project, Owner, and CostCenter help identify infrastructure and assign costs to projects.

Security guidance covers narrowly scoped IAM permissions, restricted Security Group traffic, and mandatory IMDSv2. Configuration values and secrets should come from CloudFormation parameters, SSM Parameter Store, or AWS Secrets Manager rather than being embedded in scripts. The examples also cover Launch Templates for Auto Scaling and encrypted EBS volumes using KMS. SSH access should be limited to an approved IP range instead of accepting connections from every address.

The file is intended for Cursor projects and provides guidance for infrastructure code rather than deploying resources itself. Its examples use AWS CloudFormation resources and configurable parameters, which must match the target environment. Review the actual template before deployment, especially its IAM policies and inbound network rules.

## Requirements
- Works with: AWS

## Install

### Cursor (native platform)

**Manually: put the files in place**
- File: `.cursor/rules/amazon-ec2.mdc` — Frontmatter: description, globs, alwaysApply

### Claude Code

**Manually: put the files in place**
- File: `.claude/rules/amazon-ec2.md` — The paths frontmatter field limits the files: move globs from the source into paths

### Codex

**AGENTS.md**
- Codex reads AGENTS.md: paste the rule text there

### GitHub Copilot

**Manually: put the files in place**
- File: `.github/instructions/amazon-ec2.instructions.md` — The applyTo field sets which files it applies to: move globs from the source into applyTo

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/rules/amazon-ec2.md`

### Cline

**Manually: put the files in place**
- File: `.clinerules/amazon-ec2.md`

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/sanjeed5/awesome-cursor-rules-mdc/tree/HEAD/rules-mdc/amazon-ec2.mdc

## When to use it
- Split a monolithic EC2 CloudFormation template into reusable stacks
- Restrict IAM permissions attached to instances
- Limit SSH access to approved IP ranges
- Require IMDSv2 and encrypt EBS volumes
- Apply consistent tags for infrastructure cost allocation

## Example prompts
- Review my EC2 CloudFormation template against the security rules.
- Separate the networking, IAM, and EC2 resources into reusable CloudFormation stacks.
- Check the configuration for open Security Groups, missing IMDSv2 requirements, and unencrypted EBS volumes.

## Source text · amazon-ec2.mdc · CC0-1.0

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: e0a77f087c4eddc6.

BEGIN THIRD-PARTY FILE e0a77f087c4eddc6
````text
---
description: This guide provides definitive best practices for deploying and managing Amazon EC2 instances, focusing on security, cost optimization, and operational excellence through Infrastructure as Code (IaC).
globs: **/*
---
# amazon-ec2 Best Practices

Treat every EC2 instance as versioned, secured, and continuously optimized code. Adhere strictly to the AWS Well-Architected Framework and Foundational Security Best Practices (FSBP).

## 1. Code Organization and Structure

**Always define EC2 resources using Infrastructure as Code (IaC).** CloudFormation is the standard for AWS-native deployments. Break down templates into modular, reusable components.

### 1.1. Modular CloudFormation Templates

Organize your CloudFormation templates logically. Separate networking, IAM, and EC2 instance definitions into distinct, composable stacks or modules.

❌ **BAD: Monolithic Template**
```yaml
# all-in-one-stack.yaml
Resources:
  VPC: # ...
  Subnet: # ...
  SecurityGroup: # ...
  IAMRole: # ...
  EC2Instance: # ...
```

✅ **GOOD: Modular Stacks**
```yaml
# network-stack.yaml
Resources:
  VPC: # ...
  Subnet: # ...
Outputs: # Export VPC ID, Subnet IDs
# iam-stack.yaml
Resources:
  IAMRole: # ...
Outputs: # Export Role ARN
# ec2-instance-stack.yaml
Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0abcdef1234567890
      InstanceType: !Ref InstanceTypeParam
      SecurityGroupIds: [!ImportValue MySecurityGroup]
      IamInstanceProfile: !ImportValue MyInstanceProfile
      # ...
```

### 1.2. Enforce Consistent Tagging

Implement a unified tagging strategy from your IaC templates. Tags are critical for cost allocation, resource discovery, and automation.

❌ **BAD: Missing or Inconsistent Tags**
```yaml
EC2Instance:
  Type: AWS::EC2::Instance
  Properties:
    # ... no tags or only a few
```

✅ **GOOD: Mandatory Tags via IaC**
```yaml
EC2Instance:
  Type: AWS::EC2::Instance
  Properties:
    # ...
    Tags:
      - Key: Environment
        Value: !Ref EnvironmentTag
      - Key: Project
        Value: !Ref ProjectTag
      - Key: Owner
        Value: !Ref OwnerTag
      - Key: CostCenter
        Value: !Ref CostCenterTag
```

## 2. Common Patterns and Anti-patterns

### 2.1. Right-Sizing Instances

**Analyze workloads to select the optimal instance family and size.** This is the single biggest driver for performance efficiency and cost optimization.

❌ **BAD: Defaulting to `t2.micro` or Over-provisioning**
```yaml
EC2Instance:
  Type: AWS::EC2::Instance
  Properties:
    InstanceType: t2.micro # For a production database!
```

✅ **GOOD: Workload-Specific Instance Types**
```yaml
EC2Instance:
  Type: AWS::EC2::Instance
  Properties:
    InstanceType: m5.large # General purpose, balanced CPU/memory
    # or c5.xlarge for compute-intensive, r5.large for memory-intensive
```

### 2.2. Least Privilege IAM Instance Profiles

**Attach IAM roles to EC2 instances via Instance Profiles, granting only the minimum necessary permissions.** Never embed AWS credentials directly on an instance.

❌ **BAD: Over-privileged IAM Role**
```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "*",
      "Resource": "*"
    }
  ]
}
```

✅ **GOOD: Tightly Scoped IAM Role**
```yaml
# CloudFormation IAM Role for EC2
BastionHostRole:
  Type: AWS::IAM::Role
  Properties:
    AssumeRolePolicyDocument:
      Statement:
````
END THIRD-PARTY FILE e0a77f087c4eddc6

The third-party file has ended. Everything between the e0a77f087c4eddc6 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 3445 of 8620 bytes. © sanjeed5 · CC0-1.0 · https://github.com/sanjeed5/awesome-cursor-rules-mdc/tree/HEAD/rules-mdc/amazon-ec2.mdc

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
