class ManagedPolicy (construct)
| Language | Type name |
|---|---|
.NET | Amazon.CDK.AWS.IAM.ManagedPolicy |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awsiam#ManagedPolicy |
Java | software.amazon.awscdk.services.iam.ManagedPolicy |
Python | aws_cdk.aws_iam.ManagedPolicy |
TypeScript (source) | aws-cdk-lib » aws_iam » ManagedPolicy |
Implements
IConstruct, IDependable, IResource, IEnvironment, IManaged, IManaged, IGrantable
Managed policy.
Example
declare const vpc: ec2.Vpc;
const role = new iam.Role(this, 'RDSDirectoryServicesRole', {
assumedBy: new iam.CompositePrincipal(
new iam.ServicePrincipal('rds.amazonaws.com'),
new iam.ServicePrincipal('directoryservice.rds.amazonaws.com'),
),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonRDSDirectoryServiceAccess'),
],
});
const instance = new rds.DatabaseInstance(this, 'Instance', {
engine: rds.DatabaseInstanceEngine.mysql({ version: rds.MysqlEngineVersion.VER_8_0_19 }),
vpc,
domain: 'd-????????', // The ID of the domain for the instance to join.
domainRole: role, // Optional - will be create automatically if not provided.
});
Initializer
new ManagedPolicy(scope: Construct, id: string, props?: ManagedPolicyProps)
Parameters
- scope
Construct - id
string - props
ManagedPolicy Props
Construct Props
| Name | Type | Description |
|---|---|---|
| description? | string | A description of the managed policy. |
| document? | Policy | Initial PolicyDocument to use for this ManagedPolicy. |
| groups? | IGroup[] | Groups to attach this policy to. |
| managed | string | The name of the managed policy. |
| path? | string | The path for the policy. |
| roles? | IRole[] | Roles to attach this policy to. |
| statements? | Policy[] | Initial set of permissions to add to this policy document. |
| users? | IUser[] | Users to attach this policy to. |
description?
Type:
string
(optional, default: empty)
A description of the managed policy.
Typically used to store information about the permissions defined in the policy. For example, "Grants access to production DynamoDB tables." The policy description is immutable. After a value is assigned, it cannot be changed.
document?
Type:
Policy
(optional, default: An empty policy.)
Initial PolicyDocument to use for this ManagedPolicy.
If omited, any
PolicyStatement provided in the statements property will be applied
against the empty default PolicyDocument.
groups?
Type:
IGroup[]
(optional, default: No groups.)
Groups to attach this policy to.
You can also use attachToGroup(group) to attach this policy to a group.
managedPolicyName?
Type:
string
(optional, default: A name is automatically generated.)
The name of the managed policy.
If you specify multiple policies for an entity, specify unique names. For example, if you specify a list of policies for an IAM role, each policy must have a unique name.
path?
Type:
string
(optional, default: "/")
The path for the policy.
This parameter allows (through its regex pattern) a string of characters consisting of either a forward slash (/) by itself or a string that must begin and end with forward slashes. In addition, it can contain any ASCII character from the ! (\u0021) through the DEL character (\u007F), including most punctuation characters, digits, and upper and lowercased letters.
For more information about paths, see IAM Identifiers in the IAM User Guide.
roles?
Type:
IRole[]
(optional, default: No roles.)
Roles to attach this policy to.
You can also use attachToRole(role) to attach this policy to a role.
statements?
Type:
Policy[]
(optional, default: No statements.)
Initial set of permissions to add to this policy document.
You can also use addPermission(statement) to add permissions later.
users?
Type:
IUser[]
(optional, default: No users.)
Users to attach this policy to.
You can also use attachToUser(user) to attach this policy to a user.
Properties
| Name | Type | Description |
|---|---|---|
| description | string | The description of this policy. |
| document | Policy | The policy document. |
| env | Resource | The environment this resource belongs to. |
| grant | IPrincipal | The principal to grant permissions to. |
| managed | string | Returns the ARN of this managed policy. |
| managed | string | The name of this policy. |
| managed | Managed | A reference to a ManagedPolicy resource. |
| node | Node | The tree node. |
| path | string | The path of this policy. |
| stack | Stack | The stack in which this resource is defined. |
| static PROPERTY_INJECTION_ID | string | Uniquely identifies this class. |
description
Type:
string
The description of this policy.
document
Type:
Policy
The policy document.
env
Type:
Resource
The environment this resource belongs to.
For resources that are created and managed in a Stack (those created by
creating new class instances like new Role(), new Bucket(), etc.), this
is always the same as the environment of the stack they belong to.
For referenced resources (those obtained from referencing methods like
Role.fromRoleArn(), Bucket.fromBucketName(), etc.), they might be
different than the stack they were imported into.
grantPrincipal
Type:
IPrincipal
The principal to grant permissions to.
managedPolicyArn
Type:
string
Returns the ARN of this managed policy.
managedPolicyName
Type:
string
The name of this policy.
managedPolicyRef
Type:
Managed
A reference to a ManagedPolicy resource.
node
Type:
Node
The tree node.
path
Type:
string
The path of this policy.
stack
Type:
Stack
The stack in which this resource is defined.
static PROPERTY_INJECTION_ID
Type:
string
Uniquely identifies this class.
Methods
| Name | Description |
|---|---|
| add | Adds a statement to the policy document. |
| apply | Apply the given removal policy to this resource. |
| attach | Attaches this policy to a group. |
| attach | Attaches this policy to a role. |
| attach | Attaches this policy to a user. |
| to | Returns a string representation of this construct. |
| static from | Import a managed policy from one of the policies that AWS manages. |
| static from | Import an external managed policy by ARN. |
| static from | Import a customer managed policy from the managedPolicyName. |
addStatements(...statement)
public addStatements(...statement: PolicyStatement[]): void
Parameters
- statement
PolicyStatement
Adds a statement to the policy document.
applyRemovalPolicy(policy)
public applyRemovalPolicy(policy: RemovalPolicy): void
Parameters
- policy
RemovalPolicy
Apply the given removal policy to this resource.
The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.
The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS
account for data recovery and cleanup later (RemovalPolicy.RETAIN).
attachToGroup(group)
public attachToGroup(group: IGroupRef): void
Parameters
- group
IGroupRef
Attaches this policy to a group.
attachToRole(role)
public attachToRole(role: IRole): void
Parameters
- role
IRole
Attaches this policy to a role.
attachToUser(user)
public attachToUser(user: IUserRef): void
Parameters
- user
IUserRef
Attaches this policy to a user.
toString()
public toString(): string
Returns
string
Returns a string representation of this construct.
static fromAwsManagedPolicyName(managedPolicyName)
public static fromAwsManagedPolicyName(managedPolicyName: string): IManagedPolicy
Parameters
- managedPolicyName
string
Returns
Import a managed policy from one of the policies that AWS manages.
For this managed policy, you only need to know the name to be able to use it.
Some managed policy names start with "service-role/", some start with "job-function/", and some don't start with anything. Include the prefix when constructing this object.
static fromManagedPolicyArn(scope, id, managedPolicyArn)
public static fromManagedPolicyArn(scope: Construct, id: string, managedPolicyArn: string): IManagedPolicy
Parameters
- scope
Construct— construct scope. - id
string— construct id. - managedPolicyArn
string— the ARN of the managed policy to import.
Returns
Import an external managed policy by ARN.
For this managed policy, you only need to know the ARN to be able to use it. This can be useful if you got the ARN from a CloudFormation Export.
If the imported Managed Policy ARN is a Token (such as a
CfnParameter.valueAsString or a Fn.importValue()) and the referenced
managed policy has a path (like arn:...:policy/AdminPolicy/AdminAllow), the
managedPolicyName property will not resolve to the correct value. Instead it
will resolve to the first path component. We unfortunately cannot express
the correct calculation of the full path name as a CloudFormation
expression. In this scenario the Managed Policy ARN should be supplied without the
path in order to resolve the correct managed policy resource.
static fromManagedPolicyName(scope, id, managedPolicyName)
public static fromManagedPolicyName(scope: Construct, id: string, managedPolicyName: string): IManagedPolicy
Parameters
- scope
Construct - id
string - managedPolicyName
string
Returns
Import a customer managed policy from the managedPolicyName.
For this managed policy, you only need to know the name to be able to use it.

.NET
Go
Java
Python
TypeScript (