AWS Classroomnotes 08/Mar/2023

Serverless

  • To run the code with web interfaces, we use servers (web/app/..) and we run them continuously for the application to be accesible & available
  • Serverless technology is all about writing code as a function.
  • This function will be executed only when the calls are made and the results will be processed
  • The Serverless technology provider internally runs the server when the calls are made..
  • AWS Lambda is Serverless Provider who enables to write code using
    • java
    • c#
    • python
    • javascript
    • go
    • php
  • Charges will be applied only for the duration when the function is getting executed.
  • Pricing will be in the units of milliseconds Refer Here
  • Serverless technology has no common standards, so we need to rewrite the code.
  • Lets create our first lambda
    Preview
    Preview
    Preview
  • From AWS lambda we can use boto3 which is sdk to interact with AWS Refer Here
  • Exercise:

    • Try to find the code to start ec2 instances by tag using boto3
    • Try to find the code to terminate databases based on tags
    • Create a hello world python lambda function.
  • Refer Here here some boto3 examples
import boto3

# create an EC2 client object
ec2 = boto3.client('ec2')

# create an empty list to store the instance IDs
instance_ids = []

# get information about all EC2 instances
response = ec2.describe_instances()

# loop through the instances and check if they have a "Name" tag
for reservation in response['Reservations']:
    for instance in reservation['Instances']:
        for tag in instance['Tags']:
            if tag['Key'] == 'Name':
                instance_ids.append(instance['InstanceId'])

# print the instance IDs that have a "Name" tag
print(instance_ids)
ec2.start_instances(instance_ids)
  • The lambda function
import json
import boto3
from botocore.config import Config



def lambda_handler(event, context):
    my_config = Config(
    region_name = 'us-west-2')

    ec2 = boto3.client('ec2', config=my_config)
    # create an empty list to store the instance IDs
    instance_ids = []

    # get information about all EC2 instances
    response = ec2.describe_instances()

    # loop through the instances and check if they have a "Name" tag
    for reservation in response['Reservations']:
        for instance in reservation['Instances']:
            for tag in instance['Tags']:
                if tag['Key'] == 'Name':
                    instance_ids.append(instance['InstanceId'])
                    ec2.start_instances(instance['InstanceId'])
                    #ec2.stop_instances(instance['InstanceId'])
                    #ec2.terminate_instances(instance['InstanceId'])

    # print the instance IDs that have a "Name" tag
    print(instance_ids)
    return {
        'statusCode': 200,
        'body': json.dumps(instance_ids)
    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner