[AWS] Create User And Attach Policy Using AWS CLI
Tags: awscli, iam, createuserwithcli
Published: 2023-12-30 12:36:40

---
title: [AWS] Create User And Attach Policy Using AWS CLI
published: true
date: 2023-02-14 17:08:08 UTC
tags: 
- AWSCLI
- iam
- CreateUserWithCLI
canonical_url: https://www.kmp.tw/post/createuserandattachpolicyusingawscli/
---


# create policy
## add policy file

<!--more-->

`vim aws-permission.json`

```
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "VisualEditor0",
      "Effect": "Allow",
      "Action": [
        "<service1>:<action1>",
        "<service2>:<action2>"
      ],
      "Resource": "*"
    }
  ]
}
```

## create new policy with permission file
 `aws iam create-policy --policy-name new-policy --policy-document file://aws-permission.json`

### Result
```
-------------------------------------------------------------------------------------------
|                                        GetPolicy                                        |
+-----------------------------------------------------------------------------------------+
||                                        Policy                                         ||
|+--------------------------------+------------------------------------------------------+|
||  Arn                           |  arn:aws:iam::<account-id>:policy/new-policy         ||
||  AttachmentCount               |  0                                                   ||
||  CreateDate                    |  2023-02-14T07:48:15+00:00                           ||
||  DefaultVersionId              |  v1                                                  ||
||  IsAttachable                  |  True                                                ||
||  Path                          |  /                                                   ||
||  PermissionsBoundaryUsageCount |  0                                                   ||
||  PolicyId                      |  ANP000000000000000000                               ||
||  PolicyName                    |  new-policy                                          ||
||  UpdateDate                    |  2023-02-14T07:48:15+00:00                           ||
|+--------------------------------+------------------------------------------------------+|
```


## Create User

`aws iam create-user --user-name cutomer-user`


### Result
```
--------------------------------------------------------------------
|                            CreateUser                            |
+------------------------------------------------------------------+
||                              User                              ||
|+------------+---------------------------------------------------+|
||  Arn       |  arn:aws:iam::<account-id>:user/cutomer-user      ||
||  CreateDate|  2023-02-14T08:16:06+00:00                        ||
||  Path      |  /                                                ||
||  UserId    |  AIDA00000000000000000                            ||
||  UserName  |  cutomer-user                                     ||
|+------------+---------------------------------------------------+|
```

## Attach user policy

`aws iam attach-user-policy --policy-arn arn:aws:iam::<account-id>:policy/new-policy --user-name cutomer-user`


## Create Access Key

`aws iam create-access-key --user-name cutomer-user`


### Result
```
-------------------------------------------------------------------
|                         CreateAccessKey                         |
+-----------------------------------------------------------------+
||                           AccessKey                           ||
|+------------------+--------------------------------------------+|
||  AccessKeyId     |  <YourKeyID>                               ||
||  CreateDate      |  2023-02-14T08:23:16+00:00                 ||
||  SecretAccessKey |  <YourSecretAccessKey>                     ||
||  Status          |  Active                                    ||
||  UserName        |  cutomer-user                              ||
|+------------------+--------------------------------------------+|
```