Skip to content

AWS ‐ CLI

Full Stack edited this page Mar 4, 2025 · 17 revisions

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

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

References

Clone this wiki locally