Why should you opt to learn several AWS CLI commands when you can do the same thing via the console? Hold that thought, friend! There are several scenarios when the command line speeds up the access and functionality. Hence we make use of it. On that note, let’s delve deeper into this specially prepared AWS CLI Commands Cheat Sheet.
IAM Basic Commands
IAM or AWS Identity and Access Management specifies who can access what on AWS services. It’s the perfect way to control and manage entry to various AWS functionalities. Coming at no additional charge, IAM can manage permissions for the workforce as well as different workloads.
However, to run IAM, you will need to know some basic IAM Commands. This short blog provides you with the perfect cheat sheet of commands to help you get started. So without further ado, let’s dive right in!
With this list of commands, you should be able to perform essential management of your AWS account. So, here’s how to use/manage AWS IAM using AWS CLI.
- To list AWS IAM users
Follow this command to list the users in that specified path prefix. If no options are available, the command will list all the users in that Amazon Web Services account.
aws iam list-users |
For more information on this command, click here.
- To list AWS EC2 regions
This instruction describes all the Regions that AWS EC2 supports or just the regions enabled for your account. For more details on this particular instruction, click here.
aws ec2 describe-regions or aws ec2 describe-regions –output text (for text format or use “table” for table format) |
- Create an IAM user group
This command helps to create a new group in IAM. For more information regarding this command, you can check out this link. We have shown the instruction below.
aws iam create-group –group-name nameofthegroup |
- Create IAM user
Next comes a very important instruction with a simple function to create a new IAM user for Amazon Web Services. To know how to do this, check out this link.
aws iam create-user –user-name nameoftheuser |
- Create login profile for user
This instruction creates a password for any particular IAM user. Executing this command makes it possible for the concerned IAM user to access Amazon Web Services via the Amazon Web Services Management Console. To know more, click on this link.
aws iam create-login-profile –user-name nameoftheuser –password yourpwd |
- Create access key for the IAM user
Creates a new AWS secret access key. Moreover, it creates an AWS access key ID specific to the user. The default status for the new key is Active. Use this link to know more.
aws iam create-access-key –user-name nameoftheuser |
- Add IAM user to a group
This simple instruction adds a specific user to a particular group. More details are available here.
aws iam add-user-to-group –user-name nameoftheuser –group-name nameofthegroup |
- Get group details
This IAM command gives as output a list of users belonging to a specific IAM group. More details are available here.
aws iam get-group –group-name nameofthegroup |
- Attach Policy to the Group
Use this command to attach the specified managed policy to the IAM Group. More information can be found at this link.
aws iam attach-group-policy –group-name nameofthegroup –policy-arn arn:aws:iam::aws:policy/AdministratorAccess (policy can vary) |
- List attached grouped policies
Lists all the managed policies that are attached to the specific IAM group. There may be inline policies as well. For more details, you can check out here.
aws iam list-attached-group-policies –group-name nameofthegroup |
EC2 Basic Commands
Anything you can do on the console, you can do better on the Command Line Interface. At least, that’s what certain system administrators seem to believe! Here are some EC2 commands you can learn to make the handling of multiple servers simpler. Let’s start right away!
- Create a new key pair for EC2
This command creates a 2048-bit RSA key pair with the specified name. Amazon EC2 saves the private key to a file and stores the public key. You can find further details regarding this command here.
aws ec2 create-key-pair –key-name yourkeyname –query ‘KeyMaterial’ –output text > yourkeyname.pem (the key will be saved on your current dir path) |
- Create a security group for EC2
A security group is the equivalent of a firewall that controls the inbound and outbound traffic. This command creates a security group. For more information on it, you can check out this link.
aws ec2 create-security-group –group-name yourSGname –description “My security group” |
- Add an inbound rule to a security group
This command adds specified inbound rules to a security group. An inbound rule allows instances to receive traffic from specified IPv4 or IPv6 addresses. To know more about this command, check out the official documentation here.
aws ec2 authorize-security-group-ingress –group-name yourSGname cli –protocol tcp –port 80 –cidr *****/32 (rules can vary upon your needs) |
- Run an EC2 instance
The command shown below is responsible for launching many instances using an AMI (Amazon Machine Image) for which you have the requisite permission. You can find further details and examples here.
aws ec2 run-instances –image-id ami-0c1a7f89451184c8b –count 1 –instance type t2.micro –key yourkeyname –security-groups yourSGname (aim and instancetype mayv vary upon your needs) |
- Describe instance as per our need
This command describes specific instances using instance IDs or all instances. You can also use filters to clear out several unnecessary instances. Details and examples are available here.
aws ec2 describe-instances –query ‘Reservations[*].Instances[*]. {Instance:InstanceId,Launchtime:LaunchTime}’ –output table (keys and values can be changed) |
- To Terminate an EC2 instance
An idempotent command that shuts down the specified instance. If the command is executed more than once, each call succeeds. More details and examples can be found here.
aws ec2 terminate-instances –instance-ids i-08b0f2eeee2b93369 (choose your Instance ID) |
S3 Basic Commands
It’s easier to manage S3 buckets and objects from the Command line than the console. Below we have given a quick look at some of the most important S3 commands. Let’s start.
- Create an S3 bucket
To create an S3 bucket you can use the following command:
aws s3 mb s3://yourbucketname |
For examples and details refer to this link.
- List S3 buckets
Lists S3 objects and similar prefixes under a prefix or S3 buckets. Further details can be found here.
aws s3 ls |
- Remove a S3 bucket
Note that this command deletes an empty S3 bucket. The bucket must be completely empty of versioned objects and objects before it can be removed. More information is available at this link.
aws s3 rb s3://yourbucketname –force ( –force can be used if bucket is not empty otherwise without it) |
- Copy a file to the bucket
This command makes a copy of a local file or S3 object to another location in S3 or locally. Get more information here.
aws s3 cp filename s3://yourbucketname |
- Copy all files and folders to the bucket
aws s3 cp folderpath s3://yourbucketname –recursive |
- List files in the bucket
This instruction performs two tasks:
- Lists S3 objects
- List common prefixes under a particular prefix or all S3 buckets.
You can obtain more information from here.
aws s3 ls s3://yourbucketname |
- Set permissions to a file in a bucket
aws s3 cp filename s3://yourbucketname –acl public-read |
- Sync files from folder to s3
This command syncs S3 prefixes and directories. For more information regarding this, check here.
aws s3 sync pathofthefolder s3://yourbucketname |
- Delete files in S3 bucket
This command removes an S3 bucket. More details are available here.
aws s3 rm s3://yourbucketname/filename |
- Delete directories in S3 bucket
This command deletes directories in S3 buckets. For more details, look here.
aws s3 rm s3://yourbucketname/directory aws s3 rm s3://yourbucketname/directory –recursive # (In case if directory is not empty) |
- Use S3 API to list contents in the bucket
This command returns some or all of the objects in a bucket. It can be up to 1000 objects. More details are here.
aws s3api list-objects –bucket yourbucketname |
Wrapping up
Anything a console can do CLI can do better! That seems to be the conclusion of the cheat sheet. With CLI you can manage AWS resources across accounts, environments and regions. Moreover, you can control services manually or create scripts.
At SysAlly, we experiment with Cloud and the most useful information is passed on to the world as blogs. Signup for the updates.
Until later, here’s SysAlly signing off!