2.1단계: 배포 패키지 생성 - 아마존 ElastiCache

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

2.1단계: 배포 패키지 생성

현재 Lambda 함수의 예제 코드는 Python에서만 제공됩니다.

Python

다음의 예제 Python 코드는 항목을 읽어서 ElastiCache 클러스터에 기록합니다. 코드를 복사한 후 app.py 파일에 저장합니다. 코드의 elasticache_config_endpoint 값을 1단계에서 복사한 엔드포인트 주소로 바꿔야 합니다.

import uuid import ssl from pymemcache.client.base import Client elasticache_config_endpoint = "serverlesscacheforlambda-ces85m.serverless.use1.cache.amazonaws.com" target_port = 11211 context = ssl.create_default_context() memcached_client = Client((elasticache_config_endpoint, target_port), tls_context=context) def lambda_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 memcached_client.set("uuid", uuid_in, expire=500, noreply=False) # get the item (UUID) from the cache result = memcached_client.get("uuid") decoded_result = result.decode("utf-8") # check the retrieved item matches the item added to the cache and print # the results if decoded_result == uuid_in: print(f"Success: Inserted {uuid_in}. Fetched {decoded_result} from Memcached.") else: raise Exception(f"Bad value retrieved. Expected {uuid_in}, got {decoded_result}") return "Fetched value from Memcached"

이 코드는 Python pymemcache 라이브러리를 사용하여 항목을 캐시에 넣고 검색합니다. pymemcache가 포함된 배포 패키지를 만들려면 다음 단계를 수행합니다.

  1. app.py 소스 코드 파일이 포함된 프로젝트 디렉터리에서 package 폴더를 만들어 pymemacache 라이브러리를 설치합니다.

    mkdir package
  2. pip를 사용하여 pymemcache를 설치합니다.

    pip install --target ./package pymemcache
  3. pymemcache 라이브러리가 포함된 .zip 파일을 생성합니다. Linux 및 macOS에서 다음 명령을 실행합니다. Windows에서 선호하는 zip 유틸리티를 사용하여 루트에 pymemache 라이브러리가 포함된 .zip 파일을 생성합니다.

    cd package zip -r ../my_deployment_package.zip .
  4. 함수 코드를 .zip 파일에 추가합니다. Linux 및 macOS에서 다음 명령을 실행합니다. Windows에서 선호하는 zip 유틸리티를 사용하여 .zip 파일의 루트에 app.py를 추가합니다.

    cd .. zip my_deployment_package.zip app.py

다음 단계

2.2단계: IAM 역할 생성(실행 역할)