In general, AWS services can be accessed using
- AWS web interface,
- API libraries in a programming language, such as
boto3for Python 3, - AWS command-line interface, i.e.
awscli.
I opted for the API library since it is
| pipeline { | |
| options { | |
| buildDiscarder(logRotator(numToKeepStr: '10')) // Retain history on the last 10 builds | |
| ansiColor('xterm') // Enable colors in terminal | |
| timestamps() // Append timestamps to each line | |
| timeout(time: 20, unit: 'MINUTES') // Set a timeout on the total execution time of the job | |
| } | |
| agent { | |
| // Run this job within a Docker container built using Dockerfile.build | |
| // contained within your projects repository. This image should include |
| def s3_to_pandas(client, bucket, key, header=None): | |
| # get key using boto3 client | |
| obj = client.get_object(Bucket=bucket, Key=key) | |
| gz = gzip.GzipFile(fileobj=obj['Body']) | |
| # load stream directly to DF | |
| return pd.read_csv(gz, header=header, dtype=str) | |
| def s3_to_pandas_with_processing(client, bucket, key, header=None): |
| provider "aws" { } | |
| resource "aws_iam_role" "iam_for_lambda" { | |
| name = "iam_for_lambda" | |
| assume_role_policy = <<EOF | |
| { | |
| "Version": "2012-10-17", | |
| "Statement": [ | |
| { |
| { | |
| "Version": "2012-10-17", | |
| "Statement": [ | |
| { | |
| "Action": "sts:AssumeRole", | |
| "Principal": { | |
| "Service": "ec2.amazonaws.com" | |
| }, | |
| "Effect": "Allow", | |
| "Sid": "" |
| import boto3 | |
| # you can assign role in the function like below | |
| # ROLE_ARN = 'arn:aws:iam::01234567890:role/my_role' | |
| # | |
| # or you can pass role as an evironment varibale | |
| # ROLE_ARN = os.environ['role_arn'] | |
| ROLE_ARN = = os.environ['role_arn'] |
| import boto3 | |
| from boto3.session import Session | |
| def assume_role(arn, session_name): | |
| """aws sts assume-role --role-arn arn:aws:iam::00000000000000:role/example-role --role-session-name example-role""" | |
| client = boto3.client('sts') | |
| account_id = client.get_caller_identity()["Account"] | |
| print(account_id) |
This article walks you through an example of deploying a Python 3.6 application that uses Pandas and AWS S3 on AWS Lambda using Boto3 in Python in 2018. No shell, no bash, no web console, everything is automated in Python. The previous article of a Hello World example can be found here.
Again, the reason to use Python Boto3 to interact with AWS is that,
| ############ REQUIREMENTS #################### | |
| # sudo apt-get install python-pip | |
| # sudo apt-get install libpq-dev | |
| # sudo pip install psycopg2 | |
| # sudo pip install sqlalchemy | |
| # sudo pip install sqlalchemy-redshift | |
| ############################################## | |
| import sqlalchemy as sa | |
| from sqlalchemy.orm import sessionmaker |