{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DescribeInstanceActions",
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeInstances",
        "ec2:DescribeRouteTables",
        "ec2:DescribeInstanceTypes",
        "ec2:DescribeVolumes",
        "ec2:DescribeInstanceAttribute",
        "ec2:DescribeSnapshots",
        "ec2:DescribeVpcs",
        "ssm:GetCommandInvocation",
        "ssm:DescribeInstanceInformation"
      ],
      "Resource": "*"
    },
    {
      "Sid": "DescribeInstanceStatus",
      "Effect": "Allow",
      "Action": "ec2:DescribeInstanceStatus",
      "Resource": "*"
    },
    {
      "Sid": "TargetRuleActions",
      "Effect": "Allow",
      "Action": [
        "events:DeleteRule",
        "events:PutTargets",
        "events:DescribeRule",
        "events:PutRule",
        "events:RemoveTargets"
      ],
      "Resource": [
        "arn:*:events:*:*:rule/SSMSAPManagedRule*",
        "arn:*:events:*:*:event-bus/default"
      ]
    },
    {
      "Sid": "DocumentActions",
      "Effect": "Allow",
      "Action": [
        "ssm:DescribeDocument",
        "ssm:SendCommand"
      ],
      "Resource": [
        "arn:*:ssm:*:*:document/AWSSystemsManagerSAP-*",
        "arn:*:ssm:*:*:document/AWSSSMSAP*",
        "arn:*:ssm:*:*:document/AWSSAP*"
      ]
    },
    {
      "Sid": "CustomerSendCommand",
      "Effect": "Allow",
      "Action": "ssm:SendCommand",
      "Resource": "arn:*:ec2:*:*:instance/*",
      "Condition": {
        "StringEqualsIgnoreCase": {
          "ssm:resourceTag/SSMForSAPManaged": "True"
        }
      }
    },
    {
      "Sid": "InstanceTagActions",
      "Effect": "Allow",
      "Action": [
        "ec2:CreateTags",
        "ec2:DeleteTags"
      ],
      "Resource": "arn:*:ec2:*:*:instance/*",
      "Condition": {
        "Null": {
          "aws:RequestTag/awsApplication": "false"
        },
        "StringEqualsIgnoreCase": {
          "ec2:ResourceTag/SSMForSAPManaged": "True"
        }
      }
    },
    {
      "Sid": "DescribeTag",
      "Effect": "Allow",
      "Action": "ec2:DescribeTags",
      "Resource": "*"
    },
    {
      "Sid": "GetApplication",
      "Effect": "Allow",
      "Action": "servicecatalog:GetApplication",
      "Resource": "arn:*:servicecatalog:*:*:*"
    },
    {
      "Sid": "UpdateOrDeleteApplication",
      "Effect": "Allow",
      "Action": [
        "servicecatalog:DeleteApplication",
        "servicecatalog:UpdateApplication"
      ],
      "Resource": "arn:*:servicecatalog:*:*:*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/SSMForSAPCreated": "True"
        }
      }
    },
    {
      "Sid": "CreateApplication",
      "Effect": "Allow",
      "Action": [
        "servicecatalog:TagResource",
        "servicecatalog:CreateApplication"
      ],
      "Resource": "arn:*:servicecatalog:*:*:*",
      "Condition": {
        "StringEquals": {
          "aws:RequestTag/SSMForSAPCreated": "True"
        }
      }
    },
    {
      "Sid": "CreateServiceLinkedRole",
      "Effect": "Allow",
      "Action": "iam:CreateServiceLinkedRole",
      "Resource": "arn:aws:iam::*:role/aws-service-role/servicecatalog-appregistry.amazonaws.com/AWSServiceRoleForAWSServiceCatalogAppRegistry",
      "Condition": {
        "StringEquals": {
          "iam:AWSServiceName": "servicecatalog-appregistry.amazonaws.com"
        }
      }
    },
    {
      "Sid": "PutMetricData",
      "Effect": "Allow",
      "Action": "cloudwatch:PutMetricData",
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "cloudwatch:namespace": [
            "AWS/Usage",
            "AWS/SSMForSAP"
          ]
        }
      }
    },
    {
      "Sid": "CreateAttributeGroup",
      "Effect": "Allow",
      "Action": "servicecatalog:CreateAttributeGroup",
      "Resource": "arn:*:servicecatalog:*:*:/attribute-groups/*",
      "Condition": {
        "StringEquals": {
          "aws:RequestTag/SSMForSAPCreated": "True"
        }
      }
    },
    {
      "Sid": "GetAttributeGroup",
      "Effect": "Allow",
      "Action": "servicecatalog:GetAttributeGroup",
      "Resource": "arn:*:servicecatalog:*:*:/attribute-groups/*"
    },
    {
      "Sid": "DeleteAttributeGroup",
      "Effect": "Allow",
      "Action": "servicecatalog:DeleteAttributeGroup",
      "Resource": "arn:*:servicecatalog:*:*:/attribute-groups/*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/SSMForSAPCreated": "True"
        }
      }
    },
    {
      "Sid": "AttributeGroupActions",
      "Effect": "Allow",
      "Action": [
        "servicecatalog:AssociateAttributeGroup",
        "servicecatalog:DisassociateAttributeGroup"
      ],
      "Resource": "arn:*:servicecatalog:*:*:*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/SSMForSAPCreated": "True"
        }
      }
    },
    {
      "Sid": "ListAssociatedAttributeGroups",
      "Effect": "Allow",
      "Action": "servicecatalog:ListAssociatedAttributeGroups",
      "Resource": "arn:*:servicecatalog:*:*:*"
    },
    {
      "Sid": "CreateGroup",
      "Effect": "Allow",
      "Action": [
        "resource-groups:CreateGroup",
        "resource-groups:Tag"
      ],
      "Resource": "arn:*:resource-groups:*:*:group/SystemsManagerForSAP-*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/SSMForSAPCreated": "True"
        },
        "ForAllValues:StringEquals": {
          "aws:TagKeys": [
            "SSMForSAPCreated"
          ]
        }
      }
    },
    {
      "Sid": "GetGroup",
      "Effect": "Allow",
      "Action": "resource-groups:GetGroup",
      "Resource": "arn:*:resource-groups:*:*:group/SystemsManagerForSAP-*"
    },
    {
      "Sid": "DeleteGroup",
      "Effect": "Allow",
      "Action": "resource-groups:DeleteGroup",
      "Resource": "arn:*:resource-groups:*:*:group/SystemsManagerForSAP-*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/SSMForSAPCreated": "True"
        }
      }
    },
    {
      "Sid": "CreateAppTagResourceGroup",
      "Effect": "Allow",
      "Action": [
        "resource-groups:CreateGroup"
      ],
      "Resource": "arn:*:resource-groups:*:*:group/AWS_AppRegistry_AppTag_*",
      "Condition": {
        "StringEquals": {
          "aws:RequestTag/EnableAWSServiceCatalogAppRegistry": "true"
        }
      }
    },
    {
      "Sid": "TagAppTagResourceGroup",
      "Effect": "Allow",
      "Action": [
        "resource-groups:Tag"
      ],
      "Resource": "arn:*:resource-groups:*:*:group/AWS_AppRegistry_AppTag_*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/EnableAWSServiceCatalogAppRegistry": "true"
        }
      }
    },
    {
      "Sid": "GetAppTagResourceGroupConfig",
      "Effect": "Allow",
      "Action": [
        "resource-groups:GetGroupConfiguration"
      ],
      "Resource": [
        "arn:*:resource-groups:*:*:group/AWS_AppRegistry_AppTag_*"
      ]
    },
    {
      "Sid": "StartStopInstances",
      "Effect": "Allow",
      "Action": [
        "ec2:StartInstances",
        "ec2:StopInstances"
      ],
      "Resource": "arn:*:ec2:*:*:instance/*",
      "Condition": {
        "StringEqualsIgnoreCase": {
          "ec2:resourceTag/SSMForSAPManaged": "True"
        }
      }
    },
    {
      "Sid": "SsmSapResourceGroup",
      "Effect": "Allow",
      "Action": [
        "resource-groups:Tag",
        "resource-groups:CreateGroup"
      ],
      "Resource": "arn:aws:resource-groups:*:*:group/SystemsManagerForSAP-*",
      "Condition": {
        "StringEquals": {
          "aws:RequestTag/SSMForSAPCreated": "True"
        },
        "ForAllValues:StringEquals": {
          "aws:TagKeys": [
            "SSMForSAPCreated"
          ]
        }
      }
    },
    {
      "Sid": "ManageSsmSapTagsOnEc2Instances",
      "Effect": "Allow",
      "Action": [
        "ec2:CreateTags",
        "ec2:DeleteTags"
      ],
      "Resource": "arn:aws:ec2:*:*:instance/*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/SSMForSAPManaged": "True"
        },
        "ForAllValues:StringLike": {
          "aws:TagKeys": [
            "SystemsManagerForSAP-*"
          ]
        }
      }
    },
    {
      "Sid": "ManageSsmSapTagsOnEbsVolumes",
      "Effect": "Allow",
      "Action": [
        "ec2:CreateTags",
        "ec2:DeleteTags"
      ],
      "Resource": "arn:aws:ec2:*:*:volume/*",
      "Condition": {
        "ForAllValues:StringLike": {
          "aws:TagKeys": [
            "SystemsManagerForSAP-*"
          ]
        }
      }
    },
    {
      "Sid": "ManageAppTagsOnEbsVolumes",
      "Effect": "Allow",
      "Action": [
        "ec2:CreateTags",
        "ec2:DeleteTags"
      ],
      "Resource": "arn:aws:ec2:*:*:volume/*",
      "Condition": {
        "ArnLike": {
          "aws:RequestTag/awsApplication": "arn:aws:resource-groups:*:*:group/*/*"
        },
        "ForAllValues:StringEquals": {
          "aws:TagKeys": [
            "awsApplication"
          ]
        }
      }
    },
    {
      "Sid": "ManageCostAllocationTags",
      "Effect": "Allow",
      "Action": [
        "ce:ListCostAllocationTags",
        "ce:UpdateCostAllocationTagsStatus",
        "ce:ListCostAllocationTagBackfillHistory",
        "ce:StartCostAllocationTagBackfill"
      ],
      "Resource": "*"
    }
  ]
}
