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"
상기 코드는 pymemcache
및 elasticache-auto-discovery
라이브러리에 의존합니다. 이들 라이브러리는 pip를 사용하여 설치합니다.
-
pymemcache
- Lambda 함수 코드가 이 라이브러리를 사용하여(pymemcache참조) Memcached에서 항목을 설정하고 가져오는 HashClient
객체를 생성합니다. -
elasticache-auto-discovery
- Lambda 함수가 이 라이브러리를 사용하여(elasticache-auto-discovery참조) Amazon ElastiCache 클러스터에 노드를 가져옵니다.
상기 Python 코드를 이름이 app.py
인 파일에 저장합니다. 그런 다음, 이들 파일을 모두 app.zip 파일에 압축하여 배포 패키지를 생성합니다. 단계별 지침은 배포 패키지 생성(Python)을 참조하세요.
다음 단계