IAM Policy Examples
AWS access is fully controlled by IAM. IAM works based on json format. Please check the below json examples :
Allows full permission to S3 buckets
{ "Version": "2012-10-17", "Statement":[{ "Effect": "Allow", "Action": "s3:*", "Resource": "*" } ] }
Allows full permission to defined S3 buckets
This IAM policy grants the IAM entity (user, group, or role) it is attached to permission to perform any S3 operation on the bucket named “my_bucket”, as well as that bucket’s contents.
{ "Version": "2012-10-17", "Statement":[{ "Effect": "Allow", "Action": "s3:*", "Resource": ["arn:aws:s3:::my_bucket", "arn:aws:s3:::my_bucket/*"] } ] }
Bucket Policy
This S3 bucket policy enables the root account 111122223333 and the IAM user Alice under that account to perform any S3 operation on the bucket named “my_bucket”, as well as that bucket’s contents.
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": ["arn:aws:iam::111122223333:user/Alice", "arn:aws:iam::111122223333:root"] }, "Action": "s3:*", "Resource": ["arn:aws:s3:::my_bucket", "arn:aws:s3:::my_bucket/*"] } ] }
Allows Read and Write Access to Objects in an S3 Bucket
The s3:*Object action uses a wildcard as part of the action name. The AllObjectActions statement allows the GetObject, DeleteObject, PutObject, and any other Amazon S3 action that ends with the word "Object".
{ "Version": "2012-10-17", "Statement": [ { "Sid": "ListObjectsInBucket", "Effect": "Allow", "Action": ["s3:ListBucket"], "Resource": ["arn:aws:s3:::bucket-name"] }, { "Sid": "AllObjectActions", "Effect": "Allow", "Action": "s3:*Object", "Resource": ["arn:aws:s3:::bucket-name/*"] } ] }
Restrict access to a bucket to specific IP addresses
To secure our files on Amazon S3, we can restrict access to a S3 bucket to specific IP addresses.
The following bucket policy grants permissions to any user to perform any S3 action on objects in the specified bucket. However, the request must originate from the range of IP addresses specified in the condition. The condition in this statement identifies 192.168.143.* range of allowed IP addresses with one exception, 192.168.143.188.
{ "Version": "2012-10-17", "Id": "S3PolicyIPRestrict", "Statement": [ { "Sid": "IPAllow", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": "s3:*", "Resource": "arn:aws:s3:::bucket/*", "Condition" : { "IpAddress" : { "aws:SourceIp": "192.168.143.0/24" }, "NotIpAddress" : { "aws:SourceIp": "192.168.143.188/32" } } } ] }
Allows specify which VPCs or IP addresses can access my Amazon S3 bucket
Following bucket policy blocks traffic to the bucket unless the request is from specified VPC endpoints (aws:SourceVpce) or external IP addresses (aws:SourceIp). Note the following:
*To use this policy with the aws:SourceVpce condition, you must have a VPC endpoint for Amazon S3.
*To allow users to perform S3 actions on the bucket from the VPC endpoints or IP addresses, you must explicitly grant those user-level permissions. You can grant user-level permissions on either an AWS Identity and Access Management (IAM) policy or another statement in the bucket policy.
{ "Version": "2012-10-17", "Id": "VPCe and SourceIP", "Statement": [{ "Sid": "VPCe and SourceIP", "Effect": "Deny", "Principal": "*", "Action": "s3:*", "Resource": [ "arn:aws:s3:::awsexamplebucket", "arn:aws:s3:::awsexamplebucket/*" ], "Condition": { "StringNotLike": { "aws:sourceVpce": [ "vpce-1111111", "vpce-2222222" ] }, "NotIpAddress": { "aws:SourceIp": [ "11.11.11.11/32", "22.22.22.22/32" ] } } }] }
Allows to list all buckets and its location and able to Read bucket object in an S3 Bucket
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListAllMyBuckets", "s3:GetBucketLocation" ], "Resource": [ "arn:aws:s3:::*" ] }, { "Effect": "Allow", "Action": "s3:GetObject", "Resource": [ "arn:aws:s3:::YOURBUCKETNAME", "arn:aws:s3:::YOURBUCKETNAME/*" ] } ] }
Allows only specific user and deny all in S3 Bucket
Note: Dont remove root user ARN
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": ["arn:aws:iam::689304765025:user/guru", "arn:aws:iam::111122223333:root"] }, "Action": "s3:*", "Resource": [ "arn:aws:s3:::octoberbucketclass", "arn:aws:s3:::octoberbucketclass/*" ] }, { "Effect": "Allow", "Principal": "*", "Action": "s3:*", "Resource": [ "arn:aws:s3:::octoberbucketclass", "arn:aws:s3:::octoberbucketclass/*" ] } ] }
Allows Read and Write Access to Objects in an S3 Bucket
Allows Read and Write Access to Objects in an S3 Bucket
Allows Read and Write Access to Objects in an S3 Bucket
Click to Download after