Skip to content

Azure Functions

Azure Functions는 Microsoft Azure의 서버리스 플랫폼이다. 이벤트에 반응하여 코드를 실행할 수 있으며, 기본적인 컴퓨팅 리소스를 자동으로 관리한다.

Hono는 처음부터 Azure Functions를 위해 설계되지 않았다. 하지만 Azure Functions Adapter를 사용하면 Azure Functions에서도 실행할 수 있다.

이 어댑터는 Node.js 18 이상에서 실행되는 Azure Functions V4와 호환된다.

1. CLI 설치

Azure Function을 생성하려면 먼저 Azure Functions Core Tools를 설치해야 한다.

macOS에서 설치하는 방법:

sh
brew tap azure/functions
brew install azure-functions-core-tools@4

다른 운영체제에서 설치하는 방법은 다음 링크를 참고한다:

2. 설정

현재 폴더에 TypeScript Node.js V4 프로젝트를 생성한다.

sh
func init --typescript

호스트의 기본 라우트 프리픽스를 변경한다. host.json의 루트 json 객체에 다음 속성을 추가한다:

json
"extensions": {
    "http": {
        "routePrefix": ""
    }
}

INFO

Azure Functions의 기본 라우트 프리픽스는 /api이다. 위와 같이 변경하지 않으면 모든 Hono 라우트를 /api로 시작해야 한다.

이제 Hono와 Azure Functions 어댑터를 설치할 준비가 되었다:

sh
npm i @marplex/hono-azurefunc-adapter hono
sh
yarn add @marplex/hono-azurefunc-adapter hono
sh
pnpm add @marplex/hono-azurefunc-adapter hono
sh
bun add @marplex/hono-azurefunc-adapter hono

3. Hello World

src/app.ts 파일을 생성한다:

ts
// src/app.ts
import { Hono } from 'hono'
const app = new Hono()

app.get('/', (c) => c.text('Hello Azure Functions!'))

export default app

src/functions/httpTrigger.ts 파일을 생성한다:

ts
// src/functions/httpTrigger.ts
import { app } from '@azure/functions'
import { azureHonoHandler } from '@marplex/hono-azurefunc-adapter'
import honoApp from '../app'

app.http('httpTrigger', {
  methods: [
    // 지원할 모든 HTTP 메서드를 여기에 추가한다
    'GET',
    'POST',
    'DELETE',
    'PUT',
  ],
  authLevel: 'anonymous',
  route: '{*proxy}',
  handler: azureHonoHandler(honoApp.fetch),
})

4. 실행

로컬에서 개발 서버를 실행한다. 웹 브라우저에서 http://localhost:7071에 접속한다.

sh
npm run start
sh
yarn start
sh
pnpm start
sh
bun run start

5. 배포

INFO

Azure에 배포하기 전에 클라우드 인프라에 몇 가지 리소스를 생성해야 한다. Azure 함수를 위한 지원 리소스 생성에 대한 Microsoft 문서를 참고한다.

배포를 위해 프로젝트를 빌드한다:

sh
npm run build
sh
yarn build
sh
pnpm build
sh
bun run build

Azure Cloud의 함수 앱에 프로젝트를 배포한다. <YourFunctionAppName>을 앱 이름으로 대체한다.

sh
func azure functionapp publish <YourFunctionAppName>

Released under the MIT License.