Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

EC2 Key Pairs vs AWS Credentials: Practical Fixes for SSH and Security Group Setup

This guide explains the two setup issues that most often block first-time EC2 deployments: mixing up AWS API credentials with EC2 SSH key pairs, and forgetting to allow SSH in security groups. If you are onboarding to Amazon EC2 or troubleshooting SSH-based automation, you will learn what each credential is for, how to configure access correctly, and how to verify it with practical commands.

Originally published on 2010-03-09; fully revised and updated.

Amazon EC2 key pairs and security group stumbling blocks

Untangling EC2 key pairs and AWS API credentials

In the original context, this confusion frequently affected users of Cloud Tools and Cloud Foundry. The core issue is still common: the key used to sign AWS API calls is not the same key used to SSH into an EC2 instance.

Credential or keyPrimary purposeWhere it is usedCommon confusion
EC2 key pair (key name plus private .pem)OS login to EC2 over SSHKey pair name in launch request; private key file in ssh -iMistaken for AWS API authentication credentials
Access key ID plus secret access keyProgrammatic AWS API authenticationAWS CLI, SDKs, automation toolsCannot be used directly for SSH login
X.509 certificate plus private keyLegacy AWS request-signing pathOlder tooling and historical workflowsLooks like an SSH key pair but serves a different purpose

In most modern environments, teams prefer short-lived credentials through IAM roles for API access. But for direct SSH, EC2 key pairs are still a standard pattern unless your organization uses an alternative such as Session Manager.

EC2 access workflow at a glance

flowchart TD
    A[Create EC2 key pair] --> B[Download private key file]
    B --> C[Set secure file permissions]
    C --> D[Allow SSH in security group]
    D --> E[Launch instance with key pair name]
    E --> F[Connect with ssh using private key]
    F --> G{Connected}
    G -- no --> H[Check key name security group username and network path]
    G -- yes --> I[Continue with provisioning]

Create an EC2 key pair and protect the private key

You can create key pairs from the AWS console or CLI. The critical part is that the private key must be stored securely, and access to that file must be restricted.

aws ec2 create-key-pair --key-name demo-key --query 'KeyMaterial' --output text > demo-key.pem
chmod 400 demo-key.pem
aws ec2 describe-key-pairs --key-names demo-keyCode language: JavaScript (javascript)
  • Open the EC2 console and go to Key Pairs.
  • Choose Create key pair.
  • Enter a key pair name, create it, and download the private key file immediately.

Historical AWS console screenshots from the original article: Creating a key pair and Saving a key pair.

Allow SSH in the correct security group

If port 22 is blocked, SSH-based tooling will fail even when the key pair is correct. Apply ingress rules to the security group attached to your instance, and prefer a narrow source CIDR instead of broad internet access.

aws ec2 authorize-security-group-ingress \
  --group-id sg-0123456789abcdef0 \
  --protocol tcp \
  --port 22 \
  --cidr 203.0.113.10/32

aws ec2 describe-security-groups --group-ids sg-0123456789abcdef0

Launch and connect to an instance

At launch time, specify the key pair name. At connection time, provide the matching private key file to SSH.

aws ec2 run-instances \
  --image-id ami-0123456789abcdef0 \
  --instance-type t3.micro \
  --key-name demo-key \
  --security-group-ids sg-0123456789abcdef0 \
  --subnet-id subnet-0123456789abcdef0

ssh -i demo-key.pem ec2-user@EC2_PUBLIC_DNSCode language: CSS (css)

If login fails, verify all four elements together: instance key pair name, private key file, security group rules, and OS username (for example, many images use ec2-user or ubuntu).

Choose an access pattern

Access methodBest forInbound port 22 neededEC2 key pair needed
Direct SSH to instanceSimple admin access and legacy toolingYesYes
AWS Systems Manager Session ManagerManaged shell access without open SSH portNoNo
SSH through bastion hostControlled access in private networksYesUsually yes

Common pitfalls and best practices

  • Do not confuse AWS API credentials with EC2 SSH key pairs; they solve different problems.
  • Download private key material once, then store it securely and set restrictive file permissions.
  • Avoid opening SSH to 0.0.0.0/0 unless there is a temporary, controlled need.
  • Confirm the instance is attached to the security group you edited.
  • Use reproducible commands or Infrastructure as Code instead of one-off manual fixes.
  • Where possible, prefer short-lived API credentials and centrally managed access patterns.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

AWS CLI Credentials Setup: Configure, Verify, and Secure Access

Learn how to configure AWS credentials with the AWS CLI, verify your identity, and understand where credentials are stored. This updated guide also compares profiles, environment variables, and role-based access.

Read More

Provision an AWS EC2 Instance with Chef (Legacy ChefDK Workflow)

A practical, step-by-step walkthrough for provisioning an AWS EC2 VM using the classic ChefDK and chef-provisioning approach. Includes workflow, commands, recipe examples, and validation checks for legacy environments.

Read More

EC2 Boot from EBS Explained: Architecture, Trade-offs, and an AWS CLI Workflow

Booting Amazon EC2 instances from Amazon EBS was a major shift from ephemeral root disks to persistent root volumes. This guide explains what changed, why it mattered, and how to use the model safely in modern operations.

Read More

Deploying Java EE on Amazon EC2 with EC2Deploy: Workflow, Script, and Modern Lessons

This revised guide breaks down the original EC2Deploy approach for provisioning EC2 instances, deploying Java EE apps, and running JMeter tests. It also maps the same deployment pattern to modern DevOps practices.

Read More

EC2Deploy and the Cloud Tools Maven Plugin: Architecture, Goals, and Deployment Workflow

This revised guide explains what the 2010 EC2Deploy and Cloud Tools release actually introduced, how its components fit together, and how the Maven goals map to deployment operations on EC2. It also covers what modern teams should verify before relying on this legacy toolchain.

Read More

Cloud Tools Adds Amazon EBS Support: Durable EC2 Storage for Database Workloads

This historical update covers when Cloud Tools introduced Amazon EBS support for EC2 applications, including database-backed deployments. It explains the supported storage modes, the backup/restore flow, and practical command-level equivalents.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x