Unlocking AWS Automation: AWS CLI & AWS SDK

ยท

5 min read

Unlocking AWS Automation: AWS CLI & AWS SDK

Introduction to AWS Acess Options

In AWS, there are 3 ways to interact with services and resources, let's delve into each of the following with some examples of AWS CLI and AWS SDK later.

There are 3 ways on how can we access AWS services:

  1. AWS Management Console

    This is a web-based GUI that provides a user-friendly way to manage AWS resources and services. It is a valuable tool for users who are new to AWS or who prefer a clickable interface. However, this way of accessing the AWS services is quite time-consuming for managing complex or large-scale AWS deployments.

  2. AWS CLI (Command Line Interface)

    This is a command-line tool that allows users to manage AWS resources and services from the command line itself. It's a more powerful and flexible tool than the AWS management console and is well-suited for automating tasks and managing large-scale deployments. However, the AWS CLI can be difficult to learn initially for users who are not much familiar with command line tools and as well as with AWS.

  3. AWS SDK (Software Development Kit)

    AWS SDK are software libraries that allow developers to integrate AWS services into their applications. This provides a programmatic way to access AWS resources and services, and they are essential for building cloud-native applications. However, this is quite complex to learn and use and requires a certain level of programming expertise.

Access AWS services using AWS CLI

Here, we are going to see some examples of creating AWS resources using AWS CLI.

  1. Launch a simple EC2 Instance in a default VPC with AWS CLI.

     aws ec2 create-key-pair --key-name <key-name>
    
     aws ec2 create-key-pair --key-name awscli-demo
    
     aws ec2 run-instances --image-id <image-id> --instance-type <instance-type> --key-name <keypair>
    
     aws ec2 run-instances --image-id ami-093467ec28ae4fe03 --instance-type t2.micro --key-name awscli-demo
    
     aws ec2 describe-instance-status --instance-ids <instance-id>
    

  2. Create a S3 bucket and upload objects into it using AWS CLI.

     aws s3api create-bucket --bucket <bucket-name> --region <region> --create-bucket-configuration LocationConstraint=<region>
    
     aws s3api create-bucket --bucket awscli-demobucket2911 --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2
    
     aws s3api put-object --bucket <created-bucket> --key <object-key> --body <object-data>
    
     aws s3api put-object --bucket awscli-demobucket2911 --key awsclidemo/python.zip --body python.zip
    

  3. Launch a simple EC2 Instance in a custom VPC with AWS CLI.

    Please Try It on your own, I have provided the AWS CLI command below for the same, please do the required modifications & run this command, and later verify the EC2 Instance creation within the public subnet with a public IP associated.

     aws ec2 run-instances \
         --image-id <image-id> \
         --instance-type <instance-type> \
         --subnet-id <subnet-id> \
         --security-group-ids <security-group-id> \
         --associate-public-ip-address \
         --key-name <keypair>
    

Access AWS services using AWS SDK

Here, we are going to see the same 2 examples that we did via AWS CLI in the previous section but with AWS SDK -

  1. Create a simple EC2 Instance with AWS SDK.

    Here, we need to use AWS Lambda service (Serverless) where we are creating a lambda function and using AWS SDK for Python boto3 to create or run EC2 instances. Please add the required permission to your lambda function IAM Role so that it can call other services like EC2 in our case.

    Please refer to the code below.

     import boto3
     import json
    
     ec2_client = boto3.client('ec2')
    
     def lambda_handler(event, context):
         instance_params = {
             'ImageId': 'ami-093467ec28ae4fe03',
             'InstanceType': 't2.micro',
             'MinCount': 1,
             'MaxCount': 1,
             'TagSpecifications': [
                 {
                     'ResourceType':'instance',
                     'Tags': [
                         {
                             'Key': 'Name',
                             'Value': 'AWSSDKInstanceDemo'
                         }
                     ]
                 }
             ]
         }
         response = ec2_client.run_instances(**instance_params)
         InstanceId = response['Instances'][0]['InstanceId']
    
         return {
             'statusCode': 200,
             'body': f'Instance has been launched with Instance Id - {InstanceId}'
         }
    

  2. Create a S3 bucket and upload objects into it using AWS SDK.

    Here, we need to use AWS Lambda service (Serverless) where we are creating a lambda function and using AWS SDK for Python boto3 to create an S3 bucket and upload an object to it. Please add the required permission to your lambda function IAM Role so that it can call other services like S3 in our case.

    Please refer to the code below.

     import boto3
     import json
    
     s3_client = boto3.client('s3')
    
     def lambda_handler(event, context):
         bucket_name = 'awssdkdemo29112023'
         region = 'us-west-2'
         object_name = 'awssdks3demo/file.txt'
         object_content = b'Demo for creating bucket and then uploading object to that bucket'
    
         create_bucket_response = s3_client.create_bucket(
             Bucket=bucket_name,
             CreateBucketConfiguration={
                 'LocationConstraint': region
             }
         )
    
         upload_object_to_bucket_response = s3_client.put_object(
             Bucket=bucket_name,
             Key=object_name,
             Body=object_content 
         )
    
         return {
             'statusCode': 200,
             'body': f'Bucket {bucket_name} created and object {object_name} uploaded.'
         }
    

Reference Links:

AWS CLI Getting Started -

https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html

AWS CLI Reference -

https://docs.aws.amazon.com/cli/latest/reference/

AWS SDK for python (boto3) - https://boto3.amazonaws.com/v1/documentation/api/latest/index.html

AWS EC2 Run Instances (boto3) -

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2/client/run_instances.html

AWS S3 Create Bucket (boto3) -

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/create_bucket.html

AWS S3 Put object to S3 (boto3) -

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/put_object.html

Conclusion

The AWS CLI and SDKs are powerful tools that can be leveraged to automate tasks, streamline processes, and enhance efficiency in your AWS environment. Firstly, we have seen some introduction of these and later, we have also seen some examples of interacting with the AWS resources using AWS CLI and AWS SDK approach.

By mastering these tools, we can unlock the full potential of AWS automation and transform our cloud infrastructure management.

Thank you so much for reading my blog! ๐Ÿ˜Š I hope you found it helpful and informative. If you did, please ๐Ÿ‘ give it a like and ๐Ÿ’Œ subscribe to my newsletter for more of this type of content. ๐Ÿ’Œ

I'm always looking for ways to improve my blog, so please feel free to leave me a comment or suggestion. ๐Ÿ’ฌ

Thanks again for your support!

Connect with me -

LinkedIn - https://www.linkedin.com/in/rachitmishra1997/

Twitter - https://twitter.com/racs1997

#aws #awscommunity #cloudcomputing #cloud

Did you find this article valuable?

Support Cloud & Devops with Rachit by becoming a sponsor. Any amount is appreciated!

ย