Search code examples
amazon-web-servicesamazon-iamamazon-ami

AWS IAM policy permissions clash issue


I have tried to create a policy that will prevent the deregistration of AMIs, unless the AMIs have the appropriate "delete this" tag. When I run the IAM policy simulator, the policy doesn't seem to work and the AMIs are allowed to be deregistered, because users already are associated with policies that are more permissive than my new policy.

Is it possible to make my custom policy take priority over other policies? Or do I have to create new policies that explicitly do not have the Deregister AMI permission?


Solution

  • The following IAM policy will deny deregistration of an AMI (just replace with your concrete resource ARN) when said AMI does not have the "delete" tag or that tag's value is not "yes". This works regardless of any possible Allow permissions that the calling identity might have.

    This is because permission statements with "Deny" action always take precedence over any Allow permissions.

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Deny",
          "Action": "ec2:DeregisterImage",
          "Resource": "arn:aws:ec2:*::image/*",
          "Condition": {
            "StringNotEquals": {
              "aws:ResourceTag/delete": "yes"
            }
          }
        }
      ]
    }
    

    Read this page for the detailed algorithm IAM uses to evaluate permissions: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_evaluation-logic.html