2

I’m learning remix and trying to set up a remix project that uses aws cdk to do the server. I have found a GitHub example here: https://github.com/ajhaining/remix-cloudfront-cdk-example But it doesn’t really explain how or what’s going on / how to do this from scratch. If anyone could help explain how to set this up it would be a great help!

Oliver Darby
  • 454
  • 1
  • 6
  • 15
  • I would start with one (remix or CDK) and then go from there. They both provided Hello World style tutorials. Once you have a better understanding of one (or both) separately it will be easier to understand how they interact. – Jacob Smit Feb 10 '23 at 03:55

1 Answers1

2

In the solution you referenced this person is using AWS CDK to deploy a front-end and back-end solution that uses the Remix framework.

In case you are unfamiliar with CDK: AWS CDK allows you to write code describing your AWS infrastructure for deployment to AWS i.e infrastructure as code.

They are using the following AWS infrastructure:

  • An AWS S3 bucket for storing static files (front end bundles including assets like css etched )
  • AWS Lambda using Cloudfront Lambda@Edge (the backend used to do server side rendering)
  • AWS Cloudfront as a CDN. This routes the traffic to the correct "origin" (for front end assets or server side rendering)

The whole "stack" is described in cdk-remix-app-stack.ts.

Here they describe where the source of the AWS Lambda function to do server side rendering:

const edgeFn = new NodejsFunction(this, "EdgeFn", {
      currentVersionOptions: {
        removalPolicy: RemovalPolicy.DESTROY,
      },
      entry: "server/index.ts",
      logRetention: RetentionDays.THREE_DAYS,
      memorySize: 1024,
      timeout: Duration.seconds(10),
    });

Here they describe where the source for the front-end assets is to be stored in s3:

new BucketDeployment(this, "AssetsDeployment", {
      destinationBucket: assetsBucket,
      distribution,
      prune: true,
      sources: [Source.asset("public")],
      cacheControl: [
        CacheControl.maxAge(Duration.days(365)),
        CacheControl.sMaxAge(Duration.days(365)),
      ],
    });

This bit is a bit more complicated, here they configure the CDN to point the distribution to the specific origins (s3 for front-end or lambda for back-end rendering)

const distribution = new Distribution(this, "Distribution", {
      defaultBehavior: {
        allowedMethods: AllowedMethods.ALLOW_ALL,
        cachePolicy: CachePolicy.CACHING_DISABLED,
        compress: true,
        edgeLambdas: [
          {
            eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
            functionVersion: edgeFn.currentVersion,
            includeBody: true,
          },
        ],
        origin: assetsBucketS3Origin,
        originRequestPolicy: new OriginRequestPolicy(
          this,
          "OriginRequestPolicy",
          {
            headerBehavior: OriginRequestHeaderBehavior.all(),
            queryStringBehavior: OriginRequestQueryStringBehavior.all(),
            cookieBehavior: OriginRequestCookieBehavior.all(),
          }
        ),
        viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
      },
      additionalBehaviors: {
        "build/*": {
          allowedMethods: AllowedMethods.ALLOW_GET_HEAD,
          cachePolicy: CachePolicy.CACHING_OPTIMIZED,
          compress: true,
          origin: assetsBucketS3Origin,
          viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
        },
      },
    });
shenku
  • 11,969
  • 12
  • 64
  • 118
  • If i was to start a project in remix and then run cdk init would this work or should i create the bin, lib and cdk.json file manually in my remix project? What would be the best way to start... – Oliver Darby Feb 20 '23 at 22:37
  • 1
    not exactly. cdk like remix has an "init" command. CDK can only be initialised in an empty directory. what I would do is initialise remix project in one directory and cdk in another empty directory. then copy the CDK files into the remix directory. you would have to merge the package.json but shouldn't be hard CDK doesn't have too much in it. you will need to run npm install again of course. – shenku Feb 21 '23 at 04:04