PolicyDocument Class
A PolicyDocument is a collection of statements.
Example
import * as iam from 'aws-cdk-lib/aws-iam';
const myFileSystemPolicy = new iam.PolicyDocument({
statements: [new iam.PolicyStatement({
actions: [
'elasticfilesystem:ClientWrite',
'elasticfilesystem:ClientMount',
],
principals: [new iam.AccountRootPrincipal()],
resources: ['*'],
conditions: {
Bool: {
'elasticfilesystem:AccessedViaMountTarget': 'true',
},
},
})],
});
const fileSystem = new efs.FileSystem(this, 'MyEfsFileSystem', {
vpc: new ec2.Vpc(this, 'VPC'),
fileSystemPolicy: myFileSystemPolicy,
});
import aws_cdk.aws_iam as iam
my_file_system_policy = iam.PolicyDocument(
statements=[iam.PolicyStatement(
actions=["elasticfilesystem:ClientWrite", "elasticfilesystem:ClientMount"
],
principals=[iam.AccountRootPrincipal()],
resources=["*"],
conditions={
"Bool": {
"elasticfilesystem:AccessedViaMountTarget": "true"
}
}
)]
)
file_system = efs.FileSystem(self, "MyEfsFileSystem",
vpc=ec2.Vpc(self, "VPC"),
file_system_policy=my_file_system_policy
)
import software.amazon.awscdk.services.iam.*;
PolicyDocument myFileSystemPolicy = PolicyDocument.Builder.create()
.statements(List.of(PolicyStatement.Builder.create()
.actions(List.of("elasticfilesystem:ClientWrite", "elasticfilesystem:ClientMount"))
.principals(List.of(new AccountRootPrincipal()))
.resources(List.of("*"))
.conditions(Map.of(
"Bool", Map.of(
"elasticfilesystem:AccessedViaMountTarget", "true")))
.build()))
.build();
FileSystem fileSystem = FileSystem.Builder.create(this, "MyEfsFileSystem")
.vpc(new Vpc(this, "VPC"))
.fileSystemPolicy(myFileSystemPolicy)
.build();
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.IAM;
var myFileSystemPolicy = new PolicyDocument(new PolicyDocumentProps {
Statements = new [] { new PolicyStatement(new PolicyStatementProps {
Actions = new [] { "elasticfilesystem:ClientWrite", "elasticfilesystem:ClientMount" },
Principals = new [] { new AccountRootPrincipal() },
Resources = new [] { "*" },
Conditions = new Dictionary<string, object> {
{ "Bool", new Dictionary<string, string> {
{ "elasticfilesystem:AccessedViaMountTarget", "true" }
} }
}
}) }
});
var fileSystem = new FileSystem(this, "MyEfsFileSystem", new FileSystemProps {
Vpc = new Vpc(this, "VPC"),
FileSystemPolicy = myFileSystemPolicy
});
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"
myFileSystemPolicy := iam.NewPolicyDocument(&PolicyDocumentProps{
Statements: []PolicyStatement{
iam.NewPolicyStatement(&PolicyStatementProps{
Actions: []*string{
jsii.String("elasticfilesystem:ClientWrite"),
jsii.String("elasticfilesystem:ClientMount"),
},
Principals: []IPrincipal{
iam.NewAccountRootPrincipal(),
},
Resources: []*string{
jsii.String("*"),
},
Conditions: map[string]interface{}{
"Bool": map[string]*string{
"elasticfilesystem:AccessedViaMountTarget": jsii.String("true"),
},
},
}),
},
})
fileSystem := efs.NewFileSystem(this, jsii.String("MyEfsFileSystem"), &FileSystemProps{
Vpc: ec2.NewVpc(this, jsii.String("VPC")),
FileSystemPolicy: myFileSystemPolicy,
})
Initializer
new PolicyDocument(props?: PolicyDocumentProps)
PolicyDocument(props: typing.Optional[PolicyDocumentProps] = None)
new PolicyDocument(PolicyDocumentProps props)
new PolicyDocument(PolicyDocumentProps? props = null)
NewPolicyDocument(Props PolicyDocumentProps)
Properties
creationStackstring[]creation_stacktyping.List[str]creationStackList<String>CreationStackstring[]CreationStack*[]*stringrequired#This may return an array with a single informational element indicating how to get this property populated, if it was skipped for performance reasons.
statementCountnumberstatement_countint | floatstatementCountNumberStatementCountdoubleStatementCount*float64required#Can be used, for example, to generate unique "sid"s within the policy.
Methods
addStatements(...statement: PolicyStatement[]): voiddef add_statements(self, statement: PolicyStatement) -> Nonepublic void addStatements(PolicyStatement statement)public void AddStatements(PolicyStatement statement)func AddStatements(Statement PolicyStatement)#1 parameter
Parameters
statementstatementstatementstatementStatement— the statement to add.
fromJson(obj: any): PolicyDocument // chainable@staticmethod def from_json(obj: Any) -> PolicyDocumentpublic static PolicyDocument fromJson(Object obj)public static PolicyDocument FromJson(object obj)func FromJson(Obj interface{}) PolicyDocumentstatic#This will accept an object created from the .toJSON() call
1 parameter
Parameters
objobjobjobjObj— the PolicyDocument in object form.
resolve(context: IResolveContext): anydef resolve(self, context: IResolveContext) -> Anypublic Object resolve(IResolveContext context)public object Resolve(IResolveContext context)func Resolve(Context IResolveContext) interface{}#toJSON(): anydef to_json(self, ) -> Anypublic Object toJSON()public object ToJSON()func ToJSON() interface{}#Used when JSON.stringify() is called
toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#validateForAnyPolicy(): string[]def validate_for_any_policy(self, ) -> typing.List[str]public List<String> validateForAnyPolicy()public string[] ValidateForAnyPolicy()func ValidateForAnyPolicy() *[]*string#returns
Returns: An array of validation error messages, or an empty array if the document is valid.
validateForIdentityPolicy(): string[]def validate_for_identity_policy(self, ) -> typing.List[str]public List<String> validateForIdentityPolicy()public string[] ValidateForIdentityPolicy()func ValidateForIdentityPolicy() *[]*string#returns
Returns: An array of validation error messages, or an empty array if the document is valid.
validateForResourcePolicy(options?: ResourcePolicyValidationOptions): string[]def validate_for_resource_policy(self, options: typing.Optional[ResourcePolicyValidationOptions] = None) -> typing.List[str]public List<String> validateForResourcePolicy(ResourcePolicyValidationOptions options)public string[] ValidateForResourcePolicy(ResourcePolicyValidationOptions? options = null)func ValidateForResourcePolicy(Options ResourcePolicyValidationOptions) *[]*string#1 parameter · returns
Parameters
optionsoptionsoptionsoptionsOptions— Optional validation options.
Returns: An array of validation error messages, or an empty array if the document is valid.
validateForTrustPolicy(): string[]def validate_for_trust_policy(self, ) -> typing.List[str]public List<String> validateForTrustPolicy()public string[] ValidateForTrustPolicy()func ValidateForTrustPolicy() *[]*string#Trust policies are a special type of resource-based policy where the resource is implicit (the role itself).
returns
Returns: An array of validation error messages, or an empty array if the document is valid.