I need to create an AWS IAM policy that prevents users from disabling the "Block all public access" configuration on S3 buckets, but still allows them to re-enable it if it was already disabled. I need to provide exemptions, when needed like exempting some accounts and buckets.
I initially tried this policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyDisablingBPAExceptExemptBucketsAndAccounts",
"Effect": "Deny",
"Action": [
"s3:PutBucketPublicAccessBlock",
"s3:PutAccountPublicAccessBlock"
],
"NotResource": [
"arn:aws:s3:::<bucketname>"
],
"Condition": {
"StringNotEquals": {
"aws:PrincipalAccount": [
"account-id-1"
]
},
"StringEquals": {
"s3:x-amz-public-access-block-enabled": "false"
}
}
}
]
}
The condition key s3:x-amz-public-access-block-enabled is invalid in AWS.
I need to,
- Deny users from disabling the public access block settings
- If Block Public Access (BPA) is already ON, no one should be able to turn it OFF.
- If BPA is OFF, users should be allowed to turn it ON.
The above policy is disabling both the conditions. How can I construct an IAM policy that achieves this?