tjc4
tjc4

Reputation: 301

How to convert code to an AWS Lambda function?

I wrote some Python code that requests data from two APIs, opens a .CSV file, adds the API data to the .CSV as a new row, and saves the updated .CSV file.

Runs great on my local machine.

Now I'd like to turn this code into an AWS Lambda function (triggered 1x per hour by CloudWatch with .CSV stored in S3) but not sure how.

I think I figured out the deployment package part. I have a .zip with dependencies. Now I think I just need to add a .py file with my function.

And the trigger is straightforward.

But not sure how to turn my code into a function.

Could someone tell me how to turn my code into a Lambda function?

import requests
import json
import csv
import pandas as pd
import time

# OpenWeatherMap parameters
OWM_parameters = {"lat": 30.4013804, "lon": -97.6863783, "units": 'imperial', "APPID": 'redacted'}

# OpenWeatherMap API call 
OWM_response = requests.get("http://api.openweathermap.org/data/2.5/weather",params=OWM_parameters)
OMW_json_data = OWM_response.content 
OMW_json_parsed = json.loads(OMW_json_data) 

# OWM does not provide rain data if it's not raining 
# This code avoids an error by setting rain = 0 if API data not provided 
try:
    rain = OMW_json_parsed['rain']['1h']
except:
    rain = 0

#USGS Parameters 
USGS_parameters = {'format':'json', "sites": '08158200', "parameterCd": '00065', 'siteStatus': 'all'}

#USGS API call
USGS_response = requests.get("https://waterservices.usgs.gov/nwis/iv/", params=USGS_parameters)
USGS_json_data = USGS_response.content 
USGS_json_parsed = json.loads(USGS_json_data) 

# Pulling creek level from USGS json
creek_level_ft = float(USGS_json_parsed['value']['timeSeries'][0]['values'][1]['value'][0]['value'])

# creates dictionary from API data 
new_weather_data = {'timestamp': time.time(), 'temp': OMW_json_parsed['main']['temp'], 'temp_min': OMW_json_parsed['main']['temp_min'], 'temp_max': OMW_json_parsed['main']['temp_max'], 'pressure': OMW_json_parsed['main']['pressure'], 'humidity': OMW_json_parsed['main']['humidity'], 'visibility': OMW_json_parsed['visibility'], 'wind_speed': OMW_json_parsed['wind']['speed'], 'wind_dir': OMW_json_parsed['wind']['deg'], 'cloud_cover': OMW_json_parsed['clouds']['all'], 'sunrise': OMW_json_parsed['sys']['sunrise'], 'sunset': OMW_json_parsed['sys']['sunset'], 'precipitation': rain, 'creek_level_ft': creek_level_ft}

# open CSV
df = pd.read_csv('weather_data.csv')

# appends CSV with dictionary
df = df.append([new_weather_data], sort=False)

# saves CSV
df.to_csv('weather_data.csv',index=False)

Upvotes: 3

Views: 4200

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 270274

You can add a .py file to your deployment package (eg lambda_function.py).

Within the file, add a handler, such as:

def lambda_handler(event, context):

Put your code inside the function.

Then, tell Lambda the function reference in the format filename.function-name, such as lambda_function.lambda_handler. The Lambda system will then call your lambda_handler() function when it is triggered.

It seems that you are creating temporary files. If so, put them in the /tmp directory and delete them after use. You can copy them to Amazon S3 using copy_object() in boto3.

Upvotes: 6

Related Questions