Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6589

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T07:55:08+00:00 2024-11-27T07:55:08+00:00

Secure Pattern for Deploying WASM on S3

  • 60k

Picking up where I left off from the last article, I'd built a simple WASM project with Rust and walked through how to generate a publishable distribution. In this edition, which is probably the penultimate in the series, I need to get a path towards CloudFront and S3. I want to stay true to the Serverless objective and those two services are perfect for shipping web-delivered code. So let's dive into Deploying WASM on S3.

Series Articles

This is as I mentioned the second article in a series about Serverless WASM with Rust. If you missed the first, below is the link to jump in and read that first. Don't worry, this will still be here.

  1. Getting started with Serverless Web Assembly (WASM) with Rust

Let's take a look at the architecture I will be building for the rest of this piece.

Architecture

The main stars for deploying WASM on S3 are CloudFront and of course S3. Those two services will do the heavy lifting with our compiled WASM distribution.

Deploying WASM on S3

What's cool about using WASM is that it's just some HTML, JavaScript, and an executable WASM file. That means that it's just like running normal HTML, CSS, and JavaScript which makes S3 the perfect storage vehicle for this code. And using CloudFront with it is a match made in heaven

Deploying WASM on S3

Output of Trunk

Going back to building the WASM package, I used a tool called Trunk to build and bundle the Rust code. When I run the command trunk build I'm presented with the following images. The first is what the build looks like from the console and the second is the contents of the dist directory that is created and populated.

Trunk Build

Trunk Dist

With a dist directory ready, I need to figure out a way to get that up into S3. Let's explore how to make that happen.

S3 for Static Website

My default these days is to use CDK to build infrastructure and that's what I'm going to use here. Specifically, CDK with TypeScript.

To start deploying WASM on S3, I need to set up a bucket that is geared towards being a static website. What this does for me is restrict access and set some other sensible and secure defaults.

The code to accomplish that looks like this:

const bucket = new Bucket(this, 'Bucket', {     accessControl: BucketAccessControl.PRIVATE, });  new BucketDeployment(this, 'BucketDeployment', {     destinationBucket: bucket,     sources: [Source.asset('./dist')] }) 
Enter fullscreen mode Exit fullscreen mode

What's going on above is that I'm creating a new bucket by “newing” a Bucket construct. And then from that bucket, I'm creating another construct called BucketDeployment and sending two things in.

  1. The bucket I just created.
  2. The directory that holds the output of my trunk build command.

With the S3 deployment part created in my deploying WASM on S3, it's now time to move to CloudFront.

Establishing the CloudFront Distribution

There's no magic in any of this. Sure CDK makes it easy to build and package infrastructure but sometimes, things just are right in front of me.

Creating a CloudFront distribution in front of my S3 bucket gives me the ability to ship my ./dist output to all of the edge locations that AWS provides and when a user requests access, it'll grab from that edge cache first before reaching out to the S3 origin. Using this technique when deploying WASM on S3 works just like any other static website.

const originAccessIdentity = new OriginAccessIdentity(this, 'OriginAccessIdentity'); bucket.grantRead(originAccessIdentity);  new Distribution(this, 'Distribution', {     defaultRootObject: 'index.html',     defaultBehavior: {     origin: new S3Origin(bucket, { originAccessIdentity }),     }, }) 
Enter fullscreen mode Exit fullscreen mode

Here's what is happening in this code:

  1. Create an origin identity.
  2. Give the newly created bucket read access to the identity.
  3. Create a new distribution and assign index.html as the default root object.

Putting it Together

Running cdk deploy in the working directory will push the code and complete the last step in deploying WASM to S3.

All put together:

trunk build cdk deploy 
Enter fullscreen mode Exit fullscreen mode

The S3 bucket will then show the HTML, JS, and WASM files.

S3 Files

If I then browse to Cloudfront, I can pick up the URL for the distribution so that I can see if the WASM renders in the browser.

CloudFront

Final Check

Now that we are coming to the end of this article on deploying WASM on S3, we can take a look at the browser to see where we are.

PGA Players.

It's nothing fancy but it's a start for where I'm going to go next with it.

Wrapping Up

Two articles into this now-planned 3 article series I've shown you how to build a simple WASM application with Rust and then demonstrated a solution for deploying WASM on S3. Moving into the finale, I'll put together the following finishing touches.

  1. More styled UI
  2. API build in Rust
  3. Connect the WASM to the Rust API.

Once these pieces are in place, I'll have a Serverless WASM implementation with Rust.

I'm still not 100% sure about the use cases here, but I believe by exploring the topics above and building out more useful functionality, I'll be able to assess whether this is something worth exploring more. WASM isn't just for the web, it can also run on Lambda and other compute options which might be worth checking out as well.

And as always, here is the source code that I'm working from on GitHub.

Thanks for reading and happy building!

awsrustserverlesswebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.