💡

簡単にHonoをSAMでLambdaにデプロイする方法

に公開

HonoをSAMでデプロイする方法の手順です。

1. samプロジェクトのセットアップ

適当なディレクトリでsam initを実施する。

Which template source would you like to use?

1を選択する。

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を選択する。

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 - Lambda EFS example
	13 - Serverless Connector Hello World Example
	14 - Multi-step workflow with Connectors
	15 - DynamoDB Example
	16 - Machine Learning
Template: 1

Nを選択する。

Which runtime would you like to use?

11を選択する。

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

What package type would you like to use?

1を選択する。

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

Select your starter template

2を選択する。

Select your starter template
	1 - Hello World Example
	2 - Hello World Example TypeScript
Template: 2

その他オプション

Nを選択する。

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]: N

Project name [sam-app]:

任意の名前を入力する。

Project name [sam-app]: hono-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 - Lambda EFS example
	13 - Serverless Connector Hello World Example
	14 - Multi-step workflow with Connectors
	15 - DynamoDB Example
	16 - Machine Learning
Template: 1

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

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

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

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

Select your starter template
	1 - Hello World Example
	2 - Hello World Example TypeScript
Template: 2

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]: N

Project name [sam-app]: hono-lambda
                                                                                                                                            
Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)                                                   

    -----------------------
    Generating application:
    -----------------------
    Name: hono-lambda
    Runtime: nodejs22.x
    Architectures: x86_64
    Dependency Manager: npm
    Application Template: hello-world-typescript
    Output Directory: .
    Configuration file: hono-lambda/samconfig.toml
    
    Next steps can be found in the README file at hono-lambda/README.md
        

2.Honoプロジェクト作成

デプロイするHonoプロジェクトはhello-worldのテンプレートを利用します。

hello-worldフォルダを消す

まずは、最初からあるnodejsのフォルダは消します。

Honoプロジェクトの作成

npm create hono@latest [任意のPJ名]を実行する。

npm create hono@latest hello-hono

https://hono.dev/docs/getting-started/basic

Which template do you want to use?

aws-lambdaを選択する。

 Which template do you want to use? aws-lambda

その他オプション

任意で選択する。

 Do you want to install project dependencies? Yes
 Which package manager do you want to use? npm
コマンド詳細
 npm create hono@latest hello-hono
> npx
> "create-hono" hello-hono

create-hono version 0.19.2
 Using target directory hello-hono
 Which template do you want to use? aws-lambda
 Do you want to install project dependencies? Yes
 Which package manager do you want to use? npm
 Cloning the template
 Installing project dependencies
🎉 Copied project files
Get started with: cd hello-hono

作成後の状態

3.template.yamlの修正

不必要な部分は削り、プロジェクト構成に合わせて修正します。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  hono-lambda

  Sample SAM Template for hono-lambda
  
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

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/
+      CodeUri: hello-hono/
-      Handler: app.lambdaHandler
+      Handler: src/index.handler
      Runtime: nodejs22.x
      Architectures:
        - x86_64
       # APIGWは使わないので消す
-      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
    Metadata: # Manage esbuild properties
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: "es2020"
        Sourcemap: true
        EntryPoints: 
-        - app.ts
+        - src/index.ts

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
-  HelloWorldApi:
-    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
-    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

4.package.jsonの修正

作成したHonoプロジェクトのpackage.jsonを調整する。
以下を実行する。

  • npm uninstall esbuild
  • npm install esbuild

以下のようになっていればOK。

  "devDependencies": {
    "npm-run-all2": "^6.2.0"
  },
  "dependencies": {
    "esbuild": "^0.25.9",
    "hono": "^4.9.6"
  }

5.ビルド

sam buildを実行してエラーが出ていないことを確認する。
Build Succeededが出ればOK!

6.デプロイ

sam deploy --guided --profile [profile-name]を実行する。
デプロイする環境に合わせて内容を選択する。
Deploy this changeset? でyを選択する。
Succeessfully が出ていればOK!
これでSAMでデプロイができているはずです。

Configuring SAM deploy
======================

        Looking for config file [samconfig.toml] :  Found
        Reading default arguments  :  Success

        Setting default arguments for 'sam deploy'
        =========================================
        Stack Name [hono-lambda]: hono-lambda-stack
        AWS Region [us-east-1]: ap-northeast-1
        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
        Confirm changes before deploy [Y/n]: Y
        #SAM needs permission to be able to create roles to connect to the resources in your template
        Allow SAM CLI IAM role creation [Y/n]:  Y
        #Preserves the state of previously provisioned resources when an operation fails
        Disable rollback [y/N]: y
        Save arguments to configuration file [Y/n]:  Y
        SAM configuration file [samconfig.toml]: 
        SAM configuration environment [default]: 

        Looking for resources needed for deployment:

Discussion