AWS Lambda
开发人员指南

步骤 2.1:创建部署程序包

Filter View 列表中,选择要用于 Lambda 函数的语言。适当的部分随即出现,其中包含创建部署程序包的代码和具体说明。

Node.js

按照以下说明创建一个 AWS Lambda 函数部署程序包。

  1. 打开文本编辑器,然后复制以下代码。

    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}'); } };

    注意

    代码示例与 Node.js 运行时 v6.10 或 v4.3 兼容。有关更多信息,请参阅编程模型 (Node.js)

  2. 将该文件保存为 LambdaFunctionOverHttps.js

  3. LambdaFunctionOverHttps.js 文件压缩为 LambdaFunctionOverHttps.zip

下一步

步骤 2.2:创建执行角色(IAM 角色)

Python

按照以下说明创建一个 AWS Lambda 函数部署程序包。

  1. 打开文本编辑器,然后复制以下代码。

    注意

    利用 from __future__ 语句,可以编写与 Python 2 或 3 兼容的代码。如果您使用的是运行时版本 3.6,则不必将它包含在内。

    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. 将该文件保存为 LambdaFunctionOverHttps.py

  3. LambdaFunctionOverHttps.py 文件压缩为 LambdaFunctionOverHttps.zip

下一步

步骤 2.2:创建执行角色(IAM 角色)

转到

按照以下说明创建一个 AWS Lambda 函数部署程序包。

  1. 打开文本编辑器,然后复制以下代码。

    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 }
  2. 将该文件保存为 LambdaFunctionOverHttps.go

  3. LambdaFunctionOverHttps.go 文件压缩为 LambdaFunctionOverHttps.zip

下一步

步骤 2.2:创建执行角色(IAM 角色)

本页内容: