However you choose to do it: your AWS IAM policy must be a good fit for your application’s actual access needs. We’ll discuss three ways to get it done.
Welcome to part six of our AWS Security Series. Last week I introduced Identity & Access Manager (IAM) and how you can control access to resources by using the predefined user, group, and role policy templates created and designed by AWS. This week, I want to show you how you can create your own custom IAM policy in the form of a JSON script and test it using the AWS Policy Simulator.
This article will cover the main elements, syntax, and structure of an IAM policy, and different ways to create your own IAM policy. Using a predefined IAM policy is more likely than not a perfect match for the permissions you actually need. However, now and again, you may want to tweak a small part of it to more exactly fit your requirements. Remember, when it comes to security, the more precisely you can define your controls, the better. You should avoid being lazy and deploying implementations that are “close enough” rather than hand crafting the perfect fit.
An IAM Policy is a JSON script made up of statements following a set syntax for allowing or denying permissions to an object within your AWS environment.
IAM Policy Syntax
Each policy has to have at least one statement whose structure might look like this:
The ‘Effect’ element can be set to either ‘Allow’ or ‘Deny’. These are explicit. By default, access to your resources is denied and so therefore if this is set to ‘Allow’ it replaces the default ‘Deny’. Similarly, if this were set to ‘Deny’ it would override any previous ‘Allow’. The ‘Action’ Element corresponds to API calls to AWS Services that authenticate through IAM. For example, this example represents API calls to delete a bucket (the action) within S3 (the service):
You are able to list multiple actions, if required, by using a comma to separate them:
Wildcards (*) are also allowed. So, for example, you could create an action to carry out all APIs relating to S3, such as:
The ‘Resource’ element specifies the actual resource you wish the permission to be applied to. AWS uses unique identifiers known as ARNs (Amazon Resource Name) to specify resources. Typically ARNs follow a syntax of:
- ‘Partition’ – This relates to the partition that the resource is found in. For standard AWS regions, this section would be ‘aws’.
- ‘Service’ – This reflects the specific AWS service, for example ‘s3’ or ‘ec2’.
- ‘Region’ – This is the region where the resource is located. Some services do not need a region specified, so this can sometimes be left blank.
- ‘Account-ID’ – This is your AWS Account ID (without hyphens). Again, some services do not need this information, and so can be left blank.
- ‘Resource’ – The value of this field will depend on the AWS service you are using. For example, if I were using the Action: “Action”:”s3:DeleteBucket”, then I could use the bucket name that I wanted the permission to delete: arn:aws:s3:::cloudacademyblog.
The ‘Condition’ element of the IAM Policy is an optional element that allows you to specify when the permissions will be activated based upon set conditions. Conditions use key value pairs, and all conditions must be met for the permissions to be activated. Here’s a full listing of these conditions.
Now we have a basic understanding of how JSON scripts are put together and their general flow, let’s see how we can modify existing policies to tweak them to your needs.
Copying an existing AWS IAM Policy
There will be times when you may not need to create a policy completely from scratch. You may be able to get away with adapting one that already exists. This can potentially save you a lot of time and effort. Open up your AWS console and select ‘IAM > Policies > Create Policy’. You will be presented with these three options:
For this example, select “Copy an AWS Managed Policy”.
You will then be shown a list all existing AWS managed policies, and there are a LOT! You can narrow your search by using the search function at the top of the page. For this example, I am going to search for S3 policies.
I will select the “AmazonS3FullAccess” IAM policy with the goal of modifying it to only allow full access to a specific bucket (cloudacademyblog).
Once selected, it displays the following JSON policy:
After reviewing this policy, I am happy with the ‘Effect’ and ‘Action’ elements, however I want to modify the ‘Resource’ section, which is currently set to “all” with the * wildcard. I will add the ARN for a specific bucket called ‘cloudacademyblog’. To do this, all I need to do is edit the policy and change the ‘Resource’ line to:
Once edited, I can give the policy a meaningful name and click Save. This policy will now appear in your list of the policies you can assign to your groups and users. Once assigned, it’s always a good idea to test your policy via the simulator by clicking on Simulate Policy.
This will load the policy simulator for the policy I just created:
This shows the policy’s JSON script on the left and, on the right hand side, I can select specific Services and Actions. The image below shows that I have selected ‘S3’ as the Service and ‘Create Bucket’ and ‘Delete Bucket’ as the Actions. I have also added the ARN for the cloudacademyblog bucket in the ‘Resource Type’ field.
Once all the information you want to test is there, click ‘Run Simulation’
The simulator results show that this IAM policy allows those actions to be carried out on the cloudacademyblog bucket. To confirm these actions are not possible on other buckets, I ran the following simulation with the ‘Resource’ set to all (checking all buckets) with the * wildcard:
As you can see, this was denied by the policy as the policy only states that we have full access to the arn:aws:s3:::cloudacademyblog bucket.
Using the IAM Policy Generator
If you do not yet feel confident enough to edit existing policies, then AWS provides the IAM Policy Generator. From with the AWS Console select ‘IAM > Policies > Create Policy’ and this time select ‘Policy Generator’.
From here, via drop down boxes, you can select the Effect, Service, Action, and Resource. Additionally, if required, you can also set up conditions. Once all your options are set, click the ‘Add Statement’ button.
The image below shows that I have simply replicated the IAM policy I created in the last section, but this time via the Policy Generator:
When you are happy with your new IAM policy, select ‘Next Step’. From there, you can give your new IAM policy a name and description, and view it as a JSON script. Click ‘Create Policy’ and then attach it to any groups or users as needed.
Writing your own IAM policy
If you’re ready to write your own IAM policy from scratch, there’s nothing stopping you. From the AWS Console, select ‘IAM > Policies > Create Policy’, and this time select ‘Create your own policy’.
Here you are presented with a blank canvas allowing you to give your policy a name and description…but most importantly, a blank policy document where you can write your JSON document adhering to the policy syntax rules discussed at the beginning of this article.
Once you are happy with your policy, use the ‘Validate Policy’ button at the bottom to determine if there are any errors with your policy. Again, when you are finished click ‘Create Policy’.
As you can see there are a number of methods available to help you refine your IAM policies, and you aren’t forced to use predefined policies. Once you get used to the syntax – and benefits – of writing your own policies, you will be able to effectively and efficiently lock down access to your resources to ensure they are only accessed by authorized API calls.
There are many, many specific commands that can be controlled through an IAM policy, but they’re a bit beyond the scope of this post. AWS provides great API listings for the different services through their extensive documentation for advanced policy writers.
What are the key elements we have we learned this week?
- It is always best to refine your IAM Policy down to an API call level for the tightest level of security.
- Policies can be created in a number of ways via IAM: from writing your own JSON script to assisted creation via drop-down menus.
- Testing your policies via the Simulator is a very important part of ensuring they only allow or deny exactly what they were designed for.
In my next AWS Security article, I will be taking a look at Federated access with IAM, Trusted Advisor, AWS Billing restrictions and AWS Linked accounts. Until then I hope you all have a Happy New year!
Thank you for taking the time to read my article. If you have any feedback please leave a comment below.
New Content: AWS, Azure, Typescript, Java, Docker, 13 New Labs, and Much More
This month, our Content Team released a whopping 13 new labs in real cloud environments! If you haven't tried out our labs, you might not understand why we think that number is so impressive. Our labs are not “simulated” experiences — they are real cloud environments using accounts on A...
Kickstart Your Tech Training With a Free Week on Cloud Academy
Are you looking to make a jump in your technical career? Want to get trained or certified on AWS, Azure, Google Cloud Platform, DevOps, Kubernetes, Python, or another in-demand skill?Then you'll want to mark your calendar. Starting Monday, June 22 at 12:00 a.m. PDT (3:00 a.m. EDT), ...
New Content: AZ-500 and AZ-400 Updates, 3 Google Professional Exam Preps, Practical ML Learning Path, C# Programming, and More
This month, our Content Team released tons of new content and labs in real cloud environments. Not only that, but we introduced our very first highly interactive "Office Hours" webinar. This webinar, Acing the AWS Solutions Architect Associate Certification, started with a quick overvie...
Azure vs. AWS: Which Certification Provides the Brighter Future?
More and more companies are using cloud services, prompting more and more people to switch their current IT position to something cloud-related. The problem is most people only have that much time after work to learn new technologies, and there are plenty of cloud services that you can ...
Blog Digest: 5 Reasons to Get AWS Certified, OWASP Top 10, Getting Started with VPCs, Top 10 Soft Skills, and More
Thank you for being a valued member of our community! We recently sent out a short survey to understand what type of content you would like us to add to Cloud Academy, and we want to thank everyone who gave us their input. If you would like to complete the survey, it's not too late. It ...
New Content: Alibaba, Azure Cert Prep: AI-100, AZ-104, AZ-204 & AZ-400, Amazon Athena Playground, Google Cloud Developer Challenge, and much more
This month, our Content Team released 8 new learning paths, 4 courses, 7 labs in real cloud environments, and 4 new knowledge check assessments. Not only that, but we introduced our very first course on Alibaba Cloud, and our expert instructors are working 'round the clock to create 6 n...
Top 5 Reasons to Get AWS Certified Right Now
Cloud computing trends are on the rise and have been for some time already. Fortunately, it’s never too late to start learning cloud computing. Skills like AWS and others associated with cloud computing are in high demand because cloud technologies have become crucial for many businesse...
Introducing Our Newest Lab Environments: Lab Playgrounds
Want to train in a real cloud environment, but feel slowed down by spinning up your own deployments? When you consider security or pricing costs, it can be costly and challenging to get up to speed quickly for self-training. To solve this problem, Cloud Academy created a new suite of la...
Blog Digest: AWS Breaking News, Azure DevOps, AWS Study Guide, 8 Ways to Prevent a Ransomware Attack, and More
New articles by topicAWS Azure Data Science Google Cloud Cloud Adoption Platform Updates & New Content Security Women in TechAWSBreaking News: All AWS Certification Exams Now Available Online As an Advanced AWS Technology Partner, C...
AWS Certified Solutions Architect Associate: A Study Guide
Want to take a really impactful step in your technical career? Explore the AWS Solutions Architect Associate certificate. Its new version (SAA-C02) was released on March 23, 2020, though you can still take SAA-C01 through July 1, 2020. This post will focus on version SAA-C02.The AWS...
New on Cloud Academy: AWS Solutions Architect Exam Prep, Azure Courses, GCP Engineer Exam Prep, Programming, and More
Free content on Cloud Academy More and more customers are relying on our technology and content to keep upskilling their people in these months, and we are doing our best to keep supporting them. While the world fights the COVID-19 pandemic, we wanted to make a small contribution to he...
Breaking News: All AWS Certification Exams Now Available Online
Remote proctoring for all AWS certifications Cloud Academy is an Advanced AWS Technology Partner, and we are happy to announce all AWS certification exams are available online! What does this mean for you? You can stay focused on your certification goal. Or you can start a certifica...