Skip to content

Instantly share code, notes, and snippets.

@masawada
Created October 7, 2022 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masawada/d5438241ab27e82194bb30a95b48012b to your computer and use it in GitHub Desktop.
Save masawada/d5438241ab27e82194bb30a95b48012b to your computer and use it in GitHub Desktop.
import { Stack, StackProps, RemovalPolicy } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as origins from 'aws-cdk-lib/aws-cloudfront-origins';
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
export interface DotfilesRedirectorStackProps extends StackProps {
readonly certificateArn: string;
}
export class DotfilesRedirectorStack extends Stack {
constructor(scope: Construct, id: string, props: DotfilesRedirectorStackProps) {
super(scope, id, props);
const { certificateArn } = props;
const bucket = new s3.Bucket(this, 'dummy-origin-bucket', {
removalPolicy: RemovalPolicy.DESTROY,
});
const cfFunction = new cloudfront.Function(this, 'redirector-function', {
code: cloudfront.FunctionCode.fromFile({ filePath: 'src/cf-functions/redirect.js' }),
});
const distribution = new cloudfront.Distribution(this, 'redirector-distribution', {
certificate: acm.Certificate.fromCertificateArn(this, 'redirector-certificate', certificateArn),
domainNames: [ 'dot.masawada.me' ],
defaultBehavior: {
origin: new origins.S3Origin(bucket),
functionAssociations: [{
function: cfFunction,
eventType: cloudfront.FunctionEventType.VIEWER_REQUEST,
}],
},
});
}
}
function handler(event) {
var request = event.request;
var headers = request.headers;
var host = request.headers.host.value;
var uri = request.uri;
if (host !== 'dot.masawada.me') {
return {
statusCode: 400,
statusDescription: 'Bad Request',
};
}
var newUrl = `https://raw.githubusercontent.com/masawada/dotfiles/main${uri}`;
return {
statusCode: 302,
statusDescription: 'Found',
headers: { location: { value: newUrl } },
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment