To manage costs you need to manage and control your usage. AWS offers multiple regions, so depending on your business requirements you can limit access to AWS services depending on the region. This can be used to ensure usage is only allowed in specific regions which are more cost effective, and minimize associated usage and cost, such as data transfer.
We will create a policy that allows all EC2, RDS and S3 access in a single region only. NOTE: it is best practice to provide only the minimum access required, the policy used here is for brevity and simplicity, and should only be implemented as a demonstration before being removed.
Go to the IAM service page:
Select Policies from the left menu:
Click Create Policy:
Click the JSON tab:
Copy and paste the policy into the console:
Click Review policy:
Create the policy with the following details:
You have successfully created an IAM policy to restrict usage by region.
Select Groups from the left menu:
Click on the CostTest group (created previously):
Select the Permissions tab:
Click Attach Policy:
Click Policy Type and select Customer Managed:
Select the checkbox next to Region_Restrict (created above) and click Attach Policy:
You have successfully attached the policy to the CostTest group.
Log out from the console
Logon to the console as the TestUser1 user, go to the EC2 Service dashboard:
Click the current region in the top right, and select US West (N.California):
Try to launch an instance by clicking Launch Instance, select Launch Instance:
Click on Select next to the Amazon Linux 2 AMI, You will receive an error when you select an AMI as you do not have permissions:
You have successfully verified that you cannot launch any instances outside of the N.Virginia region. We will now verify we have access in us-east-1 (N.Virginia):
Change the region by clicking the current region, and selecting US East (N.Virginia):
Now attempt to launch an instance, choose the Amazon Linux 2 AMI, leave 64-bit (x86) selected, click Select:
Scroll down and select a c5.large, and click Review and Launch:
Take note of the security group created (as you need to delete it), Click Launch:
Select Proceed without a key pair, and click I acknowledge.. checkbox, and click Launch Instances:
You will get a success message, click on the instance id:
Ensure the correct instance is selected, click Actions, then Instance State, then Terminate:
Confirm the instance ID is correct, click Yes, Terminate:
Log out of the console as TestUser1.
You have successfully implemented an IAM policy that restricts all EC2, RDS and S3 operations to a single region. You have also successfully launched a c5 Instance Type.