DEV Community

ChungWei Wei
ChungWei Wei

Posted on • Originally published at kmp.tw on

[AWS] Create User And Attach Policy Using AWS CLI

create policy

add policy file

vim aws-permission.json

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

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                           ||
|+--------------------------------+------------------------------------------------------+|
Enter fullscreen mode Exit fullscreen mode

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                                     ||
|+------------+---------------------------------------------------+|
Enter fullscreen mode Exit fullscreen mode

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                              ||
|+------------------+--------------------------------------------+|
Enter fullscreen mode Exit fullscreen mode

Top comments (0)