AWS CloudShell

AWS CloudShell is a browser-based shell environment available directly through the AWS Management Console. It provides a command-line interface (CLI) to manage and interact with AWS resources securely without needing to install any software or set up credentials on your local machine.

Use Cases

Quick Access to AWS CLI

Allows you to run AWS CLI commands directly without configuring your local machine. It's perfect for quick tasks like managing AWS resources (e.g., EC2 instances, S3 buckets, or Lambda functions).

Development and Automation

You can write and execute scripts using common programming languages like Python and Shell. It’s great for testing and automating tasks directly within your AWS environment.

Secure and Pre-Configured Environment

AWS CloudShell comes pre-configured with AWS CLI, Python, Node.js, and other essential tools. It uses your IAM permissions, so you don’t need to handle keys or credentials directly, making it secure and convenient.

Access to Filesystem and Persistent Storage

You get a persistent 1 GB home directory per region to store scripts, logs, or other files between sessions, which can be used to manage files related to your AWS resources.

Cross-Region Management

You can access and manage resources across different AWS regions directly from CloudShell, making it useful for multi-region setups.


Basic Commands

    aws s3 ls
    aws ec2 describe-instances

    sudo apt install jq

list_buckets.sh

#!/bin/bash
echo "Listing all S3 buckets:"
aws s3 ls
    bash list_buckets.sh
# get account details

aws sts get-caller-identity

# list available regions

aws ec2 describe-regions --query "Regions[].RegionName" --output table

# create a bucket

aws s3 mb s3://chandr34-newbucket

# upload a file to a bucket 

echo "Hello, CloudShell!" > hello.txt
aws s3 cp hello.txt s3://chandr34-newbucket

# List files in bucket 

aws s3 ls s3://chandr34-newbucket/

# Delete bucket  with files 

aws s3 rb s3://chandr34-newbucket --force

# List AMIs

aws ec2 describe-images --owners amazon --query 'Images[*].{ID:ImageId,Name:Name}' --output table

# quickly launch a ec2

aws ec2 create-key-pair --key-name gcnewkeypair --query 'KeyMaterial' --output text > myNewKeyPair.pem

# Change Permission

chmod 0400 myNewKeyPair.pem

# Launch new EC2

aws ec2 run-instances --image-id ami-0866a3c8686eaeeba --count 1 --instance-type t2.micro --key-name gcnewkeypair --security-groups default

# Get Public IP

aws ec2 describe-instances --query "Reservations[].Instances[].PublicIpAddress" --output text

# Login to server

ssh -i myKeyNewPair.pem ubuntu@<getthehostip>

# terminate the instance

aws ec2 terminate-instances --instance-ids <>

Cloud Formation

my-webserver.yml

AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template to launch an Amazon Linux EC2 instance with Nginx installed.

Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: ami-0866a3c8686eaeeba
      KeyName: gcnewkeypair
      SecurityGroupIds:
        - !Ref InstanceSecurityGroup
      UserData:
        Fn::Base64: 
          !Sub |
            #!/bin/bash
            apt update -y
            apt install -y nginx
            systemctl start nginx
            systemctl enable nginx
      Tags:
        - Key: Name
          Value: MyNginxServer

  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable SSH and HTTP access
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0  # SSH access, restrict this to your IP range for security
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          FromPort: 443
          ToPort: 443
          CidrIp: 0.0.0.0/0  # HTTP access for Nginx

Outputs:
  InstanceId:
    Description: The Instance ID of the EC2 instance
    Value: !Ref MyEC2Instance
  PublicIP:
    Description: The Public IP address of the EC2 instance
    Value: !GetAtt MyEC2Instance.PublicIp
  WebURL:
    Description: URL to access the Nginx web server
    Value: !Sub "http://${MyEC2Instance.PublicIp}"

Launch the Stack via CloudShell

# Create the stack
aws cloudformation create-stack --stack-name gc-stack --template-body file://my-webserver.yml --capabilities CAPABILITY_NAMED_IAM


# Check the status

aws cloudformation describe-stacks --stack-name gc-stack --query "Stacks[0].StackStatus"


aws cloudformation describe-stacks --stack-name gc-stack --query "Stacks[0].Outputs"

# delete the stack

aws cloudformation delete-stack --stack-name gc-stack


aws cloudformation describe-stacks --stack-name gc-stack --query "Stacks[0].StackStatus"

# confirm the deletion status

aws cloudformation list-stacks --query "StackSummaries[?StackName=='gc-stack'].StackStatus"