Menu
AWS Lambda
Developer Guide

Step 2.1: Create a Deployment Package

From the Filter View list, choose the language you want to use for your Lambda function. The appropriate section appears with code and specific instructions for creating a deployment package.

Node.js

Follow the instructions to create a AWS Lambda function deployment package.

  1. Open a text editor, and then copy the following code.

    Copy
    console.log('Loading function'); var AWS = require('aws-sdk'); var dynamo = new AWS.DynamoDB.DocumentClient(); /** * Provide an event that contains the following keys: * * - operation: one of the operations in the switch statement below * - tableName: required for operations that interact with DynamoDB * - payload: a parameter to pass to the operation being performed */ exports.handler = function(event, context, callback) { //console.log('Received event:', JSON.stringify(event, null, 2)); var operation = event.operation; if (event.tableName) { event.payload.TableName = event.tableName; } switch (operation) { case 'create': dynamo.put(event.payload, callback); break; case 'read': dynamo.get(event.payload, callback); break; case 'update': dynamo.update(event.payload, callback); break; case 'delete': dynamo.delete(event.payload, callback); break; case 'list': dynamo.scan(event.payload, callback); break; case 'echo': callback(null, "Success"); break; case 'ping': callback(null, "pong"); break; default: callback('Unknown operation: ${operation}'); } };

    Note

    The code sample is compliant with the Node.js runtimes v6.10 or v4.3. For more information, see Programming Model (Node.js)

  2. Save the file as LambdaFunctionOverHttps.js.

  3. Zip the LambdaFunctionOverHttps.js file as LambdaFunctionOverHttps.zip.

Next Step

Step 2.2: Create the Execution Role (IAM Role)

Python

Follow the instructions to create AWS Lambda function deployment package.

  1. Open a text editor, and then copy the following code.

    Copy
    from __future__ import print_function import boto3 import json print('Loading function') def handler(event, context): '''Provide an event that contains the following keys: - operation: one of the operations in the operations dict below - tableName: required for operations that interact with DynamoDB - payload: a parameter to pass to the operation being performed ''' #print("Received event: " + json.dumps(event, indent=2)) operation = event['operation'] if 'tableName' in event: dynamo = boto3.resource('dynamodb').Table(event['tableName']) operations = { 'create': lambda x: dynamo.put_item(**x), 'read': lambda x: dynamo.get_item(**x), 'update': lambda x: dynamo.update_item(**x), 'delete': lambda x: dynamo.delete_item(**x), 'list': lambda x: dynamo.scan(**x), 'echo': lambda x: x, 'ping': lambda x: 'pong' } if operation in operations: return operations[operation](event.get('payload')) else: raise ValueError('Unrecognized operation "{}"'.format(operation))

  2. Save the file as LambdaFunctionOverHttps.py.

  3. Zip the LambdaFunctionOverHttps.py file as LambdaFunctionOverHttps.zip.

Next Step

Step 2.2: Create the Execution Role (IAM Role)

On this page: