# ec2：防止在没有 IMDSv2 ,EBS未加密的情况下启动 EC2 实例

使用IMDSv1版本的实例非常容易被攻击者获取到安全凭据也就是IAM的AKSK，因此为了避免严重的安全事件，建议所有的EC2实例都使用IMDSv2。

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DenyRunInstanceWithNoIMDSv2",
      "Effect": "Deny",
      "Action": "ec2:RunInstances",
      "Resource": [
        "arn:aws:ec2:*:*:instance/*"
      ],
      "Condition": {
        "StringNotEquals": {
          "ec2:MetadataHttpTokens": "required"
        }
      }
    },
    {
      "Sid": "DenyRunInstanceWithUnencryptEBS",
      "Effect": "Deny",
      "Action": [
        "ec2:RunInstances",
        "ec2:AttachVolume"
      ],
      "Resource": "arn:aws:ec2:*:*:volume/*",
      "Condition": {
        "Bool": {
          "ec2:Encrypted": "false"
        }
      }
    }
  ]
}
```

参考资料

[防止在没有 IMDSv2 的情况下启动 EC2 实例](https://docs.aws.amazon.com/zh_cn/organizations/latest/userguide/orgs_manage_policies_scps_examples_ec2.html#example-ec2-2)

[防止在EBS没有加密的情况下启动EC2实例](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonec2.html#amazonec2-ec2_Encrypted)
