DEV Community

Cover image for AWS πŸ”’ How to restrict access by IP
Vitali
Vitali

Posted on β€’ Originally published at securityblog.cloud

7 1

AWS πŸ”’ How to restrict access by IP

Hi there!

In today's post, I would like to show you how you can restrict access to your AWS Account. Very often companies use static IP addresses to access the Internet. So if you know that access to your AWS account has to happen from specific IP, why allow it from the whole Internet.
Here is a logic schema of how we are going to make restriction:

logic schema

⚠The most important part is an IAM policy that will enforce our restriction. The policy denies any user's actions made from untrusted IP. To make so, we have to create a condition and specify two keys:

  • aws:SourceIp
  • aws:ViaAWSService

By the first one, we allow access from our IPs, by the second one we allow AWS Services to access our resources without the restriction. 
Your policy may look like it:

{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Deny",
        "Action": "*",
        "Resource": "*",
        "Condition": {
            "NotIpAddress": {
                "aws:SourceIp": [
                    "XXX.XXX.XXX.0/24",
                    "YYY.YYY.YYY.0/24"
                ]
            },
            "Bool": {"aws:ViaAWSService": "false"}
        }
    }
}

The good way to apply our restriction is to use IAM users' groups. IAM users groups usage is a good practice to handle permissions. But our approach will work with a single user as well. Depends on your case you may or may not use IAM groups.

So next, create a group, attach a policy with necessary accesses and with IP restriction.

Create a Group

Now even if API keys or a user's credentials will be compromised, an attacker has to avoid one more security mechanism in your AWS Account.
Bye!πŸ‘‹

Photo by Markus Spiske on Unsplash

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
rcfrias profile image
Roberto Frias β€’

I've tried adding this policy directly to the user and to the user group, and it just denies any attempt to use the aws-cli, but I've been unable to use programmatic access from a valid IP address. :(

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay