Skip to content

Instantly share code, notes, and snippets.

@satanas
Last active January 13, 2018 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satanas/6f28ae904a2bb1941f22de4c09cbddb7 to your computer and use it in GitHub Desktop.
Save satanas/6f28ae904a2bb1941f22de4c09cbddb7 to your computer and use it in GitHub Desktop.
AWS Lambda 101
  1. First, create your AWS account, set up AWS CLI and AWS SAM. AWS CLI is the command line interface to interact with Lambda and AWS SAM is the tool to test your Lambda functions locally without deploying. Check the Getting Started Documentation for more details.

  2. Install python3 and virtualenv

  3. Develop feature Check main.py file

  4. Create deployment package This is the .zip that will be pushed to Lambda and used by AWS SAM to test locally. To create the zip you can do something like:

# Zip the main file
zip -9r python_test.zip main.py
# Zip all the dependencies
cd lambda/lib/python3.6/site-packages
zip -9r ../../../../python_test.zip *
  1. Create a SAM template This template will allow SAM to invoke your Lambda function locally

  2. Test your function locally with SAM

$ echo '{"first_name": "Foo" }' | sam local invoke "IPGreeter"
  1. Upload to AWS Lambda
aws lambda create-function --region us-west-2 --function-name IPGreeter --zip-file fileb://python_test.zip --role arn:aws:iam::<your_account_id>:role/service-role/myTestRole --handler main.handler --runtime python3.6 --timeout 15 --memory-size 512

You may need to get the full path for your role. In order to do that, execute:

aws iam get-role --role-name myTestRole
import requests
def handler(event, context):
response = requests.get('https://httpbin.org/ip')
return 'Hello, {1}. Your IP address is {0}'.format(response.json()['origin'], event['first_name'])
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My first serverless app.
Resources:
IPGreeter:
Type: AWS::Serverless::Function
Properties:
Handler: main.handler
Runtime: python3.6
CodeUri: 'python_test.zip'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment