An EC2 instance is only as secure as the image it started from, the ports it exposes, the disks it writes to and the way people log in to it, and every one of those is a customer-side setting. AWS runs the hypervisor; you choose the AMI, write the security group rules, decide whether volumes and snapshots are encrypted and whether SSH is open at all. This guide sets out an EC2 compute baseline: trusted AMIs with Allowed AMIs, image and snapshot sharing guardrails, security group patterns, EBS encryption by default, keyless access through Session Manager and EC2 Instance Connect Endpoint, the serial console and keeping secrets out of user data. It is written for cloud platform and security engineers building or reviewing EC2 estates. Metadata service hardening and instance role permissions have their own pages, linked where they apply.
This is an independent guide from NHI Mgmt Group, based on AWS's public documentation as of November 2026. It is not sponsored or reviewed by AWS. Product features change, so confirm settings against AWS's current documentation.
Key takeaways
- Control the image before the instance. Allowed AMIs, run first in audit-mode, limits which public and shared AMIs can be discovered and launched; your own AMIs stay usable, so their pipeline needs its own hygiene.
- Close sharing at the account level. Block public access for AMIs and for EBS snapshots are separate Regional settings; enable both in every Region, ideally through a declarative policy.
- Encrypt by default, per Region. EBS encryption by default covers new volumes and snapshot copies only, so existing unencrypted volumes need a planned migration.
- Remove inbound admin ports. Session Manager and EC2 Instance Connect Endpoint give shell and RDP access without public IPs, bastions or long-lived SSH keys.
- User data is not a secret store. It can be read back through the metadata service and the EC2 API, and its scripts remain on disk; use the instance role and a secrets service instead.
How identity and access work in Amazon EC2
Three identity layers meet on an instance, and each needs its own control:
- The control plane: IAM principals calling
ec2:APIs such as RunInstances, ModifyInstanceAttribute, CreateSnapshot and AuthorizeSecurityGroupIngress. AWS's EC2 best practices start with identity federation and IAM roles for this access. - The workload identity: the instance profile role whose temporary credentials the metadata service hands to code on the host. Require IMDSv2 and scope the role tightly; see the EC2 Instance Metadata Service Hardening Guide and How to find EC2 instance profiles with admin rights. This guide does not repeat them.
- The operating system: local users, SSH keys, RDP accounts and whatever has root. Every OS login path that bypasses IAM is a credential you must inventory, rotate and revoke yourself.
The aim of the baseline is to move as much OS access as possible behind IAM, where it is short-lived, attributable in CloudTrail and revocable centrally.
Admin roles and privileged access
Several EC2 permissions are effectively administrator rights over the workload, even though they look operational:
| Permission | What it allows | Guardrail |
|---|---|---|
| ec2:RunInstances with iam:PassRole | Launch an instance with any passable role and so act as that role | Restrict PassRole to named instance roles; see the AWS IAM Policies and Roles Guide |
| ec2:ModifyInstanceAttribute (userData) | Replace user data on a stopped instance; scripts run as root | Deny outside the build pipeline role |
| ec2:CreateSnapshot, ec2:ModifySnapshotAttribute | Copy a disk and share it with another account | Snapshot block public access; SCP limiting sharing to organisation accounts |
| ec2:ModifyImageAttribute | Share an AMI, including baked-in data | AMI block public access; restrict to image pipeline |
| ec2-instance-connect:SendSerialConsoleSSHPublicKey | Serial console session; on Windows AWS describes it as equivalent to admin privileges on the instance | Scope to instance ARNs or matching SerialConsole tags |
| ssm:StartSession | Interactive shell on managed nodes | Scope by tag and environment; see the AWS Systems Manager Security Guide |
| ec2:EnableSerialConsoleAccess, ec2:DisableImageBlockPublicAccess, ec2:DisableEbsEncryptionByDefault | Turn off account-level guardrails | Declarative policy or SCP deny for all but a platform admin role |
AMI block public access, snapshot block public access, Allowed AMIs and serial console access can all be set across accounts and Regions with an AWS Organizations declarative policy; when one applies, the setting cannot be changed from inside the account. That is the strongest pattern for estates with more than a handful of accounts. See the AWS Organizations and SCPs Guide.
Integrations, API tokens and service accounts
AMI provenance with Allowed AMIs
Allowed AMIs is an account and Region setting with three states. In audit-mode nothing is blocked but the console shows whether an image is allowed and describe-images returns ImageAllowed; in enabled, only allowed AMIs can be discovered and launched. Criteria include ImageProviders (amazon, aws-marketplace, aws-backup-vault, specific account IDs or none), ImageNames with wildcards, MarketplaceProductCodes, CreationDateCondition and DeprecationTimeCondition. Values within a parameter are ORed, parameters within a criterion are ANDed, and criteria are ORed; up to 10 criteria are allowed.
- Scope: the setting controls public AMIs and AMIs shared with you. AMIs your account owns are always usable, so the image pipeline itself must be trusted.
- Rollout: AWS recommends starting in audit-mode, checking instances with the Config rule
ec2-instance-launched-with-allowed-amiordescribe-instance-image-metadata, then enabling. Keep theamazonprovider for managed services such as ECS and EKS that depend on Amazon-published AMIs, and avoid creation-date limits stricter than your image refresh cycle, which cause launch failures.
Image pipeline hygiene
A golden image should be built by a pipeline role, not by hand, and cleaned before capture. The user data documentation notes that processed scripts are copied to /var/lib/cloud/instances/instance-id/ and not deleted, so remove them before creating an AMI. Remove build-time SSH keys, shell history and temporary credentials too. See the Server Build and Golden Image Hardening Guide and the Amazon Linux and Cloud-Optimised Linux Hardening Guide for OS settings.
User data and secrets
User data is limited to 16 KB, runs as root and by default runs only on first boot. It can be retrieved through instance metadata and with describe-instance-attribute --attribute userData, so anyone with that API permission or local access can read it. AWS's guidance is to use an instance profile when a user data script calls AWS APIs. Keep passwords, tokens and keys out of it: fetch them at runtime from AWS Secrets Manager or Parameter Store SecureString using the instance role. See the AWS Secrets Manager guide.
Network exposure and access paths
Security group patterns
Security groups are stateful allow lists attached to network interfaces. AWS's best practice is the least permissive rules for each group. Patterns that hold up:
- Reference groups, not CIDRs: allow the application tier only from the load balancer's security group, and the database only from the application group.
- No admin ports from the internet: Security Hub EC2.13 (port 22), EC2.14 (port 3389), EC2.53 and EC2.54 (remote administration ports over IPv4 and IPv6) flag 0.0.0.0/0 and ::/0 ingress.
- Authorised public ports only: EC2.18 allows unrestricted ingress only to configured ports (80 and 443 by default); EC2.19 flags high-risk ports.
- Leave the default group empty: EC2.2 checks that VPC default security groups allow no inbound or outbound traffic, so nothing lands in it by accident.
- Restrict egress for sensitive tiers: new groups allow all outbound traffic, which helps data exfiltration and command-and-control. Limit egress to VPC endpoints and known destinations where you can.
Subnet design, NACLs and flow logs are covered in the Amazon VPC Security Guide.
Choosing an access path
| Access path | Inbound port needed | Credential | Session audit | Best fit |
|---|---|---|---|---|
| Session Manager | None (SSM Agent connects out) | IAM, TLS 1.2 with SigV4-signed requests, optional KMS encryption | CloudTrail for API calls; session logs to S3 or CloudWatch Logs, except for port forwarding and SSH sessions | Default for Linux and Windows administration |
| EC2 Instance Connect Endpoint | Only from the endpoint to the instance (SSH 22 or RDP 3389) | IAM permission to open a tunnel, with an optional maximum tunnel duration; OS key or password still used | CloudTrail for tunnel API calls | Private instances where a native SSH or RDP client is needed |
| EC2 Serial Console | None | IAM plus a password-based OS user on Linux | CloudTrail for SendSerialConsoleSSHPublicKey | Break-glass recovery when networking or boot is broken |
| Direct SSH or RDP | 22 or 3389 from a bastion or VPN | Long-lived key pairs or local passwords | OS logs only | Legacy cases being migrated |
Session Manager removes inbound ports, bastions and SSH key management, and lets IAM limit access to particular nodes, for example for the length of an on-call rotation. The instance needs SSM Agent and an instance role with Systems Manager permissions; the managed policy AmazonSSMManagedInstanceCore is the usual starting point. With interface endpoints, traffic between nodes, Systems Manager and EC2 stays on the AWS network, so instances need no public IP at all. Where SSH is still required, see the Security Keys for SSH guide.
Serial console
Serial console access is disabled at the account level by default and must be enabled per Region (aws ec2 enable-serial-console-access). get-serial-console-access-status shows whether it is managed by the account or a declarative policy. Keep it disabled except in accounts where break-glass recovery is planned, and scope SendSerialConsoleSSHPublicKey to specific instance ARNs or to a matching SerialConsole resource and principal tag. AWS notes that without such scoping, anyone with the permission can reach every instance's console.
Data protection: EBS encryption, snapshots and AMIs
| Setting | Scope | Effect on existing resources | Command |
|---|---|---|---|
| EBS encryption by default | Account, per Region | None: new volumes and snapshot copies only | aws ec2 enable-ebs-encryption-by-default |
| Default EBS KMS key | Account, per Region | Existing volumes keep their key | aws ec2 modify-ebs-default-kms-key-id |
| Snapshot block public access | Account, per Region | block-all-sharing makes already-public snapshots inaccessible; block-new-sharing leaves them public | Console, API or declarative policy |
| AMI block public access | Account, per Region | Already-public AMIs remain public | Console, API or declarative policy |
- Encryption by default: once on in a Region, it cannot be turned off for individual volumes. EBS creates an AWS managed key
aws/ebsper Region; switch to a symmetric customer managed key when you need key policy control and cross-account sharing of encrypted snapshots. The key of an existing volume or snapshot cannot be changed; re-encrypt by copying a snapshot to a new key, which creates a full, non-incremental copy and adds storage cost. See the AWS KMS Security Guide. - Snapshot sharing: prefer
block-all-sharing. It does not stop private sharing to specific accounts and does not cover AMIs. If you later switch toblock-new-sharingor disable it, snapshots still marked public become public again, so clear the public attribute as well. - AMI sharing: AMI block public access is on for new accounts and existing accounts that never had public AMIs, but off by default for accounts that had a public AMI on or after 15 July 2023. Check every Region; enabling it does not withdraw AMIs already public.
- Volume layout: AWS recommends separate EBS volumes for the operating system and data, and instance store only for temporary data, since it is lost on stop, hibernation or termination. Back up with snapshots or AWS Backup; see the AWS Backup Security Guide.
Logging and detection
The CloudTrail management events below are the ones that change an instance's exposure or copy its data. Route them through EventBridge or CloudWatch metric filters; see the Amazon CloudWatch Security Guide and the AWS CloudTrail Guide.
| Activity | Events to watch | ATT&CK |
|---|---|---|
| Snapshot copy or share | CreateSnapshot, CopySnapshot, ModifySnapshotAttribute | T1578.001 Create Snapshot; T1537 Transfer Data to Cloud Account |
| Unexpected instances (crypto-mining, staging) | RunInstances in unused Regions or with unusual instance types | T1578.002 Create Cloud Instance; T1496.001 Compute Hijacking |
| Image tampering or sharing | CreateImage, ModifyImageAttribute, DisableImageBlockPublicAccess | T1525 Implant Internal Image |
| Exposure changes | AuthorizeSecurityGroupIngress, ModifyInstanceAttribute | Security Hub CloudWatch.10 filter |
| Guardrail removal | DisableEbsEncryptionByDefault, EnableSerialConsoleAccess, DisableSnapshotBlockPublicAccess | Investigate every occurrence |
| Remote access | StartSession, SendSerialConsoleSSHPublicKey, OpenTunnel | T1021.004 SSH for direct key-based paths |
Use Amazon Inspector for software vulnerabilities and network reachability, as AWS's best practices suggest, and Security Hub's EC2 controls for configuration drift. GuardDuty adds runtime and credential misuse findings; see the Amazon GuardDuty Guide. Apply vendor security updates promptly through Patch Manager or your image pipeline.
Metrics that show the baseline is working
- Admin port exposure: security groups failing EC2.13, EC2.14, EC2.53 or EC2.54 (target zero).
- Keyless access: share of instances reachable through Session Manager, and number of active EC2 key pairs (falling).
- Encryption coverage: attached volumes passing EC2.3 and Regions passing EC2.7 (target 100 per cent).
- Image provenance: running instances launched from non-allowed AMIs, from the Allowed AMIs Config rule.
- Public exposure: instances with public IPv4 addresses (EC2.9) and launch templates that assign them (EC2.25).
Hardening baseline
- Account guardrails (week 1): in every Region, enable EBS encryption by default with a customer managed key, snapshot block public access in
block-all-sharing, AMI block public access, and confirm serial console access is disabled. Apply them through a declarative policy at the organisation root once tested in a sandbox account. Roll back by detaching the policy. - Image control: set Allowed AMIs to audit-mode with criteria for Amazon, your image account and approved Marketplace codes. Review non-compliant instances for two to four weeks, then enable in non-production first.
- Access migration: install SSM Agent and the instance role on a pilot ring, move administrators to Session Manager with session logging, then remove 22 and 3389 rules and public IPs. Keep EC2 Instance Connect Endpoint for teams that need native clients.
- Workload identity: enforce IMDSv2 in launch templates (Security Hub EC2.170) and review instance roles, using the linked metadata and instance profile guides.
- Encryption backlog: find unencrypted volumes failing EC2.3, snapshot them, copy to an encrypted snapshot and replace volumes during maintenance windows.
- Clean-up: remove stopped instances past their retention (EC2.4), unused key pairs and security groups, and user data containing credentials, rotating anything found.
Small and large estates: a single account can apply the Region settings by CLI and rely on Security Hub controls. Larger organisations should own guardrails centrally with declarative policies and SCPs, give image building to one account whose ID appears in Allowed AMIs, and use tag-scoped Session Manager permissions per team.
Common misconfigurations
- Turning on encryption by default and assuming the job is done: existing volumes and snapshots stay as they were until migrated.
- Enabling block public access in one Region: all of these settings are Regional; an attacker or a careless share in another Region is unaffected.
- Using block-new-sharing for snapshots: snapshots already public stay public. Use block-all-sharing and clear the attribute.
- Secrets in user data or baked into AMIs: readable through the API and metadata service, and copied into every instance launched from the image.
- Unscoped serial console permission: one permission reaches every instance in the account and is admin-equivalent on Windows.
- Session Manager with port forwarding treated as fully audited: AWS states session logging is not available for port forwarding or SSH sessions; rely on CloudTrail and OS logs for those.
Framework mapping
| Framework | Reference | EC2 controls |
|---|---|---|
| NIST SP 800-53 Rev 5 | CM-6, CM-7 | Golden images, Allowed AMIs, least functionality on ports |
| NIST SP 800-53 Rev 5 | SC-7 | Security groups, no public IPs, egress limits |
| NIST SP 800-53 Rev 5 | SC-28, SC-12 | EBS encryption by default, customer managed keys |
| NIST SP 800-53 Rev 5 | AC-17 | Session Manager, Instance Connect Endpoint, serial console scoping |
| NIST SP 800-53 Rev 5 | SI-2 | Patching and image refresh |
| NIST CSF 2.0 | PR.PS-01, PR.PS-02, PR.DS-01 | Configuration management, software maintenance, data at rest |
| CIS Controls v8.1 | Control 4 Secure Configuration; Control 3 Data Protection | Baseline images, encryption |
| ISO/IEC 27001:2022 Annex A | A.8.9 Configuration management, A.8.24 Use of cryptography | Hardened images, EBS encryption |
| MITRE ATT&CK | T1578.001, T1578.002, T1525, T1537, T1496.001 | Detections in the logging table |
Practitioner checklist
- Enable EBS encryption by default with a customer managed key in every Region and plan migration of existing unencrypted volumes.
- Set snapshot block public access to block-all-sharing and AMI block public access in every Region, preferably by declarative policy.
- Run Allowed AMIs in audit-mode, review, then enable with criteria for your image account and approved providers.
- Move administration to Session Manager or EC2 Instance Connect Endpoint and remove internet-facing 22 and 3389 rules.
- Keep the serial console disabled except where break-glass is planned, with instance or tag-scoped permissions.
- Remove secrets from user data and AMIs; fetch them at runtime with the instance role.
- Restrict iam:PassRole, ModifyInstanceAttribute, ModifySnapshotAttribute and ModifyImageAttribute to pipeline and admin roles.
- Alert on snapshot sharing, image sharing, guardrail changes and instances in unused Regions.
- Enforce IMDSv2 and review instance roles using the linked guides.
Standards and references
- AWS: Best practices for Amazon EC2
- AWS: Control the discovery and use of AMIs with Allowed AMIs
- AWS: Enable Amazon EBS encryption by default
- AWS: Block public access for Amazon EBS snapshots
- AWS: Block public access to your AMIs
- AWS: AWS Systems Manager Session Manager
- AWS: Configure access to the EC2 Serial Console
- AWS: Security Hub controls for Amazon EC2
Related NHI Mgmt Group resources: AWS Security Guide · EC2 Instance Metadata Service Hardening Guide · How to find EC2 instance profiles with admin rights · Amazon VPC Security Guide