In modern cloud architectures, the default “admin” or overly permissive access models are a significant security liability. The principle of least privilege (PoLP) dictates that every identity—whether human or machine—should have only the permissions necessary to perform its specific function. This lab guides IT students and junior engineers through a concrete scenario in Amazon Web Services (AWS), demonstrating how to architect cross-service access using Role Assumers, Resource-Based Policies, and SCPs. By the end of this guide, you will have constructed a secure environment where a compute instance can securely read from an S3 bucket without granting it full administrative control over your AWS account.
Scenario: Secure Data Processing Pipeline
Imagine a company migrating to AWS. Their data engineering team needs to build a pipeline where an EC2 instance in a private subnet must periodically download large datasets from an Amazon S3 bucket, process them locally, and then upload the results to a different S3 bucket for archival. The security requirement is strict: the EC2 instance must not be able to list other buckets, access IAM credentials, or modify network configurations.
This scenario highlights the need for cross-service access that is tightly scoped. We will achieve this by replacing long-lived Access Keys with temporary security credentials obtained via an IAM Role attached to the EC2 instance. This approach ensures that even if credentials are compromised, they expire automatically, reducing the window of opportunity for attackers.
Prerequisites and Environment Setup
Before executing any commands, ensure you have access to an AWS account. For safety, create a dedicated “Lab” Account or use a specific Organizational Unit (OU) within your organization. Do not perform these steps on your primary production account without strict isolation.
You will need:
* An AWS CLI installed and configured with aws configure.
* A VPC with public internet access to download the S3 bucket contents if needed for verification, though the lab focuses on internal policy enforcement.
* Basic knowledge of IAM terminology (Users, Groups, Roles, Policies).
Important Note: All commands below are illustrative. Always review your specific AWS Region settings and verify that resource names do not conflict with existing production resources.
Step 1: Create the Target S3 Buckets and Initial Policy
First, we create the data sources and destinations. We will create a “Source” bucket containing dummy data and an “Archive” bucket for processed files.
aws s3 mb s3://my-lab-source-data --region us-east-1
aws s3 mb s3://my-lab-archive-data --region us-east-1
Next, we define a JSON policy that grants read-only access to the source bucket and write access to the archive bucket. This is the core of our least-privilege design.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReadSource",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:ListBucket"],
"Resource": [
"arn:aws:s3:::my-lab-source-data",
"arn:aws:s3:::my-lab-source-data/*"
]
},
{
"Sid": "AllowWriteArchive",
"Effect": "Allow",
"Action": ["s3:PutObject"],
"Resource": "arn:aws:s3:::my-lab-archive-data/*"
}
]
}
Attach this policy to an IAM User named DataProcessor. While roles are preferred for EC2, creating a user here helps us understand the difference between user-based and role-based permissions.
aws iam create-user --user-name DataProcessor
aws iam attach-user-policy --user-name DataProcessor --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
# Note: In a real lab, you would upload the custom policy above instead of attaching a managed one.
Step 2: Create and Attach an IAM Role for EC2
The critical step in cross-service access is creating an IAM Role. Unlike users, roles do not have long-term credentials; they are assumed by trusted entities like EC2 instances.
Create the role using the AWS Management Console or CLI. When defining the trust relationship (the AssumeRolePolicyDocument), ensure the principal is set to ec2.amazonaws.com. This restricts assumption strictly to EC2 instances within your account.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Create the role and attach the S3 policy defined in Step 1. Name the role EC2-S3-DataProcessor. In the AWS Console, navigate to IAM > Roles > Create Role. Select “AWS Service” as the trusted entity, choose “EC2”, and then select your custom S3 policy under “Add Permission”.
Step 3: Launch the EC2 Instance with the New Role
Now we launch an Amazon Linux 2 or Ubuntu instance. Crucially, when configuring the instance, you must specify the IAM role name in the launch parameters. Do not attach an IAM instance profile manually after launch; it must be defined at creation time for security best practices.
# Example using AWS CLI (Adjust image-id and instance-type as needed)
aws ec2 run-instances \
--image-id ami-0c55b159cbfafe1f0 \
--count 1 \
--instance-type t2.micro \
--key-name MyLabKeyPair \
--security-group-ids sg-xxxxxxxx \
--subnet-id subnet-xxxxx \
--iam-instance-profile Name=EC2-S3-DataProcessor
Expected Result: The instance launches successfully. Upon logging in via SSH, you should see the aws CLI installed (or install it). Run the following to verify that the instance has assumed the role and can access S3:
aws sts get-caller-identity
# Output should show an ARN containing 'EC2-S3-DataProcessor'
aws s3 ls s3://my-lab-source-data
# Output should list the contents of the source bucket
aws s3 ls s3://my-lab-archive-data
# Output should be empty or return access denied if write permissions are strictly enforced on listing (though usually, read is allowed).
Step 4: Verification and Common Errors
It is vital to verify that the least-privilege constraints are working. Attempt to perform an action outside the scope of the policy. For example, try to delete a bucket or modify IAM users.
aws iam list-users --region us-east-1
Expected Error: You should receive an AccessDeniedException. This confirms that the role attached to the instance does not have iam:* permissions, successfully enforcing least privilege.
Common Mistakes:
* Granting Admin Access by Accident: A frequent error is selecting “AdministratorAccess” in the AWS Console when creating a new policy. Always create a custom inline policy or use specific managed policies like AmazonS3ReadOnlyAccess.
* Missing Trust Relationship: If you define an IAM role but do not set the trust relationship to allow ec2.amazonaws.com, the EC2 instance will fail to assume the role, resulting in “No profile found” errors.
* Ignoring SCPs (Service Control Policies): In enterprise environments, you might use SCPs at the Organization level to block certain actions entirely. Even with a permissive IAM policy, an SCP denying s3:DeleteBucket would override it. Always check your organizational policies.
Student Practice Task and Industry Application
Student Task: Create a second S3 bucket named my-lab-logs. Update the IAM role’s policy to allow s3:GetObject on this new bucket but explicitly deny all other actions using an explicit deny statement in a separate policy attached to a different user for comparison. Write a script that attempts to list the logs bucket with both the EC2 instance credentials and a standard admin user, documenting the difference in access levels.
Industry Application: In production environments, this pattern is used for “Compute-to-Storage” separation. By keeping compute roles minimal and storage policies granular, organizations reduce the blast radius of a compromised container or server. If an attacker exploits a vulnerability in your web application running on EC2, they cannot pivot to delete your entire S3 data lake because the role lacks s3:* permissions beyond specific read actions.
Conclusion and Final Checklist
Implementing least-privilege roles for cross-service access is a foundational skill for any cloud practitioner. It requires moving away from broad admin accounts toward granular, resource-specific policies. Remember that terms, availability, eligibility, laws, and prices depend on the specific insurer, policy, and jurisdiction if you are integrating this with insurance-backed cloud contracts, though here we focus strictly on technical implementation.
Final Security Checklist:
1. [ ] Did I define a trust relationship allowing only ec2.amazonaws.com?
2. [ ] Is the attached policy scoped to specific resource ARNs (e.g., arn:aws:s3:::my-bucket/*)?
3. [ ] Have I removed any * (wildcard) actions from the policy unless absolutely necessary?
4. [ ] Did I test the role by launching an instance and running aws sts get-caller-identity?
5. [ ] Have I verified that denied actions return AccessDeniedException as expected?
By following these steps, you ensure your AWS infrastructure remains secure, compliant, and resilient against potential threats.
Educational-use notice: This tutorial is for learning and controlled lab use only. Test carefully, use backups, and obtain authorization before making changes. Do not run unfamiliar commands on production systems. We accept no responsibility for misuse, damage, data loss, or production issues.

