Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS - IAM Roles and Trust Relationships

Tags:

aws-iam

I an new to AWS and IAM and trying to understand roles and trust relationship.

I fully understand why roles are used, how to create them and their use case.

What I don't get is the trust relationship step. In almost all the cases I have seen it is a one to one relationship. EC2 needs a trust with EC2. Why is there the extra step?

If I create an EC2 instance and a role that has S3 permissions why isn't that enough?

like image 212
MangoGuy Avatar asked Aug 25 '18 01:08

MangoGuy


People also ask

What is trust relationship in IAM roles?

Trust relationship – This policy defines which principals can assume the role, and under which conditions. This is sometimes referred to as a resource-based policy for the IAM role.

How do I add a role to my trust relationship in AWS?

In the navigation pane of the IAM console, choose Roles. The console displays the roles for your account. Choose the name of the role that you want to modify, and select the Trust relationships tab on the details page. Choose Edit trust relationship.

What are the type of trusted entities that can be used in AWS IAM roles?

Each role has a set of permissions for making AWS service requests, and a role is not associated with a specific user or group. Instead, trusted entities such as identity providers or AWS services assume roles. For more information, see IAM roles.

What is a role Trust policy in AWS IAM service what permissions does it define to what it is attached?

A role trust policy is a required resource-based policy that is attached to a role in IAM. The principals that you can specify in the trust policy include users, roles, accounts, and services. Permissions policy. A permissions document in JSON format in which you define what actions and resources the role can use.


2 Answers

Roles are used to grant specific privileges to specific actors for a set duration of time. So, a role needs two things: permission policies (what resources can be accessed and what actions can be taken) and a trust policy (what entities can assume the role).

For example, the following CloudFormation snippet creates a role (MyInstanceRole) with a policy (MyWritePolicy) giving access to an S3 bucket and allows EC2 instances (the Principal, or the trust part) to assume the role:

MyInstanceRole:
Type: AWS::IAM::Role
Properties:
  AssumeRolePolicyDocument:
    Version: 2012-10-17
    Statement:
    - Effect: Allow
      Action: sts:AssumeRole
      Principal:
        Service: ec2.amazonaws.com
  Path: '/' 
  RoleName: MyInstanceRole
  Policies:
  - PolicyName: MyWritePolicy
    PolicyDocument:
      Version: 2012-10-17
      Statement:
      - Sid: WriteBackups
        Action: 
        - s3:PutObject
        Effect: Allow
        Resource: !Join ['', ['arn:aws:s3:::', !Join [ '-', [ 'bucketName', !Ref Environment ] ], '/*' ] ]

In many cases there will be just a single Principal, but there can be more than one (AWS account, IAM user, IAM role, federated user, or assumed-role user) if required.

There's a handy blog post at Now Create and Manage AWS IAM Roles More Easily with the Updated IAM Console that gives some more details.

like image 70
craigcaulfield Avatar answered Sep 21 '22 00:09

craigcaulfield


TLDR: Think of aws trusted relations as which aws service can implement (assume role) the permissions you giving.

Quick example: If I've created a role which contains permissions to read bucket from s3 and ec2 is trusted relations in this role, only ec2 instances can implement this role and can have access to this s3 bucket. rds for example can't assume this role and therefore can't. You grant permissions x that only aws service y can use them.

Let me explain it with some easy use case:

I want to be able to download some configuration file from s3 bucket into my web application, the web application runs on ec2 instance and the s3 bucket name is "configuration-for-app"

I'm creating a role named "my-app-role" which contains several policies ,one of them is s3 policy that can access my s3 amazon resource "configuration-for-app" and has explicit permission to get it only (not delete i, not changing it - just get it). Since the app runs on ec2 - the trusted relations in this requirements between these services would be <ec2> -> <s3> ,my application that runs on ec2 can assume that role (my-app-role) and accessing (with the correct policy in it) to s3 and get the configuration file.

The role contains this policy:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "s3:GetObject"
      "Resource": "arn:aws:s3:::configuration-for-app/*"
    }
  ]
}

The trusted policy would be:

    {
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

I grant permissions (assume-role of "my-app-role") <x> to service <y> (my ec2 instance that runs my applications) in order to accomplish operation<z> (get the s3 configuration file from bucket "configuration-for-app" the role contains this specific s3 policy).

Important - If different service in aws ( like rds / elasticsearch / amplify etc ...) wants assume this role and get the configuration file of this app it's impossible because only ec2 instances on this example has the right trusted policy.

like image 37
avivamg Avatar answered Sep 20 '22 00:09

avivamg