createPolicy method

Future<CreatePolicyResponse> createPolicy({
  1. required String policyDocument,
  2. required String policyName,
  3. String? description,
  4. String? path,
})

Creates a new managed policy for your AWS account.

This operation creates a policy version with a version identifier of v1 and sets v1 as the policy's default version. For more information about policy versions, see Versioning for Managed Policies in the IAM User Guide.

For more information about managed policies in general, see Managed Policies and Inline Policies in the IAM User Guide.

May throw InvalidInputException. May throw LimitExceededException. May throw EntityAlreadyExistsException. May throw MalformedPolicyDocumentException. May throw ServiceFailureException.

Parameter policyDocument : The JSON policy document that you want to use as the content for the new policy.

You must provide policies in JSON format in IAM. However, for AWS CloudFormation templates formatted in YAML, you can provide the policy in JSON or YAML format. AWS CloudFormation always converts a YAML policy to JSON format before submitting it to IAM.

The regex pattern used to validate this parameter is a string of characters consisting of the following:

  • Any printable ASCII character ranging from the space character (\u0020) through the end of the ASCII character range
  • The printable characters in the Basic Latin and Latin-1 Supplement character set (through \u00FF)
  • The special characters tab (\u0009), line feed (\u000A), and carriage return (\u000D)

Parameter policyName : The friendly name of the policy.

IAM user, group, role, and policy names must be unique within the account. Names are not distinguished by case. For example, you cannot create resources named both "MyResource" and "myresource".

Parameter description : A friendly description of the 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.

Parameter path : The path for the policy.

For more information about paths, see IAM Identifiers in the IAM User Guide.

This parameter is optional. If it is not included, it defaults to a slash (/).

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.

Implementation

Future<CreatePolicyResponse> createPolicy({
  required String policyDocument,
  required String policyName,
  String? description,
  String? path,
}) async {
  ArgumentError.checkNotNull(policyDocument, 'policyDocument');
  _s.validateStringLength(
    'policyDocument',
    policyDocument,
    1,
    131072,
    isRequired: true,
  );
  ArgumentError.checkNotNull(policyName, 'policyName');
  _s.validateStringLength(
    'policyName',
    policyName,
    1,
    128,
    isRequired: true,
  );
  _s.validateStringLength(
    'description',
    description,
    0,
    1000,
  );
  _s.validateStringLength(
    'path',
    path,
    1,
    512,
  );
  final $request = <String, dynamic>{};
  $request['PolicyDocument'] = policyDocument;
  $request['PolicyName'] = policyName;
  description?.also((arg) => $request['Description'] = arg);
  path?.also((arg) => $request['Path'] = arg);
  final $result = await _protocol.send(
    $request,
    action: 'CreatePolicy',
    version: '2010-05-08',
    method: 'POST',
    requestUri: '/',
    exceptionFnMap: _exceptionFns,
    shape: shapes['CreatePolicyRequest'],
    shapes: shapes,
    resultWrapper: 'CreatePolicyResult',
  );
  return CreatePolicyResponse.fromXml($result);
}