Skip to content

Instantly share code, notes, and snippets.

@ubinix-warun
Created December 19, 2023 04:03
Show Gist options
  • Save ubinix-warun/037c253c2a1bbc449b7b9526767ed635 to your computer and use it in GitHub Desktop.
Save ubinix-warun/037c253c2a1bbc449b7b9526767ed635 to your computer and use it in GitHub Desktop.
import { Hono } from 'hono'
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('/timezone', async (c, next) => {
await next()
const request = c.env.request;
const headers = request.headers;
let response = {
status: '400',
statusDescription: 'Bad Request',
body: `${request.method} ${request.uri}`
};
console.log(`/timezone EVENT: \n` + JSON.stringify(c.env.request, null, 2))
if (headers['cloudfront-viewer-time-zone']) {
const timeZone = headers['cloudfront-viewer-time-zone'][0].value;
response = {
status: '200',
statusDescription: 'OK',
body: timeZone
};
} else {
response = {
status: '500',
statusDescription: 'Internal Server Error',
body: "'cloudfront-viewer-time-zone' is not exists"
};
}
c.env.callback(null, response)
})
export const handler = handle(app)
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 acm from 'aws-cdk-lib/aws-certificatemanager';
import * as s3 from 'aws-cdk-lib/aws-s3';
export class LambdaEdgeStack 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/hono_edge.ts',
handler: 'handler',
runtime: lambda.Runtime.NODEJS_18_X,
});
const certificate = acm.Certificate.fromCertificateArn(this, '*.xyz.io',
'<>');
// Upload any html
const originBucket = new s3.Bucket(this, 'originBucket');
const myOriginRequestPolicy = new cloudfront.OriginRequestPolicy(this, 'CloudFrontViewerPolicy', {
comment: 'A CloudFront Viewer policy',
headerBehavior: cloudfront.OriginRequestHeaderBehavior.allowList(
'cloudfront-viewer-address',
'cloudfront-viewer-country',
'cloudfront-viewer-country-region-name',
'cloudfront-viewer-city',
'cloudfront-viewer-latitude',
'cloudfront-viewer-longitude',
'cloudfront-viewer-postal-code',
'cloudfront-viewer-time-zone',
),
});
new cloudfront.Distribution(this, 'Cdn', {
certificate: certificate,
domainNames: [ "edge.xyz.io" ],
defaultBehavior: {
origin: new origins.S3Origin(originBucket),
cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED,
originRequestPolicy: myOriginRequestPolicy,
// allowedMethods: [cloudfront.CloudFrontAllowedMethods.ALL],
// viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
edgeLambdas: [
{
functionVersion: edgeFn.currentVersion,
eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
},
],
},
});
}
}
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { LambdaEdgeStack } from '../lib/lambda-edge-stack';
const app = new cdk.App();
new LambdaEdgeStack(app, 'LambdaEdgeStack', {
env: {
account: '<AWS_ACCOUNT>',
region: '<AWS_REGION>'
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment