The AWS CLI (Command Line Interface) is a tool provided by Amazon Web Services that allows users to interact with various AWS services from a command-line interface. It provides a convenient way to manage AWS resources, automate tasks, and integrate with other tools and scripts. With the AWS CLI, users can perform tasks such as creating and managing EC2 instances, managing S3 buckets, configuring security groups, and much more. The AWS CLI is available for Windows, macOS, and Linux, and can be installed using various methods, such as pip, Homebrew, or as a pre-built package.
To configure the AWS Command Line Interface (CLI), follow these steps:
Install the AWS CLI
Open a terminal or command prompt and run the following command:
aws configure
Enter your AWS Access Key ID and Secret Access Key: You can find your Access Key ID and Secret Access Key in the AWS Management Console under your IAM user account. Enter these values when prompted by the CLI.
Enter your default region name: Enter the default region name that you want to use for your AWS resources, for example: "us-east-1".
Enter your default output format: Enter the default output format that you want to use for the CLI, for example: "json".
Once you have completed these steps, you should be able to use the AWS CLI to interact with your AWS resources from the command line.
Amazon S3 with AWS CLI
Here are some AWS S3 CLI commands that you can use to manage your S3 resources:
This command lists all the S3 buckets in your AWS account
aws s3 ls
aws s3api list-buckets
This command creates a new S3 bucket with the specified name.
aws s3 mb s3://bucket-name
This command uploads a file from your local machine to the specified S3 bucket.
aws s3 cp file.txt s3://bucket-name/path/to/file.txt
This command syncs a local folder with an S3 bucket, uploading new or modified files and deleting files that no longer exist locally.
aws s3 sync /path/to/local/folder s3://bucket-name/folder
This command deletes a file from the specified S3 bucket.
aws s3 rm s3://bucket-name/path/to/file.txt
This command deletes the specified S3 bucket and all the objects it contains.
aws s3 rb s3://bucket-name
This command generates a pre-signed URL for the specified S3 object, which can be used to grant temporary access to the object without requiring AWS credentials.
aws s3 presign s3://bucket-name/path/to/file.txt
This command sets a bucket policy for the specified S3 bucket, allowing you to define permissions and access controls for the bucket.
aws s3api put-bucket-policy --bucket bucket-name --policy file://policy.json
This command retrieves the access control list (ACL) for the specified S3 bucket, showing the permissions that are set for the bucket.
aws s3api get-bucket-acl --bucket bucket-name
Amazon IAM with AWS CLI
This command lists all the IAM users in your AWS account.
aws iam list-users
This command creates a new IAM user with the specified name.
aws iam create-user --user-name username
This command deletes the specified IAM user.
aws iam delete-user --user-name username
This command lists all the IAM groups in your AWS account.
aws iam list-groups
This command creates a new IAM group with the specified name.
aws iam create-group --group-name groupname
This command deletes the specified IAM group.
aws iam delete-group --group-name groupname
This command adds the specified IAM user to the specified IAM group.
aws iam add-user-to-group --user-name username --group-name groupname
This command removes the specified IAM user from the specified IAM group.
aws iam remove-user-from-group --user-name username --group-name groupname
This command lists all the IAM roles in your AWS account.
aws iam list-roles
This command creates a new IAM role with the specified name and trust policy.
aws iam create-role --role-name rolename --assume-role-policy-document file://policy.json
This command deletes the specified IAM role.
aws iam delete-role --role-name rolename
This command attaches the specified policy to the specified IAM role.
aws iam attach-role-policy --role-name rolename --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
This command detaches the specified policy from the specified IAM role.
aws iam detach-role-policy --role-name rolename --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess