Skip to content

Instantly share code, notes, and snippets.

@m-arrieta-r
Created November 29, 2022 01:57
Show Gist options
  • Save m-arrieta-r/a5bfe709438e61f603afbf7f91df08d0 to your computer and use it in GitHub Desktop.
Save m-arrieta-r/a5bfe709438e61f603afbf7f91df08d0 to your computer and use it in GitHub Desktop.
Remix.run - AWS API Gateway v2 - S3 Integration - Lambda
import * as cdk from 'aws-cdk-lib';
import type { Construct } from "constructs";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { RemovalPolicy } from 'aws-cdk-lib';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { Alias, Runtime } from 'aws-cdk-lib/aws-lambda';
import {
HttpApi, HttpMethod,
} from '@aws-cdk/aws-apigatewayv2-alpha';
import { HttpLambdaIntegration, HttpUrlIntegration } from '@aws-cdk/aws-apigatewayv2-integrations-alpha';
import { Bucket, ObjectOwnership } from 'aws-cdk-lib/aws-s3';
import { BucketDeployment, Source } from 'aws-cdk-lib/aws-s3-deployment';
export class RemixStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const httpApi = new HttpApi(this, 'HttpApi')
const assetsBucket = new Bucket(this, "AssetsBucket", {
autoDeleteObjects: true,
publicReadAccess: true,
removalPolicy: RemovalPolicy.DESTROY,
websiteIndexDocument: 'index.html',
objectOwnership: ObjectOwnership.BUCKET_OWNER_ENFORCED
});
new BucketDeployment(this, 'DeployWebsite', {
sources: [Source.asset('./public/build')],
destinationBucket: assetsBucket,
destinationKeyPrefix: 'build/'
});
const remixFn = new NodejsFunction(this, "RemixFn", {
runtime: Runtime.NODEJS_18_X,
currentVersionOptions: {
removalPolicy: RemovalPolicy.DESTROY,
},
handler: 'handler',
entry: "remix.handler.ts",
logRetention: RetentionDays.ONE_WEEK,
memorySize: 512,
timeout: cdk.Duration.seconds(10),
});
const version = remixFn.currentVersion;
const remixFnProdAlias = new Alias(this, 'RemixFnProdAlias', {
aliasName: 'Prod',
provisionedConcurrentExecutions: 1,
version,
})
const assetsIntegration = new HttpUrlIntegration('AssetsIntegration', `https://${assetsBucket.bucketDomainName}/{proxy}`, {
method: HttpMethod.GET,
});
const lambdaIntegration = new HttpLambdaIntegration('lambdaIntegration', remixFnProdAlias);
httpApi.addRoutes({
path: '/{proxy+}',
methods: [ HttpMethod.ANY ],
integration: lambdaIntegration,
});
httpApi.addRoutes({
path: '/_static/{proxy+}',
methods: [ HttpMethod.GET ],
integration: assetsIntegration,
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment