We are using Boto3 which is the Amazon Web Services (AWS) SDK for Python, which allows Python developers to write software that makes use of Amazon services like S3 and EC2. Boto provides an easy to use, object-oriented API as well as low-level direct service access.
My Lambda job is written in Python, so select Python 2.7 as your run time and we are reading JSON data from S3 bucket.
Step 1: Create role for lambda service to use S3.
Use IAM service to create role.
Now Create Lambda function on python 2.7 with existing created role.
import json
import boto3
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket = '2jan2018.test'
key = 'data/sample_data.json'
try:
data = s3.get_object(Bucket=bucket, Key=key)
json_data = data['Body'].read()
return json_data
except Exception as e:
print(e)
raise e
Note: lambda_handler method is an entry point for Lambda.
bucket variable holds the bucket name and key holds the object location inside bucket.
after that, simple python code to read and return json data.
Below is my dummy JSON on s3:
Below is the LAMBDA output:
Response:
"{\n \"glossary\": {\n \"title\": \"example glossary\",\n\t\t\"GlossDiv\": {\n \"title\": \"S\",\n\t\t\t\"GlossList\": {\n \"GlossEntry\": {\n \"ID\": \"SGML\",\n\t\t\t\t\t\"SortAs\": \"SGML\",\n\t\t\t\t\t\"GlossTerm\": \"Standard Generalized Markup Language\",\n\t\t\t\t\t\"Acronym\": \"SGML\",\n\t\t\t\t\t\"Abbrev\": \"ISO 8879:1986\",\n\t\t\t\t\t\"GlossDef\": {\n \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n\t\t\t\t\t\t\"GlossSeeAlso\": [\"GML\", \"XML\"]\n },\n\t\t\t\t\t\"GlossSee\": \"markup\"\n }\n }\n }\n }\n}\n\n"
Request ID:
"a461ee3b-efe5-11e7-a3b4-dfd45b7e2736"
Function Logs:
START RequestId: a461ee3b-efe5-11e7-a3b4-dfd45b7e2736 Version: $LATEST
END RequestId: a461ee3b-efe5-11e7-a3b4-dfd45b7e2736
REPORT RequestId: a461ee3b-efe5-11e7-a3b4-dfd45b7e2736 Duration: 318.56 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 44 MB
Below is my dummy JSON on s3:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
Response:
"{\n \"glossary\": {\n \"title\": \"example glossary\",\n\t\t\"GlossDiv\": {\n \"title\": \"S\",\n\t\t\t\"GlossList\": {\n \"GlossEntry\": {\n \"ID\": \"SGML\",\n\t\t\t\t\t\"SortAs\": \"SGML\",\n\t\t\t\t\t\"GlossTerm\": \"Standard Generalized Markup Language\",\n\t\t\t\t\t\"Acronym\": \"SGML\",\n\t\t\t\t\t\"Abbrev\": \"ISO 8879:1986\",\n\t\t\t\t\t\"GlossDef\": {\n \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n\t\t\t\t\t\t\"GlossSeeAlso\": [\"GML\", \"XML\"]\n },\n\t\t\t\t\t\"GlossSee\": \"markup\"\n }\n }\n }\n }\n}\n\n"
Request ID:
"a461ee3b-efe5-11e7-a3b4-dfd45b7e2736"
Function Logs:
START RequestId: a461ee3b-efe5-11e7-a3b4-dfd45b7e2736 Version: $LATEST
END RequestId: a461ee3b-efe5-11e7-a3b4-dfd45b7e2736
REPORT RequestId: a461ee3b-efe5-11e7-a3b4-dfd45b7e2736 Duration: 318.56 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 44 MB
No comments:
Post a Comment