본문 바로가기

⭐ AWS/SAM (ServerlessApplicationModel)

SAM (ServerlessApplicationModel) 기본 생성

# SAM 써보기

- AWS 공식 가이드는 아래와 같다.

https://docs.aws.amazon.com/ko_kr/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html

 

사용 지침서: Hello World 애플리케이션 배포 - AWS Serverless Application Model

로컬 기기에 Python이 설치되어 있지 않은 경우, sam build --use-container 명령어를 대신 사용합니다. AWS SAMCLI는 함수의 런타임과 종속물을 포함하는 Docker 컨테이너를 생성합니다. 이 명령은 로컬 기기

docs.aws.amazon.com

- 공식 가이드

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started.html

 

Getting started with AWS SAM - AWS Serverless Application Model

Thanks for letting us know this page needs work. We're sorry we let you down. If you've got a moment, please tell us how we can make the documentation better.

docs.aws.amazon.com

1. 일단 지금 사용하는 PC에 해당 프로그램들이 설치 되어 있는지 확인한다.

aws --version && sam --version && docker --version && node --version && npm --version

- 일단 다 설치되어 있는거 확인

2. SAM Application 생성하기 (빨간 단어 확인)

C:\Users\김승현\Desktop\SAM>sam init

You can preselect a particular runtime or package type when using the `sam init` experience.
Call `sam init --help` to learn more.

Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location
Choice: 1

Choose an AWS Quick Start application template
        1 - Hello World Example
        2 - Data processing
        3 - Hello World Example with Powertools for AWS Lambda
        4 - Multi-step workflow
        5 - Scheduled task
        6 - Standalone function
        7 - Serverless API
        8 - Infrastructure event management
        9 - Lambda Response Streaming
        10 - Serverless Connector Hello World Example
        11 - Multi-step workflow with Connectors
        12 - GraphQLApi Hello World Example
        13 - Full Stack
        14 - Lambda EFS example
        15 - Hello World Example With Powertools for AWS Lambda
        16 - DynamoDB Example
        17 - Machine Learning
Template: 1

Use the most popular runtime and package type? (Python and zip) [y/N]: N

Which runtime would you like to use?
        1 - aot.dotnet7 (provided.al2)
        2 - dotnet6
        3 - go1.x
        4 - go (provided.al2)
        5 - go (provided.al2023)
        6 - graalvm.java11 (provided.al2)
        7 - graalvm.java17 (provided.al2)
        8 - java21
        9 - java17
        10 - java11
        11 - java8.al2
        12 - java8
        13 - nodejs20.x
        14 - nodejs18.x
        15 - nodejs16.x
        16 - python3.9
        17 - python3.8
        18 - python3.12
        19 - python3.11
        20 - python3.10
        21 - ruby3.2
        22 - ruby2.7
        23 - rust (provided.al2)
        24 - rust (provided.al2023)
Runtime: 15

What package type would you like to use?
        1 - Zip
        2 - Image
Package type: 2

Based on your selections, the only dependency manager available is npm.
We will proceed copying the template using npm.

Would you like to enable X-Ray tracing on the function(s) in your application?  [y/N]: N

Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: N

Would you like to set Structured Logging in JSON format on your Lambda functions?  [y/N]: y
Structured Logging in JSON format might incur an additional cost. View https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-pricing for more details

Project name [sam-app]: sam-app

Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)

    -----------------------
    Generating application:
    -----------------------
    Name: sam-app
    Base Image: amazon/nodejs16.x-base
    Architectures: x86_64
    Dependency Manager: npm
    Output Directory: .
    Configuration file: sam-app\samconfig.toml

    Next steps can be found in the README file at sam-app\README.md


Commands you can use next
=========================
[*] Create pipeline: cd sam-app && sam pipeline init --bootstrap
[*] Validate SAM template: cd sam-app && sam validate
[*] Test Function in the Cloud: cd sam-app && sam sync --stack-name {stack-name} --watch

3. 이제 vscode로 위의 sam init을 수행한 경로로 이동하면 아래와 같은 폴더 및 파일들이 생성되어 있다.

- vscode로 열어보자.

아래와 같이 자동으로 여러개의 파일과 폴더를 생성한 것을 확인 할 수 있다.

4. 리소스 생성

리소스 생성을 보면 아래와 같다. 모든 리소스는 template.yaml에 Resources에 등록하여 생성 및 관리된다.

아래의 코드를 보면 어떤 리소스가 생성이 되는지 확인이 가능하다.

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.11
      Architectures:
      - x86_64
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

일단 위의 코드를 보면 HelloWorldFunction 부분은 람다 함수가 생성되는 부분이고, 그 밑에 Events 부분은 API Gateway가 생성되는 부분이다.

일단 람다 함수정보를 보면 아래와 같다. 람다 함수와 API Gateway의 관계가 표현 되어 있다.

- 람다 함수의 런타임 설정 정보는 아래와 같다. 계층 구성도 가능하다.

- API Gateway는 트리거로서 동작 한다.

트리거에 대한 세부 정보는 아래와 같다.

트리거란 말 그대로 방화쇠라는 이야기인데, 해당 람다 함수 호출에 대한 실행을 담당하는 것이 바로 API GW라고 볼 수 있다.

API GW는 엔드포인트를 가지고 있고, 해당 엔드포인트에 위의 /hello 라는 패스정보를 넣고, GET/POST/PUT/DELETE 즉 REST API를 생성 및 호출이 가능하다.

 

다음에는 파일썬을 가지고 빌드까지 해보자.

 

- 끝 -

 

'⭐ AWS > SAM (ServerlessApplicationModel)' 카테고리의 다른 글

SAM Build & Deploy with Python  (1) 2024.01.22