AWS SAM

Jae Min·2023년 4월 22일

Init $ Test

  1. 환경 셋팅

    $ brew tap aws/tap
    $ brew install aws-sam-cli
    $ brew install esbuild
  2. 초기 설정

    $ sam init

    error > AttributeError: module 'lib' has no attribute 'OpenSSL_add_all_algorithms'

    $ /opt/homebrew/Cellar/aws-sam-cli/1.67.0/libexec/bin/pip install --force-reinstall "cryptography==38.0.4"

  3. 빌드 (template.yaml 파일이 위치한곳) ← brew install esbuild 가 우선적으로 필요하다

    $ sam build
  1. 로컬 테스트

    $ sam local start-api -p 8080
  1. 배포

    • 첫 배포 - 반드시 g 옵션을 사용해야 함
      $ sam deploy -g 
      $ sam deploy --guide
      template.yaml 파일을 참고하여 배포함. 첫 배포 후 samconfig.toml 파일 생성
      • samconfig.toml : 어떤 s3 bucket에 저장할지 어떤 template 파일을 참고하여 배포했는지, cloudformation의 어떤 stack을 사용했는지 등등
    • 두번째 배포 이후
      g 옵션을 달지 않고 배포 (선build 필수)
      사전에 생성된 `samconfig.toml` 파일을 기반으로 배포한다. 

    error case 1

    처음에 옵션 g 를 추가해서 배포하면 cloudformation에 stack이 쌓이는데, 다음에 deploy를 할때 samConfig.toml 파일을 기반으로 deploy를 하기 때문에, 동일 스택에 쌓인다.
    → 다음에 deploy를 할때 g 옵션을 달면, 동일 스택이 있다고 취급하고, 배포가 되지 않는다.

    ```
    Error: Unable to upload artifact HelloWorldFunction referenced by CodeUri parameter of HelloWorldFunction resource.
    
    S3 Bucket does not exist.
    ```

    다음과 같은 에러가 발생하면 aws-cloudformation 에서 해당 스택을 지워준다.

    error case 2

    Error: Security Constraints Not Satisfied! 해당 에러가 나는 것은 y 가 아니라 그냥 무조건적으로 enter 를 눌러서 그렇다
    https://stackoverflow.com/questions/61841136/what-is-the-cause-of-the-security-constraints-not-satisfied-error-when-using-sam

  1. 새로운 lambda 함수가 생긴다.

    1. 자동으로 api gateway 를 하나 만들어서 트리거로 걸어준다.
    2. 자동으로 s3를 생성하여 안에 람다 소스코드를 넣어준다.
    3. 자동으로 cloudformation에 stack을 생성해준다.

VPC access

우리가 사용하는 MQ는 모두 private subnet 였기 때문에, lambda에서 vpc 설정을 안해주면 private MQ에 메세지를 보낼 수 없다.

lambda → 해당 함수 → 구성 → VPC

vpc, subnet, 보안그룹 설정 필요

ERROR : **The provided execution Role does not have Permissions**

https://bobbyhadz.com/blog/aws-lambda-provided-execution-role-does-not-have-permissions


AWS Skill

cloudformation

템플릿을 생성하여 AWS에서 필요한 리소스 및 속성을 관리

sam (serverless application model)

람다함수에서 필요한 기본 템플릿 제공, 빌드 및 배포, 각 람다함수에서 필요한 트리거 및 속성을 관리할 수 있는 서비스

secrets manager

sam 으로 deploy를 하면 메인 함수만 올리기 때문에 env 파일을 같이 올릴 수 없다.

→ 따로 env key 를 관리할 수 있는 서비스필요


Docs

aws secrets manager

How to use AWS secret manager and SES with AWS SAM

cloudformation

Deploying a Lambda with CloudFormation - Octopus Deploy

[AWS] AWS CloudFormation 의 개념


Auto-Scaling

How to auto-scale an AWS lambda using auto-scaling service

람다는 자동적으로 auto scaling을 해준다.

profile
자유로워지고 싶다면 기록하라.

0개의 댓글