Serverless multi region 설정 (lambda@Edge region)

손연주·2022년 3월 25일
1

하나의 레포지토리에 각각 다른 리전으로 함수를 배포해야 됐다. 기존에 미리 서울 리전에 배포된 함수가 있어서, 처음에는 serverless.yml에 두 개의 리전을 설정할 수 있는 법을 찾아봤다. serverless 자체에서 제공하는 multi-region plugin을 찾아서 시도했더니 cloudFormation 설정을 따로 해주어야 하는 것 같았다. 하지만 cloudFormation없이 미리 만들어진 cloudFront를 이용해야 됐으므로 다른 방법을 찾아 떠났다.. 하지만 2-3일째 마땅한 방법을 찾지 못하였고 설상가상 배포를 위해선 sls deploy를 해야 되는데 그럼 미리 등록된 함수에 영향이 미쳤다. (cloudFront에서 update_failed로 상태가 변경된다든지..) 그래서 함수를 지정하여 그것만 배포하는 방법을 찾았고 방법은 아래와 같다.

sls deploy -f functionname

그런데 이걸 진행하려면 sls deploy를 무조건 선행해야 됐다. 그러면

Error: Unable to setup base domain mappings for 'serverless-api.thedatahunt.com':
Unable to create base path mapping for 'serverless-api.thedatahunt.com':
ApiMapping key already exists for this domain name

와 같은 에러가 나거나 위와 같이 cloudFront 상태가 변경돼서 적절하지 않았다. 따라서 해당 오류를 해결하기 위해 구글링을 하다가..

How to deploy a single function without deploying other functions on the api using serverless framework
해당 문서를 발견하였고 여기서 답을 찾았다.

각 함수에 대해 별도의 .yaml 파일을 생성하여 이를 사용할 수 있다는 내용이었고, 이렇게 하게되면 기존의 함수에 영향을 끼치지 않고 deploy 또한 가능했다.
여기서 주의할 점은 sls deploy는 디폴트로 serverless.yml 파일을 참고하여 deploy하기 때문에 sls deploy --config <yml파일 경로>를 지정하여 deploy해준다.

나는 lambda@Edge를 위해 새롭게 yml 파일을 만든 거여서 파일 이름을 serverless-edge.yml로 지정해주었다.

따라서
sls create_domain --config serverless-edge.yml
sls deploy --config serverless-edge.yml
먼저 해주고
sls deploy -f 함수이름 --config serverless-edge.yml
이런 식으로 진행해주면 문제없이 배포할 수 있다.

배포 도중

Error:
Error: Unable to create domain 'serverless-api.thedatahunt.com':
Failed to create custom domain 'serverless-api.thedatahunt.com':
Invalid certificate ARN: arn:aws:acm:ap-northeast-... Certificate must be in 'us-east-1'.
...

이러한 내용의 error를 마주했고, 찾아보니 us-east-1와 ap-northeast-2는 다른 certificateArn, 즉 해당 region의 arn을 써야하기 때문에 us-east-1의 arn 키로 변경해주었다.

son-yeonju@MacBook-Pro-4 datahunt-serverless-api %  sls deploy --config serverless-edge.yml      
Deploying ...-resize-thumbnail to stage prod (us-east-1)
SERVERLESS-IGNORE: Loaded .slsignore
         - *.log
         - .env
         - .git/*
         - __tests__/*
Compiling with Typescript...
Using local tsconfig.json - tsconfig.json
Typescript compiled.

✔ Service deployed to stack datahunt-resize-thumbnail-prod (158s)

endpoint: GET - https://...
functions:
  resizingThumbnail: ... (21 MB)
Serverless Domain Manager:
  Domain Name: s...
  Target Domain: ...us-east-1.amazonaws.com
  Hosted Zone Id: Z...

최종적으로 배포에 성공하여 ap-northeast-2 region에 배포되어있는 함수는 serverless.yml에서, us-east-1은 serverless-edge.yml 에서 관리하도록 설정했다.

주의점

  1. us-east-1(미국 버지니아) 와 ap-northeast-2(서울) arn 키가 달라야 함
  2. lambda@Edge는 환경변수를 사용할 수 없다.
  3. lambda Edge는 us-east-1에 배포되어야 한다.
profile
할 수 있다는 생각이 정말 나를 할 수 있게 만들어준다.

0개의 댓글