[AWS] Lambda by Python

时间:2023-03-10 08:30:53
[AWS] Lambda by Python

当前统治数据分析的语言还是Python,还是暂时走:Python + GPU的常规路线好了。

numba, pyculib (分装了cublas)

Ref: 使用 Python 构建 Lambda 函数

Ref: Intro to AWS Lambda with Python | AWS Lambda Python Tutorial

AWS Lambda with Python calls S3

创建lambda函数

修改为python环境

注意将Runtime改为Python3.7

[AWS] Lambda by Python

进入IDE

自动提供了基本模板代码。

[AWS] Lambda by Python

测试模板代码

右上角"Test"按钮,打开测试模板代码。

[AWS] Lambda by Python

Python Lambda --> S3

代码展示

import json
import boto3 s3 = boto3.resource('s3') def lambda_handler(event, context):
  bucket_list = []
  for bucket in s3.buckets.all():
    print(bucket.name)
    bucket_list.append(bucket.name)
  return {
    'statusCode': 200
    'body': bucket_list
  }

IAM --> Role

给lambda添加访问S3特定数据的权限。

AWS Lambda with Python calls DynomoDB

创建lambda函数 - get_item

import json
import boto3 dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('planets') def lambda_handler(event, context):
  response = table.get_item(
    Key={
      'id': 'mercury'
    }
  )   print(response)
  return {
    'statusCode':200,
    'body':response
  }

IAM --> Role

给Lambda添加访问DynamoDB特定数据的权限。

然后就能获得数据。

[AWS] Lambda by Python

创建lambda函数 - put_item

import json
import boto3 dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('planets') def lambda_handler(event, context):
  response = table.put_item(
    Item={
      'id': 'neptune',
'temp': 'super cold'
    }
  )   response = {
    'message': 'Item added'
}
  return {
    'statusCode':200,
    'body': response
  }

/* continue */