Jasper Inc.

地方在住Webエンジニアのテック&ライフブログ

AWS SAM CLIを使ってサンプルプロジェクトをローカルで実行する

ローカルPCでLambdaのサンプルプロジェクトを実行するだけ。

プロジェクト作成

$ sam init

    SAM CLI now collects telemetry to better understand customer needs.

    You can OPT OUT and disable telemetry collection by setting the
    environment variable SAM_CLI_TELEMETRY=0 in your shell.
    Thanks for your help!

    Learn More: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-telemetry.html

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 - GraphQLApi Hello World Example
    11 - Full Stack
    12 - Durable Functions
    13 - Lambda EFS example
    14 - Serverless Connector Hello World Example
    15 - Multi-step workflow with Connectors
    16 - Hello World Durable Function Example
    17 - DynamoDB Example
    18 - Machine Learning
Template: 1

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

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

(以下略…)

ビルドする

$ cd sam-app
$ sam build

Starting Build use cache
Cache is invalid, running build and copying resources for following functions (HelloWorldFunction)
Building codeuri: /Users/ryoheijin/Projects/sam-app/hello-world runtime: provided.al2023 architecture: x86_64 functions: HelloWorldFunction
Workflow GoModulesBuilder does not support value "False" for building in source. Using default value "True".
 Running GoModulesBuilder:Build

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided

ローカルで実行する

$ sam local invoke
Invoking bootstrap (provided.al2023)
Local image was not found.
Removing rapid images for repo public.ecr.aws/sam/emulation-provided.al2023
Building image.................................................................
Using local image: public.ecr.aws/lambda/provided:al2023-rapid-x86_64.

Mounting /Users/ryoheijin/Projects/sam-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container
SAM_CONTAINER_ID: 5889fd423be392bf9aee426b0685d894f678d6011b83458d0308081c6efaf644
START RequestId: 52d85a50-0780-45e0-bb21-e5d346f7ba28 Version: $LATEST
END RequestId: 76ef4216-0d66-47a3-9515-c58526ca6006
REPORT RequestId: 76ef4216-0d66-47a3-9515-c58526ca6006  Init Duration: 1.08 ms  Duration: 133.83 ms Billed Duration: 134 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode": 200, "headers": null, "multiValueHeaders": null, "body": "Hello, world!\n"}

以上。