AWS ‐ CLI - FullstackCodingGuy/Developer-Fundamentals GitHub Wiki

Command Usage

Installing in windows Run this in command prompt : msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi

To configure the profile Aws configure --profile <profilename>

To get help of service aws dynamodb help aws ec2 help

To set default region aws configure set region us-east-1

To get the EC2 instances Aws ec2 describe-instances --profile <profilename> --region ca-central-1

To get the users aws iam list-users --profile <profilename>

To get the dynamodb tables aws dynamodb list-tables --profile clip1 --region us-east-1

Creating ECS Cluster

aws ecs create-cluster --cluster-name --region us-east-1

Creating ECS Service

Configuring Ingress for the ECS container - configuring port 80 to be exposed, by enabling in security group aws ec2 authorize-security-group-ingress --group-id --protocol tcp --port 80 --cidr 0.0.0.0/0

Creating ECS Service using json file definition

aws ecs register-task-definition --cli-input-json file://filename.json

Creating new service into the cluster

aws ecs create-service --cluster <clster-name> --service-name <name-given-in-above-json> --task-definition <task-def-name>:<version> --desired-count 1 --launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[],securityGroups=[],assignPublicIp=ENABLED}"

To see all the services defined in the cluster

aws ecs list-services --cluster <cluster=name>

To describe the running services in the cluster

aws ecs describe-services --cluster <cluster=name>


ECR

To list Amazon Elastic Container Registry (ECR) repositories using AWS CLI, use the following command:

aws ecr describe-repositories --region <region-name>

Example

aws ecr describe-repositories --region us-east-1

This will return a JSON output containing all ECR repositories in us-east-1.


List ECR Repositories with Only Repository Names

If you only want to see repository names:

aws ecr describe-repositories --query 'repositories[*].repositoryName' --output table

or in a simple list format:

aws ecr describe-repositories --query 'repositories[*].repositoryName' --output text

List ECR Images in a Repository

If you want to see the images inside a repository:

aws ecr list-images --repository-name <your-repository-name> --region <region-name>

Example:

aws ecr list-images --repository-name my-next-app --region us-east-1

Filter ECR Repositories by Name

To find a specific repository:

aws ecr describe-repositories --query "repositories[?repositoryName=='my-next-app']"

Push local docker image to ECR

Push a Local Docker Image to AWS ECR 🚀

To push a local Docker image to AWS Elastic Container Registry (ECR), follow these steps:


1️⃣ Authenticate Docker to AWS ECR

First, authenticate Docker to AWS ECR using the AWS CLI:

aws ecr get-login-password --region <region-name> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region-name>.amazonaws.com

🔹 Example for us-east-1:

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com

2️⃣ Create an ECR Repository (If Not Exists)

If you haven't created a repository yet, create one:

aws ecr create-repository --repository-name my-next-app --region <region-name>

🔹 Example:

aws ecr create-repository --repository-name my-next-app --region us-east-1

3️⃣ Tag the Local Docker Image

Tag your local image to match the ECR repository URL:

docker tag <local-image-name>:<tag> <aws_account_id>.dkr.ecr.<region-name>.amazonaws.com/<repository-name>:<tag>

🔹 Example:

docker tag my-next-app:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-next-app:latest

4️⃣ Push the Image to ECR

Now, push the image to ECR:

docker push <aws_account_id>.dkr.ecr.<region-name>.amazonaws.com/<repository-name>:<tag>

🔹 Example:

docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-next-app:latest

5️⃣ Verify the Image in ECR

After pushing the image, verify it using:

aws ecr list-images --repository-name my-next-app --region us-east-1

✅ Done!

Now, you can use the image in AWS ECS (Fargate), Kubernetes (EKS), or Lambda (Container Image).

Would you like help deploying it on ECS or Lambda? 🚀


S3

Create an S3 bucket

aws s3 mb s3://your-bucket-name

Configuring the Read Policy for s3 bucket

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicRead",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<bucket-name>/*"
        }
    ]
}

Run the following AWS CLI command (replace with your AWS region, e.g., us-east-1):

aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-*-x86_64-gp2" --region <region> --query "Images[*].[ImageId, Name]" --output table

This will return a list of Amazon Linux 2 AMIs that are available.

Delete all aws resources using one command

read

The simplest way to delete all AWS resources is to delete the AWS CloudFormation stack if you used AWS SAM or CloudFormation. However, if resources were manually created, the best approach is to delete everything under your AWS account using AWS Nuke.


1. Delete All Resources Using AWS Nuke (Recommended)

AWS Nuke is a third-party tool designed to delete all AWS resources in an account.

Steps to Use AWS Nuke

Step 1: Install AWS Nuke

wget https://github.com/rebuy-de/aws-nuke/releases/latest/download/aws-nuke-linux-amd64 -O aws-nuke
chmod +x aws-nuke
sudo mv aws-nuke /usr/local/bin/

Step 2: Configure AWS Nuke

Create a configuration file (nuke-config.yml):

regions:
  - "us-east-1"
  - "us-west-2"

account-blocklist:
  - "123456789012" # (Prevent accidental deletion of important accounts)

accounts:
  "your-account-id":
    filters:
      IAMUser:
        - "admin"
      IAMRole:
        - "OrganizationAccountAccessRole"

Step 3: Run AWS Nuke

aws-nuke -c nuke-config.yml --force
  • This will list all resources before deleting them.
  • Type "Nuke it" to confirm.

2. Delete AWS Account (Extreme Case)

If you want to delete the entire AWS account (not just resources):

  1. Go to AWS Console → Billing Dashboard
  2. Click Close Account

Which Method Do You Prefer?

  • For cleaning up all resources: ✅ Use AWS Nuke
  • For deleting AWS deployments only: ✅ Use CloudFormation delete-stack
  • For permanently closing AWS account: ✅ Delete via AWS Console

AWS Secret Manager

Store Password in AWS Secrets Manager

Run this command to create a secret:

# to create secret

aws secretsmanager create-secret \
    --name DocumentDBSecret \
    --secret-string '{"username": "admin", "password": "SecurePass123"}'

# to update secret

aws secretsmanager update-secret \
    --secret-id DocumentDBSecret \
    --secret-string '{"username": "admin", "password": "New#SecurePass123"}'

# to retrieve secret

aws secretsmanager get-secret-value --secret-id DocumentDBSecret



References

⚠️ **GitHub.com Fallback** ⚠️