CDK routines for easily assigning correct and minimal IAM permissions
Project description
AWS IAM Construct Library
Define a role and add permissions to it. This will automatically create and attach an IAM policy to the role:
const role = new Role(this, 'MyRole', {
assumedBy: new ServicePrincipal('sns.amazonaws.com')
});
role.addToPolicy(new PolicyStatement()
.addAllResources()
.addAction('lambda:InvokeFunction'));
Define a policy and attach it to groups, users and roles. Note that it is possible to attach
the policy either by calling xxx.attachInlinePolicy(policy) or policy.attachToXxx(xxx).
const user = new User(this, 'MyUser', { password: '1234' });
const group = new Group(this, 'MyGroup');
const policy = new Policy(this, 'MyPolicy');
policy.attachToUser(user);
group.attachInlinePolicy(policy);
Managed policies can be attached using xxx.attachManagedPolicy(arn):
const group = new Group(this, 'MyGroup');
group.attachManagedPolicy('arn:aws:iam::aws:policy/AdministratorAccess');
Configuring an ExternalId
If you need to create roles that will be assumed by 3rd parties, it is generally a good idea to require an ExternalId
to assume them. Configuring
an ExternalId works like this:
const role = new iam.Role(this, 'MyRole', {
assumedBy: new iam.AccountPrincipal('123456789012'),
externalId: 'SUPPLY-ME',
});
IAM Principals
When defining policy statements as part of an AssumeRole policy or as part of a
resource policy, statements would usually refer to a specific IAM principal
under Principal.
IAM principals are modeled as classes that derive from the iam.PolicyPrincipal
abstract class. Principal objects include principal type (string) and value
(array of string), optional set of conditions and the action that this principal
requires when it is used in an assume role policy document.
To add a principal to a policy statement you can either use the abstract
statement.addPrincipal, one of the concrete addXxxPrincipal methods:
addAwsPrincipal,addArnPrincipalornew ArnPrincipal(arn)for{ "AWS": arn }addAwsAccountPrincipalornew AccountPrincipal(accountId)for{ "AWS": account-arn }addServicePrincipalornew ServicePrincipal(service)for{ "Service": service }addAccountRootPrincipalornew AccountRootPrincipal()for{ "AWS": { "Ref: "AWS::AccountId" } }addCanonicalUserPrincipalornew CanonicalUserPrincipal(id)for{ "CanonicalUser": id }addFederatedPrincipalornew FederatedPrincipal(federated, conditions, assumeAction)for{ "Federated": arn }and a set of optional conditions and the assume role action to use.addAnyPrincipalornew AnyPrincipalfor{ "AWS": "*" }
If multiple principals are added to the policy statement, they will be merged together:
const statement = new PolicyStatement();
statement.addServicePrincipal('cloudwatch.amazonaws.com');
statement.addServicePrincipal('ec2.amazonaws.com');
statement.addAwsPrincipal('arn:aws:boom:boom');
Will result in:
{
"Principal": {
"Service": [ "cloudwatch.amazonaws.com", "ec2.amazonaws.com" ],
"AWS": "arn:aws:boom:boom"
}
}
The CompositePrincipal class can also be used to define complex principals, for example:
const role = new iam.Role(this, 'MyRole', {
assumedBy: new iam.CompositePrincipal(
new iam.ServicePrincipal('ec2.amazonawas.com'),
new iam.AccountPrincipal('1818188181818187272')
)
});
Features
- Policy name uniqueness is enforced. If two policies by the same name are attached to the same principal, the attachment will fail.
- Policy names are not required - the CDK logical ID will be used and ensured to be unique.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aws-cdk.aws-iam-0.27.0.tar.gz.
File metadata
- Download URL: aws-cdk.aws-iam-0.27.0.tar.gz
- Upload date:
- Size: 125.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c7d370ea5fb47440521752f48cdd49004d0a177a8bb65abef995415456355de
|
|
| MD5 |
12aaf39b78036c888831f086fe15c004
|
|
| BLAKE2b-256 |
851b5b52600c54a8a1b9cb5bfce662cd1fa5c57ce92aa079ac010ee4a05e1a51
|
File details
Details for the file aws_cdk.aws_iam-0.27.0-py3-none-any.whl.
File metadata
- Download URL: aws_cdk.aws_iam-0.27.0-py3-none-any.whl
- Upload date:
- Size: 123.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2f594f0310084e5ba6f6add86b8ba6992db637cfcda496739c52e9e8880ee91
|
|
| MD5 |
95f71994d6928814c0c842b5cfd81c0a
|
|
| BLAKE2b-256 |
61a4249fad66833ce8b3a4ad633417dc6943489c1f15393e458b18012d86fe0e
|