Launching and connecting to instances

Beginner · 20 min

Launch an EC2 instance from the CLI, understand stop versus terminate, and get a shell with Session Manager or EC2 Instance Connect instead of open SSH.

Before thisEC2 instance types and AMIsSecurity groups and network ACLsWorkload identity for EC2, Lambda and CI

Digital Cloud Training · 20 min

Launching and connecting to instances

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Digital Cloud TrainingReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A launch combines an AMI, an instance type, a subnet, security groups, an IAM instance profile, storage and optional user data.
  • Stop keeps the EBS volumes and ends compute charges. Terminate destroys the instance. Instance store data does not survive either.
  • Session Manager gives a shell with no inbound port, no SSH key and no public IP, controlled by IAM. The instance needs the SSM Agent, permissions and a path to Systems Manager.
  • EC2 Instance Connect pushes a public key that lasts 60 seconds, and an Instance Connect Endpoint reaches private instances.
  • Long-lived key pairs and bastion hosts are the older approach. Avoid them where you can.
  • Use termination protection, tags and client tokens to prevent expensive mistakes.

What a launch decides

Starting a server on AWS means answering in one request everything a data centre would spread over a week: what image, what hardware, where on the network, who may reach it, what it may do, and how big its disk is.

aws ec2 run-instances \
  --image-id ami-...  --instance-type t3.micro \
  --subnet-id subnet-...  --security-group-ids sg-... \
  --iam-instance-profile Name=web-app \
  --metadata-options HttpTokens=required \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web}]' \
  --client-token web-2026-11-29

Note what is absent: no key pair. HttpTokens=required protects instance metadata. The client token makes a retried request launch one instance, not two.

Describing the result:

{ "State": "running", "Private": "10.20.0.4", "Public": null, "Key": null,
  "Profile": "arn:aws:iam::123456789012:instance-profile/web-app" }

Check two things in that output. Public is null because the subnet does not assign public addresses, and Key is null because none was given. The tag is more than decoration: cost reports, automation and IAM conditions can all select on tags, so set them at launch and not later.

The lifecycle

pending → running → stopping → stopped, or shutting-down → terminated. Compute is billed per second while running, with a one-minute minimum each start.

Stop Terminate
Instance comes back yes no
EBS root volume kept deleted by default
Instance store data lost lost
Auto-assigned public IP released released
Compute charges end end

A started instance usually lands on different hardware. Hibernate saves memory to disk first, for supported configurations. Think of stop as switching off a computer and terminate as recycling it.

Turn on termination protection for anything precious:

$ aws ec2 terminate-instances --instance-ids i-...
An error occurred (OperationNotPermitted) ... may not be terminated.
Modify its 'disableApiTermination' instance attribute and try again.

Every instance also has status checks for the underlying system and for the instance itself, which you can alarm on to recover or reboot automatically.

Getting a shell

The old way: a key pair, port 22 open, a public IP or a bastion host. The core problem is that you now manage secrets and an open door. Keys get copied and never rotated. SSH keys and bastions explains that model; AWS offers better ones.

Session Manager. An agent on the instance connects outward to Systems Manager, and your session is relayed through it. No inbound rule, no public IP, no key. It needs three things:

  1. the SSM Agent (preinstalled on Amazon Linux and many AWS images);
  2. permissions, commonly the AmazonSSMManagedInstanceCore policy on the instance role;
  3. outbound 443 to the Systems Manager endpoints, through NAT or VPC endpoints.
aws ssm start-session --target i-0123456789abcdef0

Sessions can be logged, and port forwarding through the same channel reaches a private database from your laptop. The CLI needs the Session Manager plugin.

EC2 Instance Connect. Real SSH, but your temporary public key is pushed to the instance and lasts 60 seconds, authorised by IAM:

aws ec2-instance-connect ssh --instance-id i-...

For private instances, an EC2 Instance Connect Endpoint in the VPC tunnels to them without a public IP or bastion, at no additional charge. Use this when you need SSH tooling such as scp.

Whichever you choose, the permission to start a session is the permission to run commands on that host, often with administrator rights there and access to its role. Grant it as narrowly as you would the role itself, by instance tag where possible, and treat a request for broad access accordingly.

When you can’t get in

Check status checks first, then the console output for boot errors. For Session Manager, work through agent, role, network. For SSH, the security group, the route and the username (ec2-user on Amazon Linux). The EC2 serial console is a last resort when networking itself is broken.

The deeper fix is to need a shell less often: ship logs out, and replace broken instances from an image rather than repairing them by hand.

About the video

The embed is Digital Cloud Training’s “Connect to EC2 with Session Manager and EC2 Instance Connect”, March 2024, 20 minutes. Its chapters compare the methods, build a test VPC, launch public and private instances, deploy an Instance Connect Endpoint and set up Session Manager. It doesn’t cover lifecycle states, which are in the text. I judged it from chapters and description, since transcripts couldn’t be fetched. The short cloudonaut video in deeper shows SSH over these mechanisms.

What was run: the launch, describe and termination protection ran against moto, a local emulator, which boots nothing; I shortened the error. No shell session of any kind was opened. The ssm and ec2-instance-connect commands are from documentation, as are lifecycle and billing rules, checked in November 2026.

Where this fits

A fresh instance needs configuring without anyone logging in: instance metadata and user data.

Check yourself

  1. 01An instance in a private subnet with no NAT does not appear in Session Manager although its role is correct. What is missing?

    A network path to the Systems Manager endpoints over 443. Add NAT, or interface endpoints for the services it needs, and check their security group.

  2. 02You stop an instance over the weekend. What are you still paying for?

    Its EBS volumes and snapshots, any Elastic IP or other public IPv4 address it holds, and anything else attached such as a load balancer. Compute is not billed while stopped.

  3. 03After stop and start, your DNS record points nowhere. Why?

    The automatically assigned public IPv4 address is released on stop and a new one is assigned at start. Put a load balancer in front, or use an Elastic IP.

  4. 04Why can deleting a team member's access be easier with Session Manager than with SSH keys?

    Access is an IAM permission, removed in one place. A shared or copied SSH key stays valid on every instance until you remove it from each one.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark