In this section, we attach the policy to the previously created role to ensure the role has the appropriate permissions defined in the policy.
Access IAM Dashboard
Create a IAM policy in IAM Dashboard
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Deny",
"Action": [
"ec2:TerminateInstances",
"ec2:DeleteTags",
"ec2:CreateTags"
],
"Resource": "*",
"Condition": {
"StringEquals": {
"aws:ResourceTag/status": "isolated"
}
}
}
]
}
Deny-termination-of-isolated-instances
.In real life scenario the author recommend using Service control policies (SCP)
Create a Group
ec2-user
.AmazonEC2FullAccess
AWS Managed PolicyDeny-termination-of-isolated-instances
Create a user
testuser
.You should copy the password to a notepad or a text editor if you choose the autogenerated password avoid losing access to the account.
This is just for demo purposes, to show that once the instance is tagged as isolated, the regular users with this policy will not be able to terminate the instance. In real life you should follow the best practice of using only temporary credentials, as per Well-Architected-Framework’s best practice
After testuser creation is complete, head on to the next step Configure Response