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 an AWS Lambda function deployment package.
-
Open a text editor, and then copy the following code.
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)
-
Save the file as
LambdaFunctionOverHttps.js
. -
Zip the
LambdaFunctionOverHttps.js
file asLambdaFunctionOverHttps.zip
.
Next Step
Python
Follow the instructions to create an AWS Lambda function deployment package.
-
Open a text editor, and then copy the following code.
Note
The
from __future__
statement enables you to write code that is compatible with Python 2 or 3. If are you using runtime version 3.6, it is not necessary to include it.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))
-
Save the file as
LambdaFunctionOverHttps.py
. -
Zip the
LambdaFunctionOverHttps.py
file asLambdaFunctionOverHttps.zip
.
Next Step
Go
Follow the instructions to create an AWS Lambda function deployment package.
-
Open a text editor, and then copy the following code.
import ( "strings" "github.com/aws/aws-lambda-go/events" ) func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { fmt.Printf("Processing request data for request %s.\n", request.RequestContext.RequestId) fmt.Printf("Body size = %d.\n", len(request.Body)) fmt.Println("Headers:") for key, value := range request.Headers { fmt.Printf(" %s: %s\n", key, value) } return events.APIGatewayProxyResponse { Body: request.Body, StatusCode: 200 }, nil }
-
Save the file as
LambdaFunctionOverHttps.go
. -
Zip the
LambdaFunctionOverHttps.go
file asLambdaFunctionOverHttps.zip
.