AWS IAM
Table of Contents
Reference
assume-role-policy
- User (Replace
root
with an user name if restrict the role for a specific user)
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": {"AWS": "arn:aws:iam::123456789012:root"},
"Action": "sts:AssumeRole",
}
}
- EC2
To attach the role to EC2 instances, an instance profile must be created.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}
]
}
- Lambda
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {"Service": "lambda.amazonaws.com"},
"Action": "sts:AssumeRole"
}
]
}
kinesis
- Readonly
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kinesis:Get*",
"kinesis:DescribeStream"
],
"Resource": [
"arn:aws:kinesis:us-east-1:111122223333:stream/stream1"
]
},
{
"Effect": "Allow",
"Action": [
"kinesis:ListStreams"
],
"Resource": [
"*"
]
}
]
}
- Writeonly
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kinesis:PutRecord"
],
"Resource": [
"arn:aws:kinesis:us-east-1:111122223333:stream/*"
]
}
]
}
- Fullaccess
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "kinesis:*",
"Resource": [
"arn:aws:kinesis:us-east-1:111122223333:stream/stream1"
]
}
]
}
- Fullaccess on all streams
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "kinesis:*",
"Resource": [
"arn:aws:kinesis:*:111122223333:stream/*"
]
}
]
}
s3
- Programmatic Access
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["arn:aws:s3:::test"]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": ["arn:aws:s3:::test/*"]
}
]
}
- AWS Console Access
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:ListAllMyBuckets"
],
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["arn:aws:s3:::test"]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": ["arn:aws:s3:::test/*"]
}
]
}
Topics
IAM Role
- With some AWS SDKs, can use temporary credentials transparently.
- Alternatively, can get the temporary credentials directly from the EC2 instance metadata
Assume Role Policy
Assume Role Policy
describes how the role is used for. This is related to AWS STS(Security Token Service); Following is an example on a role for EC2:
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}
}
And it also requires its actual permissions:
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::example_bucket"
}
}
Instance Profiles
The instance profile contains the role and can provide the role's credentials to an application that runs on the instance. If you use the IAM console, the instance profile is managed for you and is mostly transparent to you. if you use the AWS CLI or API to create and manage the role and EC2 instance, then you must create the instance profile and assign the role to it as separate steps. Then, when you launch the instance, you must specify the instance profile name instead of the role name.
Policy Elements
Id
- An optional identifier
- Using a UUID for the value is recommended
- Services like SQS or SNS might require this
"Id": "cd3ad3d9-2776-4ef1-a904-4c229d1642ee"
Statement
- Required
- Contains an array of individual statements
Effect
- Either
Allow
orDeny
Sid
- An optional identifier
- The
Sid
value must be unique within a policy
Principal
To specify the user (IAM user, federated user, or assumed-role user), AWS account, AWS service, or other principal entity that is allowed or denied access to a resource.
Action
Resource
Not-
NotPrincipal, NotAction, NotResource are dvanced policy elements that explicitly matches everything except the specified list of the elements.
Condition
"Condition" : {
"DateGreaterThan" : {
"aws:CurrentTime" : "2013-08-16T12:00:00Z"
},
"DateLessThan": {
"aws:CurrentTime" : "2013-08-16T15:00:00Z"
},
"IpAddress" : {
"aws:SourceIp" : ["192.0.2.0/24", "203.0.113.0/24"]
}
}
The two values for aws:SourceIp
are evaluated using OR. The three separate condition operators are evaluated using AND.
Policies Evaluation Logic
The enforcement code evaluates all user-based and resource-based policies that are applicable to the request(based on the resource, principal, action, and conditions). The order in which the enforcement code evaluates the policies is not important.
How-to
Share resources with an external AWS account via IAM Role
This creates an assume role policy like this:
Principal
is 3rd party's AWS account id. For example:
"AWS": "arn:aws:iam::123456789012:root"
But ironically, it's unable to specify an ExternalID
when switching roles on AWS Console. They only can be specified through awscli or other programatic accesses.
An easy way to work around this is just delete that yellow line. This allows all users belong to target AWS Account to switch to the role.