AWS SDK Part 1



AWS SDK - Part 1


AWS provides three types of interfaces to interact with it. The first one is the web UI or also called the console(This is actually the web UI and not a cli). However, sometimes doing tasks via the web UI might not be convenient like automation pipelines. Thus we also have the AWS CLI which via Secret Access Key provides the opportunity to interact via command line tool and extract or pass information in json or yaml format. Lastly, AWS also provides us with the SDK/API interface on which the AWS CLI is written to interact.


Soo what we can do with the AWS SDK ? Well many things. Some common use cases might be:

- Connecting to AWS Native services like DynamoDB, S3 or SQS
    - You need to use the SDK as those services were developed by AWS and the interface to interact with them is within the SDK.
- Writing scripts to automate tasks like reports for resources you have in AWS
    - For example how many Virtual Machines do I have in my account based on tags
    - Automating things like dynamically adding an IP address into a Security Group
- Although not really popular, but you can also use the SDK as Infrastructure as Code
    - Create/Update/Delete resources and follow their state


Supported languages:
    - C++
    - Go
    - Java
    - JavaScript
    - Kotlin
    - .NET
    - Node.js
    - PHP
    - Python
    - Ruby
    - Rust
    - Swift


Let’s create an example simple application which connects to AWS SQS from Python.

Step 1

    Install AWS-CLI and add your access keys which have the correct IAM permissions to utilise SQS. If you have not done this you can follow:
    https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html - To install AWS CLI
    https://docs.aws.amazon.com/cli/latest/userguide/getting-started-quickstart.html - To configure it

Step 2
    
    Install “boto3” via pip command.  pip3 install boto3  and import the package.

Step 3

    Write our code:

import boto3

#Set which service are we going to work with
sqs = boto3.resource('sqs')

#Set the exact resource (queue name) to which we want to connect
#As we can have multiple ones
queue = sqs.get_queue_by_name(QueueName='demo-sqs-queue')

#Call the receive message method which will give return us a message
for message in queue.receive_messages(AttributeNames=['All']):
print("Message received:")
#Print the message id we are working with
print("Message ID: " + message.message_id)
#Print the message body content
print("Message Body: " + message.body)
print('==================================')
print("Approximate number of remaining messages: " + queue.attributes.get('ApproximateNumberOfMessages'))
print("Approximate number of not visible messages: " + queue.attributes.get('ApproximateNumberOfMessagesNotVisible'))
print('==================================')

As we can see with just a few lines we can get a message from our queue


Ok, but how to do we create messages and how to handle our format ? Well the message body is just a text, so you can pass json, yaml or even plain text.

import boto3
import random
import string

sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName='demo-sqs-queue')

def main():
send_message()

#Generate random string
def get_random_string(length):
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str

#Method for sending messages
def send_message():
#Generate a random 8 char long string
ran_string = get_random_string(8)

#Send our message to the queue
queue.send_message(MessageBody=ran_string)
#Print our message in the console for Debug
print("Message is: " + ran_string)
main()


if __name__ == "__main__":
main()

Well this is quite simple again. As we can see in the example, we are just sending 8 char long strings constantly until we stop the python script. Just here instead of calling the receive message method we are calling the send message with an argument containing our message content as a string.

 

Please keep in mind that this is just a very simple example we can begin to interact with the SDK. In Real production environment you will have much more complexity. Stay tunned for part 2 !


2022-10-22 08:59:42 Cloud, AWS, SDK, Python