AWS CLI version 2, the latest major version of AWS CLI, is now stable and recommended for general use. To view this page for the AWS CLI version 2, click here. For more information see the AWS CLI version 2 installation instructions and migration guide.
Creates a new managed policy for your Amazon Web Services 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 .
As a best practice, you can validate your IAM policies. To learn more, see Validating IAM 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 .
See also: AWS API Documentation
create-policy
--policy-name <value>
[--path <value>]
--policy-document <value>
[--description <value>]
[--tags <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
[--debug]
[--endpoint-url <value>]
[--no-verify-ssl]
[--no-paginate]
[--output <value>]
[--query <value>]
[--profile <value>]
[--region <value>]
[--version <value>]
[--color <value>]
[--no-sign-request]
[--ca-bundle <value>]
[--cli-read-timeout <value>]
[--cli-connect-timeout <value>]
--policy-name
(string)
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".
--path
(string)
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.Note
You cannot use an asterisk (*) in the path name.
--policy-document
(string)
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 CloudFormation templates formatted in YAML, you can provide the policy in JSON or YAML format. CloudFormation always converts a YAML policy to JSON format before submitting it to IAM.
The maximum length of the policy document that you can pass in this operation, including whitespace, is listed below. To view the maximum character counts of a managed policy with no whitespaces, see IAM and STS character quotas .
To learn more about JSON policy grammar, see Grammar of the IAM JSON policy language in the IAM User Guide .
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
)
--description
(string)
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.
--tags
(list)
A list of tags that you want to attach to the new IAM customer managed policy. Each tag consists of a key name and an associated value. For more information about tagging, see Tagging IAM resources in the IAM User Guide .
Note
If any one of the tags is invalid or if you exceed the allowed maximum number of tags, then the entire request fails and the resource is not created.(structure)
A structure that represents user-provided metadata that can be associated with an IAM resource. For more information about tagging, see Tagging IAM resources in the IAM User Guide .
Key -> (string)
The key name that can be used to look up or retrieve the associated value. For example,Department
orCost Center
are common choices.Value -> (string)
The value associated with this tag. For example, tags with a key name of
Department
could have values such asHuman Resources
,Accounting
, andSupport
. Tags with a key name ofCost Center
might have values that consist of the number associated with the different cost centers in your company. Typically, many resources have tags with the same key name but with different values.Note
Amazon Web Services always interprets the tagValue
as a single string. If you need to store an array, you can store comma-separated values in the string. However, you must interpret the value in your code.
Shorthand Syntax:
Key=string,Value=string ...
JSON Syntax:
[
{
"Key": "string",
"Value": "string"
}
...
]
--cli-input-json
(string)
Performs service operation based on the JSON string provided. The JSON string follows the format provided by --generate-cli-skeleton
. If other arguments are provided on the command line, the CLI values will override the JSON-provided values. It is not possible to pass arbitrary binary values using a JSON-provided value as the string will be taken literally.
--generate-cli-skeleton
(string)
Prints a JSON skeleton to standard output without sending an API request. If provided with no value or the value input
, prints a sample input JSON that can be used as an argument for --cli-input-json
. If provided with the value output
, it validates the command inputs and returns a sample output JSON for that command.
--debug
(boolean)
Turn on debug logging.
--endpoint-url
(string)
Override command's default URL with the given URL.
--no-verify-ssl
(boolean)
By default, the AWS CLI uses SSL when communicating with AWS services. For each SSL connection, the AWS CLI will verify SSL certificates. This option overrides the default behavior of verifying SSL certificates.
--no-paginate
(boolean)
Disable automatic pagination. If automatic pagination is disabled, the AWS CLI will only make one call, for the first page of results.
--output
(string)
The formatting style for command output.
--query
(string)
A JMESPath query to use in filtering the response data.
--profile
(string)
Use a specific profile from your credential file.
--region
(string)
The region to use. Overrides config/env settings.
--version
(string)
Display the version of this tool.
--color
(string)
Turn on/off color output.
--no-sign-request
(boolean)
Do not sign requests. Credentials will not be loaded if this argument is provided.
--ca-bundle
(string)
The CA certificate bundle to use when verifying SSL certificates. Overrides config/env settings.
--cli-read-timeout
(int)
The maximum socket read time in seconds. If the value is set to 0, the socket read will be blocking and not timeout. The default value is 60 seconds.
--cli-connect-timeout
(int)
The maximum socket connect time in seconds. If the value is set to 0, the socket connect will be blocking and not timeout. The default value is 60 seconds.
To use the following examples, you must have the AWS CLI installed and configured. See the Getting started guide in the AWS CLI User Guide for more information.
Unless otherwise stated, all examples have unix-like quotation rules. These examples will need to be adapted to your terminal's quoting rules. See Using quotation marks with strings in the AWS CLI User Guide .
Example 1: To create a customer managed policy
The following command creates a customer managed policy named my-policy
. The file policy.json
is a JSON document in the current folder that grants read only access to the shared
folder in an Amazon S3 bucket named amzn-s3-demo-bucket
.
aws iam create-policy \
--policy-name my-policy \
--policy-document file://policy.json
Contents of policy.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": [
"arn:aws:s3:::amzn-s3-demo-bucket/shared/*"
]
}
]
}
Output:
{
"Policy": {
"PolicyName": "my-policy",
"CreateDate": "2015-06-01T19:31:18.620Z",
"AttachmentCount": 0,
"IsAttachable": true,
"PolicyId": "ZXR6A36LTYANPAI7NJ5UV",
"DefaultVersionId": "v1",
"Path": "/",
"Arn": "arn:aws:iam::0123456789012:policy/my-policy",
"UpdateDate": "2015-06-01T19:31:18.620Z"
}
}
For more information on using files as input for string parameters, see Specify parameter values for the AWS CLI in the AWS CLI User Guide.
Example 2: To create a customer managed policy with a description
The following command creates a customer managed policy named my-policy
with an immutable description.
The file policy.json
is a JSON document in the current folder that grants access to all Put, List, and Get actions for an Amazon S3 bucket named amzn-s3-demo-bucket
.
aws iam create-policy \
--policy-name my-policy \
--policy-document file://policy.json \
--description "This policy grants access to all Put, Get, and List actions for amzn-s3-demo-bucket"
Contents of policy.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket*",
"s3:PutBucket*",
"s3:GetBucket*"
],
"Resource": [
"arn:aws:s3:::amzn-s3-demo-bucket"
]
}
]
}
Output:
{
"Policy": {
"PolicyName": "my-policy",
"PolicyId": "ANPAWGSUGIDPEXAMPLE",
"Arn": "arn:aws:iam::123456789012:policy/my-policy",
"Path": "/",
"DefaultVersionId": "v1",
"AttachmentCount": 0,
"PermissionsBoundaryUsageCount": 0,
"IsAttachable": true,
"CreateDate": "2023-05-24T22:38:47+00:00",
"UpdateDate": "2023-05-24T22:38:47+00:00"
}
}
For more information on Idenity-based Policies, see Identity-based policies and resource-based policies in the AWS IAM User Guide.
Example 3: To create a customer managed policy with tags
The following command creates a customer managed policy named my-policy
with tags. This example uses the --tags
parameter with the following JSON-formatted tags: '{"Key": "Department", "Value": "Accounting"}' '{"Key": "Location", "Value": "Seattle"}'
. Alternatively, the --tags
parameter can be used with tags in the shorthand format: 'Key=Department,Value=Accounting Key=Location,Value=Seattle'
.
The file policy.json
is a JSON document in the current folder that grants access to all Put, List, and Get actions for an Amazon S3 bucket named amzn-s3-demo-bucket
.
aws iam create-policy \
--policy-name my-policy \
--policy-document file://policy.json \
--tags '{"Key": "Department", "Value": "Accounting"}' '{"Key": "Location", "Value": "Seattle"}'
Contents of policy.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket*",
"s3:PutBucket*",
"s3:GetBucket*"
],
"Resource": [
"arn:aws:s3:::amzn-s3-demo-bucket"
]
}
]
}
Output:
{
"Policy": {
"PolicyName": "my-policy",
"PolicyId": "ANPAWGSUGIDPEXAMPLE",
"Arn": "arn:aws:iam::12345678012:policy/my-policy",
"Path": "/",
"DefaultVersionId": "v1",
"AttachmentCount": 0,
"PermissionsBoundaryUsageCount": 0,
"IsAttachable": true,
"CreateDate": "2023-05-24T23:16:39+00:00",
"UpdateDate": "2023-05-24T23:16:39+00:00",
"Tags": [
{
"Key": "Department",
"Value": "Accounting"
},
"Key": "Location",
"Value": "Seattle"
{
]
}
}
For more information on Tagging policies, see Tagging customer managed policies in the AWS IAM User Guide.
Policy -> (structure)
A structure containing details about the new policy.
PolicyName -> (string)
The friendly name (not ARN) identifying the policy.PolicyId -> (string)
The stable and unique string identifying the policy.
For more information about IDs, see IAM identifiers in the IAM User Guide .
Arn -> (string)
The Amazon Resource Name (ARN). ARNs are unique identifiers for Amazon Web Services resources.
For more information about ARNs, go to Amazon Resource Names (ARNs) in the Amazon Web Services General Reference .
Path -> (string)
The path to the policy.
For more information about paths, see IAM identifiers in the IAM User Guide .
DefaultVersionId -> (string)
The identifier for the version of the policy that is set as the default version.AttachmentCount -> (integer)
The number of entities (users, groups, and roles) that the policy is attached to.PermissionsBoundaryUsageCount -> (integer)
The number of entities (users and roles) for which the policy is used to set the permissions boundary.
For more information about permissions boundaries, see Permissions boundaries for IAM identities in the IAM User Guide .
IsAttachable -> (boolean)
Specifies whether the policy can be attached to an IAM user, group, or role.Description -> (string)
A friendly description of the policy.
This element is included in the response to the GetPolicy operation. It is not included in the response to the ListPolicies operation.
CreateDate -> (timestamp)
The date and time, in ISO 8601 date-time format , when the policy was created.UpdateDate -> (timestamp)
The date and time, in ISO 8601 date-time format , when the policy was last updated.
When a policy has only one version, this field contains the date and time when the policy was created. When a policy has more than one version, this field contains the date and time when the most recent policy version was created.
Tags -> (list)
A list of tags that are attached to the instance profile. For more information about tagging, see Tagging IAM resources in the IAM User Guide .
(structure)
A structure that represents user-provided metadata that can be associated with an IAM resource. For more information about tagging, see Tagging IAM resources in the IAM User Guide .
Key -> (string)
The key name that can be used to look up or retrieve the associated value. For example,Department
orCost Center
are common choices.Value -> (string)
The value associated with this tag. For example, tags with a key name of
Department
could have values such asHuman Resources
,Accounting
, andSupport
. Tags with a key name ofCost Center
might have values that consist of the number associated with the different cost centers in your company. Typically, many resources have tags with the same key name but with different values.Note
Amazon Web Services always interprets the tagValue
as a single string. If you need to store an array, you can store comma-separated values in the string. However, you must interpret the value in your code.