Class Role
- All Implemented Interfaces:
IResource,IGrantable,IIdentity,IPrincipal,IRole,IRoleRef,software.amazon.jsii.JsiiSerializable,software.constructs.IConstruct,software.constructs.IDependable
Defines an IAM role. The role is created with an assume policy document associated with
the specified AWS service principal defined in serviceAssumeRole.
Example:
// Create a custom execution role
Role executionRole = Role.Builder.create(this, "BrowserExecutionRole")
.assumedBy(new ServicePrincipal("bedrock-agentcore.amazonaws.com"))
.managedPolicies(List.of(ManagedPolicy.fromAwsManagedPolicyName("AmazonBedrockAgentCoreBrowserExecutionRolePolicy")))
.build();
// Create browser with custom execution role
BrowserCustom browser = BrowserCustom.Builder.create(this, "MyBrowser")
.browserCustomName("my_browser")
.description("Browser with custom execution role")
.networkConfiguration(BrowserNetworkConfiguration.usingPublicNetwork())
.executionRole(executionRole)
.build();
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class software.amazon.jsii.JsiiObject
software.amazon.jsii.JsiiObject.InitializationModeNested classes/interfaces inherited from interface software.constructs.IConstruct
software.constructs.IConstruct.Jsii$DefaultNested classes/interfaces inherited from interface software.amazon.awscdk.IResource
IResource.Jsii$DefaultNested classes/interfaces inherited from interface software.amazon.awscdk.services.iam.IRole
IRole.Jsii$Default, IRole.Jsii$Proxy -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddManagedPolicy(IManagedPolicy policy) Attaches a managed policy to this role.addToPolicy(PolicyStatement statement) Add to the policy of this principal.addToPrincipalPolicy(PolicyStatement statement) Adds a permission to the role's default policy document.voidapplyRemovalPolicy(RemovalPolicy policy) Skip applyRemovalPolicy if role synthesis is prevented by customizeRoles.voidattachInlinePolicy(Policy policy) Attaches a policy to this role.static voidcustomizeRoles(software.constructs.Construct scope) Customize the creation of IAM roles within the given scope.static voidcustomizeRoles(software.constructs.Construct scope, CustomizeRolesOptions options) Customize the creation of IAM roles within the given scope.static IRolefromLookup(software.constructs.Construct scope, String id, RoleLookupOptions options) Lookup an existing Role.static IRolefromRoleArn(software.constructs.Construct scope, String id, String roleArn) Import an external role by ARN.static IRolefromRoleArn(software.constructs.Construct scope, String id, String roleArn, FromRoleArnOptions options) Import an external role by ARN.static IRolefromRoleName(software.constructs.Construct scope, String id, String roleName) Import an external role by name.static IRolefromRoleName(software.constructs.Construct scope, String id, String roleName, FromRoleNameOptions options) Import an external role by name.When this Principal is used in an AssumeRole policy, the action to use.The assume role policy document associated with this role.The principal to grant permissions to.Returns the permissions boundary attached to this role.Returns the role.The AWS account ID of this principal.Returns the ARN of this role.Returns the stable and unique string identifying the role.Returns the name of the role.A reference to a Role resource.grant(IPrincipal grantee, String... actions) Grant the actions defined in actions to the identity Principal on this resource.grantAssumeRole(IPrincipal identity) Grant permissions to the given principal to assume this role.grantPassRole(IPrincipal identity) Grant permissions to the given principal to pass this role.static BooleanReturn whether the given object is a Role.Return a copy of this Role object whose Policies will not be updated.Return a copy of this Role object whose Policies will not be updated.Methods inherited from class software.amazon.awscdk.Resource
generatePhysicalName, getEnv, getPhysicalName, getResourceArnAttribute, getResourceNameAttribute, getStack, isOwnedResource, isResourceMethods inherited from class software.constructs.Construct
getNode, isConstruct, toStringMethods inherited from class software.amazon.jsii.JsiiObject
jsiiAsyncCall, jsiiAsyncCall, jsiiCall, jsiiCall, jsiiGet, jsiiGet, jsiiSet, jsiiStaticCall, jsiiStaticCall, jsiiStaticGet, jsiiStaticGet, jsiiStaticSet, jsiiStaticSetMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface software.amazon.jsii.JsiiSerializable
$jsii$toJson
-
Field Details
-
PROPERTY_INJECTION_ID
Uniquely identifies this class.
-
-
Constructor Details
-
Role
protected Role(software.amazon.jsii.JsiiObjectRef objRef) -
Role
protected Role(software.amazon.jsii.JsiiObject.InitializationMode initializationMode) -
Role
@Stability(Stable) public Role(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull RoleProps props) - Parameters:
scope- This parameter is required.id- This parameter is required.props- This parameter is required.
-
-
Method Details
-
customizeRoles
@Stability(Stable) public static void customizeRoles(@NotNull software.constructs.Construct scope, @Nullable CustomizeRolesOptions options) Customize the creation of IAM roles within the given scope.It is recommended that you do not use this method and instead allow CDK to manage role creation. This should only be used in environments where CDK applications are not allowed to created IAM roles.
This can be used to prevent the CDK application from creating roles within the given scope and instead replace the references to the roles with precreated role names. A report will be synthesized in the cloud assembly (i.e. cdk.out) that will contain the list of IAM roles that would have been created along with the IAM policy statements that the role should contain. This report can then be used to create the IAM roles outside of CDK and then the created role names can be provided in
usePrecreatedRoles.Example:
App app; Role.customizeRoles(app, CustomizeRolesOptions.builder() .usePrecreatedRoles(Map.of( "ConstructPath/To/Role", "my-precreated-role-name")) .build());- Parameters:
scope- construct scope to customize role creation. This parameter is required.options- options for configuring role creation.
-
customizeRoles
@Stability(Stable) public static void customizeRoles(@NotNull software.constructs.Construct scope) Customize the creation of IAM roles within the given scope.It is recommended that you do not use this method and instead allow CDK to manage role creation. This should only be used in environments where CDK applications are not allowed to created IAM roles.
This can be used to prevent the CDK application from creating roles within the given scope and instead replace the references to the roles with precreated role names. A report will be synthesized in the cloud assembly (i.e. cdk.out) that will contain the list of IAM roles that would have been created along with the IAM policy statements that the role should contain. This report can then be used to create the IAM roles outside of CDK and then the created role names can be provided in
usePrecreatedRoles.Example:
App app; Role.customizeRoles(app, CustomizeRolesOptions.builder() .usePrecreatedRoles(Map.of( "ConstructPath/To/Role", "my-precreated-role-name")) .build());- Parameters:
scope- construct scope to customize role creation. This parameter is required.
-
fromLookup
@Stability(Stable) @NotNull public static IRole fromLookup(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull RoleLookupOptions options) Lookup an existing Role.- Parameters:
scope- This parameter is required.id- This parameter is required.options- This parameter is required.
-
fromRoleArn
@Stability(Stable) @NotNull public static IRole fromRoleArn(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull String roleArn, @Nullable FromRoleArnOptions options) Import an external role by ARN.If the imported Role ARN is a Token (such as a
CfnParameter.valueAsStringor aFn.importValue()) and the referenced role has apath(likearn:...:role/AdminRoles/Alice), theroleNameproperty 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 Role ARN should be supplied without thepathin order to resolve the correct role resource.- Parameters:
scope- construct scope. This parameter is required.id- construct id. This parameter is required.roleArn- the ARN of the role to import. This parameter is required.options- allow customizing the behavior of the returned role.
-
fromRoleArn
@Stability(Stable) @NotNull public static IRole fromRoleArn(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull String roleArn) Import an external role by ARN.If the imported Role ARN is a Token (such as a
CfnParameter.valueAsStringor aFn.importValue()) and the referenced role has apath(likearn:...:role/AdminRoles/Alice), theroleNameproperty 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 Role ARN should be supplied without thepathin order to resolve the correct role resource.- Parameters:
scope- construct scope. This parameter is required.id- construct id. This parameter is required.roleArn- the ARN of the role to import. This parameter is required.
-
fromRoleName
@Stability(Stable) @NotNull public static IRole fromRoleName(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull String roleName, @Nullable FromRoleNameOptions options) Import an external role by name.The imported role is assumed to exist in the same account as the account the scope's containing Stack is being deployed to.
- Parameters:
scope- construct scope. This parameter is required.id- construct id. This parameter is required.roleName- the name of the role to import. This parameter is required.options- allow customizing the behavior of the returned role.
-
fromRoleName
@Stability(Stable) @NotNull public static IRole fromRoleName(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull String roleName) Import an external role by name.The imported role is assumed to exist in the same account as the account the scope's containing Stack is being deployed to.
- Parameters:
scope- construct scope. This parameter is required.id- construct id. This parameter is required.roleName- the name of the role to import. This parameter is required.
-
isRole
Return whether the given object is a Role.- Parameters:
x- This parameter is required.
-
addManagedPolicy
Attaches a managed policy to this role.- Specified by:
addManagedPolicyin interfaceIIdentity- Parameters:
policy- The the managed policy to attach. This parameter is required.
-
addToPolicy
Add to the policy of this principal.- Parameters:
statement- This parameter is required.
-
addToPrincipalPolicy
@Stability(Stable) @NotNull public AddToPrincipalPolicyResult addToPrincipalPolicy(@NotNull PolicyStatement statement) Adds a permission to the role's default policy document.If there is no default policy attached to this role, it will be created.
- Specified by:
addToPrincipalPolicyin interfaceIPrincipal- Parameters:
statement- The permission statement to add to the policy document. This parameter is required.
-
applyRemovalPolicy
Skip applyRemovalPolicy if role synthesis is prevented by customizeRoles.Because in this case, this construct does not have a CfnResource in the tree.
- Specified by:
applyRemovalPolicyin interfaceIResource- Overrides:
applyRemovalPolicyin classResource- Parameters:
policy- RemovalPolicy. This parameter is required.
-
attachInlinePolicy
Attaches a policy to this role.- Specified by:
attachInlinePolicyin interfaceIIdentity- Parameters:
policy- The policy to attach. This parameter is required.
-
grant
@Stability(Stable) @NotNull public Grant grant(@NotNull IPrincipal grantee, @NotNull String... actions) Grant the actions defined in actions to the identity Principal on this resource. -
grantAssumeRole
Grant permissions to the given principal to assume this role.- Specified by:
grantAssumeRolein interfaceIRole- Parameters:
identity- This parameter is required.
-
grantPassRole
Grant permissions to the given principal to pass this role.- Specified by:
grantPassRolein interfaceIRole- Parameters:
identity- This parameter is required.
-
withoutPolicyUpdates
@Stability(Stable) @NotNull public IRole withoutPolicyUpdates(@Nullable WithoutPolicyUpdatesOptions options) Return a copy of this Role object whose Policies will not be updated.Use the object returned by this method if you want this Role to be used by a construct without it automatically updating the Role's Policies.
If you do, you are responsible for adding the correct statements to the Role's policies yourself.
- Parameters:
options-
-
withoutPolicyUpdates
Return a copy of this Role object whose Policies will not be updated.Use the object returned by this method if you want this Role to be used by a construct without it automatically updating the Role's Policies.
If you do, you are responsible for adding the correct statements to the Role's policies yourself.
-
getAssumeRoleAction
When this Principal is used in an AssumeRole policy, the action to use.- Specified by:
getAssumeRoleActionin interfaceIPrincipal
-
getGrantPrincipal
The principal to grant permissions to.- Specified by:
getGrantPrincipalin interfaceIGrantable
-
getPolicyFragment
Returns the role.- Specified by:
getPolicyFragmentin interfaceIPrincipal
-
getRoleArn
Returns the ARN of this role.- Specified by:
getRoleArnin interfaceIRole
-
getRoleId
Returns the stable and unique string identifying the role.For example, AIDAJQABLZS4A3QDU576Q.
-
getRoleName
Returns the name of the role.- Specified by:
getRoleNamein interfaceIRole
-
getRoleRef
A reference to a Role resource.- Specified by:
getRoleRefin interfaceIRoleRef
-
getAssumeRolePolicy
The assume role policy document associated with this role. -
getPermissionsBoundary
Returns the permissions boundary attached to this role. -
getPrincipalAccount
The AWS account ID of this principal.Can be undefined when the account is not known (for example, for service principals). Can be a Token - in that case, it's assumed to be AWS::AccountId.
- Specified by:
getPrincipalAccountin interfaceIPrincipal
-