Lesson 4 of 5
Control network access with a security group
Edit the security group for your EC2 instance to allow only your own computer to connect, and block public access on the S3 bucket; this restricts who can reach your app, similar to network security groups in Azure and firewall rules in Google Cloud.
4 minCloud
After this lesson, you will be able to restrict access to your EC2 instance and S3 bucket so that only your computer can connect and upload files.
Understand your current security group
A security group is a set of rules that controls traffic to and from your EC2 instance. It acts like a firewall. When you launched your instance in lesson 2, AWS created a default security group. By default, it allows inbound SSH from any IP address. That means anyone on the internet can try to connect to your instance on port 22.
You can see your security groups in the EC2 console. In the left navigation pane, under Network & Security, choose Security Groups. Find the security group for your instance. Its name usually starts with launch-wizard. Select it and look at the Inbound rules tab. You will see a rule for SSH with source 0.0.0.0/0, which means any IP address.
Find your public IP address
To allow only your computer, you need your public IP address. This is the address your internet provider gives you. Open a web browser on your computer and search for "what is my IP". Many websites show it. Write it down. It looks like four numbers separated by dots, for example 203.0.113.24.
Your public IP address can change if you restart your router or if your provider changes it. If you cannot connect later, check your IP address again and update the security group rule.
Edit the inbound rule for SSH
In the security group details, choose the Inbound rules tab, then Edit inbound rules. Find the rule for SSH. Its type is SSH and port range is 22. In the Source column, change the value from 0.0.0.0/0 to your public IP address followed by /32. For example, if your IP is 203.0.113.24, enter 203.0.113.24/32. The /32 means exactly that one address.
Choose Save rules. Now only your computer can connect to the instance using SSH. If you try from another network, the connection will time out.
Test the rule. From your computer, open a terminal and run the SSH command you used in lesson 2. It should work. If it does not, check that you entered your public IP correctly and that the rule is saved.
Understand outbound rules
Security groups also have outbound rules. These control traffic leaving your instance. The default outbound rule allows all traffic to anywhere. That is fine for this course. You do not need to change it.
Block public access on your S3 bucket
Your S3 bucket currently allows public access. In lesson 3, you uploaded a file and downloaded it from your EC2 instance. The bucket is private by default, but you may have changed settings when you created it. To be sure, you will block all public access.
Open the S3 console. Choose your bucket. Choose the Permissions tab. Find the section Block public access. Choose Edit. Tick the box for Block all public access. This includes blocking public access through bucket policies and access control lists. Choose Save changes. Confirm by typing confirm when asked.
Now the bucket is private. Only your AWS account can access it, and only with proper permissions. Your EC2 instance can still access it because it uses your AWS credentials from lesson 3. Later, in lesson 5, you will use an IAM role instead of storing credentials on the instance.
How this maps to other clouds
Azure uses network security groups to control traffic to virtual machines. You add inbound and outbound rules similar to AWS security groups. Google Cloud uses firewall rules that apply to a whole network or to specific instances. The idea is the same: you allow only the traffic your app needs.
For object storage, Azure Blob Storage and Google Cloud Storage also have settings to block public access. You should always keep storage private unless you have a reason to make it public.
Summary of what you changed
You changed the inbound rule for SSH in your security group to allow only your public IP address. You also blocked all public access on your S3 bucket. Now your EC2 instance and your bucket are protected from unwanted access.
Key takeaways
- A security group controls inbound and outbound traffic to an EC2 instance.
- To allow only your computer, set the SSH inbound rule source to your public IP address with /32.
- Your public IP address can change, so update the rule if you cannot connect.
- Block all public access on S3 buckets unless you need it.
- Azure network security groups and Google Cloud firewall rules work similarly.
Try it
From a different network, such as a mobile phone hotspot, try to connect to your EC2 instance using SSH. The connection should fail because the security group only allows your home IP address. Then, from your home network, connect to your EC2 instance using SSH. On the instance, run aws s3 ls s3://your-bucket-name/. The listing should still work because the instance has the AWS credentials you set up in lesson 3. Finally, check the bucket's public access settings in the S3 console to confirm that all public access is blocked. A good result is that SSH from the other network times out, the S3 listing works from your EC2 instance, and the bucket shows all public access blocked.
Official documentation
Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.