Zum Inhalt springen

Lambda@Edge ​

Lambda@Edge ist eine Serverless-Plattform von Amazon Web Services. Sie ermöglicht dir, Lambda-Funktionen an den Edge-Standorten von Amazon CloudFront auszuführen und damit das Verhalten für HTTP-Anfragen und -Antworten anzupassen.

Hono unterstützt Lambda@Edge in einer Node.js-18+-Umgebung.

1. Einrichtung ​

Bei der Erstellung einer Anwendung auf Lambda@Edge ist CDK nützlich, um Funktionen wie CloudFront, IAM-Rollen, API Gateway und weitere Komponenten einzurichten.

Initialisiere dein Projekt mit der CLI cdk.

sh
mkdir my-app
cd my-app
cdk init app -l typescript
npm i hono @hono/lambda-edge
mkdir lambda
sh
mkdir my-app
cd my-app
cdk init app -l typescript
yarn add hono @hono/lambda-edge
mkdir lambda
sh
mkdir my-app
cd my-app
cdk init app -l typescript
pnpm add hono @hono/lambda-edge
mkdir lambda
sh
mkdir my-app
cd my-app
cdk init app -l typescript
bun add hono @hono/lambda-edge
mkdir lambda

2. Hello World ​

Bearbeite lambda/index_edge.ts.

ts
import { Hono } from 'hono'
import { handle } from '@hono/lambda-edge'

const app = new Hono()

app.get('/', (c) => c.text('Hello Hono on Lambda@Edge!'))

export const handler = handle(app)

3. Bereitstellung ​

Bearbeite bin/my-app.ts.

ts
#!/usr/bin/env node
import 'source-map-support/register'
import * as cdk from 'aws-cdk-lib'
import { MyAppStack } from '../lib/my-app-stack'

const app = new cdk.App()
new MyAppStack(app, 'MyAppStack', {
  env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: 'us-east-1',
  },
})

Bearbeite lambda/cdk-stack.ts.

ts
import { Construct } from 'constructs'
import * as cdk from 'aws-cdk-lib'
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'
import * as origins from 'aws-cdk-lib/aws-cloudfront-origins'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'
import * as s3 from 'aws-cdk-lib/aws-s3'

export class MyAppStack extends cdk.Stack {
  public readonly edgeFn: lambda.Function

  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props)
    const edgeFn = new NodejsFunction(this, 'edgeViewer', {
      entry: 'lambda/index_edge.ts',
      handler: 'handler',
      runtime: lambda.Runtime.NODEJS_20_X,
    })

    // Upload any html
    const originBucket = new s3.Bucket(this, 'originBucket')

    new cloudfront.Distribution(this, 'Cdn', {
      defaultBehavior: {
        origin: new origins.S3Origin(originBucket),
        edgeLambdas: [
          {
            functionVersion: edgeFn.currentVersion,
            eventType: cloudfront.LambdaEdgeEventType.VIEWER_REQUEST,
          },
        ],
      },
    })
  }
}

Führe abschließend den folgenden Befehl zur Bereitstellung aus:

sh
cdk deploy

Callback ​

Wenn du Basic-Authentifizierung hinzufügen und nach der Überprüfung die Anfrageverarbeitung fortsetzen möchtest, kannst du c.env.callback() verwenden.

ts
import { Hono } from 'hono'
import { basicAuth } from 'hono/basic-auth'
import type { Callback, CloudFrontRequest } from '@hono/lambda-edge'
import { handle } from '@hono/lambda-edge'

type Bindings = {
  callback: Callback
  request: CloudFrontRequest
}

const app = new Hono<{ Bindings: Bindings }>()

app.get(
  '*',
  basicAuth({
    username: 'hono',
    password: 'acoolproject',
  })
)

app.get('/', async (c, next) => {
  await next()
  c.env.callback(null, c.env.request)
})

export const handler = handle(app)

Veröffentlicht unter der MIT-Lizenz.