AWS Infrastructure Automation: Launching EC2 Instances, Managing EBS Volumes, and Configuring SNS with CLI and Boto3

ยท

5 min read

AWS Infrastructure Automation: Launching EC2 Instances, Managing EBS Volumes, and Configuring SNS with CLI and Boto3

๐Ÿ’ช๐Ÿป Unleash the Power of AWS Automation!

In today's cloud-centric era, AWS (Amazon Web Services) ๐ŸŒฉ๏ธ has emerged as a dominant player, providing a wide range of powerful services for building and managing cloud-based infrastructures. Among its most popular offerings are EC2 (Elastic Compute Cloud) instances, EBS (Elastic Block Store) volumes, and SNS (Simple Notification Service), which enable flexible computing, storage, and communication capabilities in the cloud.

This guide delves into the world of AWS infrastructure automation, focusing on three fundamental tasks: launching EC2 instances, managing EBS volumes, and configuring SNS using two different approaches. We will explore how to accomplish these tasks using both the AWS Command Line Interface (CLI) and the Boto3 library, which is the AWS SDK for Python, providing programmatic access to AWS services via API calls.

Whether you are an AWS enthusiast, a developer, or an IT professional looking to streamline and scale your cloud-based operations, mastering the art of automating these processes will significantly enhance your productivity and efficiency in managing AWS resources. So, let's embark on this exciting journey to harness the full potential of AWS infrastructure automation using CLI and Boto3! ๐ŸŒˆ๐Ÿ’ก

๐Ÿš€ Launching an EC2 instance and an EBS volume, and then attaching the volume to the instance using the AWS Command Line Interface (CLI)

Step1: Create a key pair:

Step2: Launch an ec2 instance

  • Open your terminal or command prompt, and then use the following command to launch the EC2 instance:

    aws ec2 run-instances --image-id ami-0ded8326293d3201b --instance-type t2.micro --security-group-ids sg-0a1b2c3d4e5f6g7h8 --key-name MyKeyPair

    • In this command:

      • --image-id: Specifies the AMI ID, which is set to ami-0ded8326293d3201b for the Amazon Linux 2023 AMI.

      • --instance-type: Specifies the instance type, which is set to t2.micro.

      • --security-group-ids:

      • --key-name: Specifies the name of the existing key pair, which is set to mykeypair (see step1)

      • After running the command:

Step 4: Access the EC2 Instance

๐Ÿ’ก
To access the EC2 Instance: ssh -i /path/to/MyKeyPair.pem ec2-user@<public_ip>
  • In the command:

    • Public_ip:

    • Editing Inbound Rules:

    • Accessing EC2 instance:

Step 5: Create an EBS volume

  • Initial Volumes

  • Open your terminal or command prompt and enter the following code.

      aws ec2 create-volume --availability-zone YOUR_AVAILABILITY_ZONE --size YOUR_SIZE_IN_GB
    

  • The command will return output containing information about the created volume, including its VolumeId. Note down this VolumeId as you'll need it in the next step.

  • Web UI of Volumes after running the command:

Step6: Attach the EBS Volume to the EC2 Instance

  • Open your terminal or command prompt and enter the following code.

      aws ec2 attach-volume --volume-id YOUR_VOLUME_ID --instance-id YOUR_INSTANCE_ID --device /dev/xvdf
    

  • Web UI of Volumes after running the command:

๐Ÿš€ Launch an EC2 instance and an EBS volume, and then attaching the volume to the instance programmatically using API(boto3).

Step1: Launch an EC2 instance

  1. Install Boto3

     pip install boto3
    
  2. Import Boto3 and Create an EC2 Client

     import boto3
    
     # Create an EC2 client
     myec2 = boto3.client('ec2')
    
  3. Launch the EC2 Instance

     myec2.run_instances( 
         ImageId = 'ami-0ded8326293d3201b',
         InstanceType = 't2.micro',
         MaxCount = 1,
         MinCount = 1
     )
     """
     The 'response' variable will contain information about the launched
     instance(s)  including their Instance IDs, Public IP addresses, and other
     details
     """
    

    In the above command:

    • MaxCount: The maximum number of instances to launch. In this example, it's set to 1, so only one instance will be launched.

    • MinCount: The minimum number of instances to launch. In this example, it's also set to 1, meaning one instance will be launched.

Step2: Launch an EBS volume

  1. Run the following code in your Jupyter notebook:

     import boto3
    
     # Create an EC2 resource using the default AWS CLI configuration
     ec2_resource = boto3.resource('ec2')
    
     # Create an EBS volume using the resource interface
     ebs_volume = ec2_resource.create_volume(
         AvailabilityZone='ap-south-1a',  # Replace with your desired availability zone
         Size=8,  # Replace with the desired size of the EBS volume in GiB
         VolumeType='gp2'  # Replace with the desired volume type (e.g., 'gp2', 'io1', 'st1', etc.)
     )
    
     # Extract the volume ID from the EBS volume object
     volume_id = ebs_volume.id
    

  2. Web UI of Volumes after running the command:

Step3: Attach the EBS Volume to the EC2 Instance

  1. Run the following code in your Jupyter Notebook:

     import boto3
     #create an EC2 instance using the default AWS CLI configuration
     ec2_resource = boto3.resource('ec2') 
     # Specify the IDs of the existing EBS volume and EC2 instance
     existing_volume_id = 'vol-097a95e0023d8dc73'  # Replace with the new volume Id you want to attach
     existing_instance_id = 'i-0c0c82259193babf4'  # Replace with the ID of EC2 instance you have
     # Get the EBS volume and EC2 instance objects
     volume = ec2_resource.Volume(existing_volume_id)
     instance = ec2_resource.Instance(existing_instance_id)
     response = volume.attach_to_instance(
         Device='/dev/xvdg',  # Replace with the desired device name for the volume on the instance
         InstanceId= existing_instance_id
     )
    

    ๐Ÿ’ก
    you cannot have the same device name for multiple volumes attached to the same EC2 instance

  2. CONCLUSION:

๐Ÿ“ข Creating an SNS (Simple Notification Service) using the AWS Command Line Interface (CLI).

Amazon SNS allows you to create topics to which you can publish messages and subscribe other AWS resources or endpoints to receive those messages.

  • Step 1: Open the AWS CLI or terminal on your local machine.

  • Step 2: Run the create-topic command with the --name parameter to specify the name of the SNS topic you want to create. For example:

      aws sns create-topic --name MySnsTopic
    

    This command will create an SNS topic named "MySnsTopic". The output of the command will provide you with the ARN (Amazon Resource Name) of the newly created topic, which you can use to publish messages or subscribe other resources.

  • step3: (Optional) You can also set a display name for the topic using the --attributes parameter. For example:

    aws sns create-topic --name MySnsTopic --attributes DisplayName="My SNS Topic"

ย