步骤 2.1:创建部署包 - Amazon ElastiCache
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

步骤 2.1:创建部署包

目前 Lambda 函数的示例代码仅在 Python 中提供。

Python

以下 Python 代码示例在 #ELC; 集群中读取和写入项目。复制代码并将其保存到名为 app.py 的文件中。

from __future__ import print_function import time import uuid import sys import socket import elasticache_auto_discovery from pymemcache.client.hash import HashClient # ElastiCache settings elasticache_config_endpoint = "your-elasticache-cluster-endpoint:port" nodes = elasticache_auto_discovery.discover(elasticache_config_endpoint) nodes = map(lambda x: (x[1], int(x[2])), nodes) memcache_client = HashClient(nodes) ###### # This function puts into memcache and get from it. # Memcached is hosted using elasticache ###### def handler(event, context): # Create a random UUID... this will be the sample element we add to the cache. uuid_in = uuid.uuid4().hex # Put the UUID to the cache. memcache_client.set('uuid', uuid_in) # Get the item (UUID) from the cache. uuid_out = memcache_client.get('uuid') # Print the results if uuid_out == uuid_in: # this print should see the CloudWatch Logs and Lambda console. print "Success: Inserted: %s. Fetched %s from memcache." %(uuid_in, uuid_out) else: raise Exception("Bad value retrieved :(. Expected %s got %s." %(uuid_in, uuid_out)) return "Fetched value from Memcached"

前面的代码依赖于 pymemcacheelasticache-auto-discovery 库。使用 pip 安装这些库。

  • pymemcache - Lambda 函数代码使用此库(请参阅 pymemcache)创建 HashClient 对象,该对象从 Memcached 中设置和获取项目。

  • elasticache-auto-discovery - Lambda 函数使用此库(请参阅 elasticache-auto-discovery)来获取 Amazon ElastiCache 集群中的节点。

将上面的 Python 代码保存在名为 app.py 的文件中。将所有这些文件压缩成名为 app.zip 的文件来创建您的部署包。有关分步说明,请参阅 Creating a Deployment Package (Python)(创建部署包(Python))。

下一步

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