Boto3 is Software Development Kit (SDK) python allows developers acccess to AWS resources in code. Boto3 relies on AWS profile in local (Ex: ~/.aws/credentials) to connect with AWS.

pip install boto3


Often time we interact with dynamodb, cognito, s3 and so on. We use client, resource or session method in boto3, which can make people confuse about when we use which one and different between them.



            
              import boto3
              def get_user_profile(username, key='username'):
                  db = boto3.resource('dynamodb')
                  table = db.Table(DB_USERS_TABLE)
                  response = table.query(IndexName=f'{key}-Id', KeyConditionExpression=Key(f'{key}').eq(username)).get("Items")
                  return response[0]


              def authenticate_and_get_token(username: str, password: str) -> str:
                  client = boto3.client('cognito-idp')
                  resp = client.admin_initiate_auth(
                      UserPoolId=USER_POOL_ID,
                      ClientId=CLIENT_ID,
                      AuthFlow='ADMIN_NO_SRP_AUTH',
                      AuthParameters={
                          "USERNAME": "username",
                          "PASSWORD": "password"
                      }
                  )
                  return resp
            
          

Actually, Client and Resource both are class to make AWS service request. Resource has higher level than Client because of building on object-oriented API while Client is just normal class. However, it is not meant to Resource better than Client, several serivces are not supported by Resource. Session is large concept, it allows us create client or resource and store configure information during use boto3.



Image


Severely, boto3 it not a available libtary in AWS lambda function. Developers need to install boto3 in layer. Layer simplely is a place to install library, package which lambda function is not on hand. Technically, It is a zip file archive that contains supplementary code or data. Layers usually contain library dependencies, a custom runtime, or configuration files.

We install boto3 in folder.


pip install boto3 -t _layers/python

Then declare in SAM to push on lambda function.


Image


sam deploy --template-file aws_cfts/sqa-lambdas.yaml --stack-name sqa-lambdas --resolve-s3 --profile developer --confirm-changeset


Result.

Image

Compare to azure function, It's quite different, all function in function app can use same pacakges that means we jsut declare library in requirement.txt (for python) or package.json (for node) then Azure automatically install them. All function in function app can use these libraries

Steve Nguyen

© Steve CV. All rights reserved.
Design - TemplateFlip