Package Exports
- @aws-cdk/aws-iam
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@aws-cdk/aws-iam) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
AWS IAM Construct Library
Define a role and add permissions to it. This will automatically create and attach an IAM policy to the role:
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).
attaching policies to user and group
Managed policies can be attached using xxx.attachManagedPolicy(arn):
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:
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.