Code Monkey home page Code Monkey logo

clubclouddemo's Introduction

Pre-requisites

  • cdk installed (version > 2.0.0 (build 4b6ce31))
  • rust / cargo installed

Part 1: cdk + dummy lambda

  1. setup cdk project

mkdir backend cd backend cdk init app --language typescript

  1. get (temporary) AWS access keys into your terminal environment

  2. bootstrap the cdk project

cdk bootstrap

This will create S3 Bucket + ECR + Roles/Policies + Version SSM Param

  1. try a deploy

cdk deploy

This will create an empty CF deployment BackendStack

  1. change /backend/lib/backed-stack.ts
  • remove sqs comments
  • add a lambda (initially a nodejs inline)
import * as lambda from 'aws-cdk-lib/aws-lambda';
    const server = new lambda.Function(this, "GraphQL-Server", {
      functionName: 'GraphQL-Server',
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: "index.handler",
      code: lambda.Code.fromInline(`
        exports.handler = async function (event, context) {
          console.log(event);
        };
      `),
    });
  • deploy and test (AWS console)

Part 2: rust + change lambda

  1. setup directory

mkdir binaries cd binaries mkdir graphql-server cd graphql-server

cargo init

test if it works using cargo run

  1. make building for Graviton possible

rustup target add aarch64-unknown-linux-gnu

create build.sh

#!/bin/bash

LAMBDA_ARCH="linux/arm64" # set this to either linux/arm64 for ARM functions, or linux/amd64 for x86 functions.
RUST_TARGET="aarch64-unknown-linux-gnu" # corresponding with the above, set this to aarch64 or x86_64 -unknown-linux-gnu for ARM or x86 functions.
RUST_VERSION="latest" # Set this to a specific version of rust you want to compile for, or to latest if you want the latest stable version.
docker run \
  --platform ${LAMBDA_ARCH} \
  --rm --user "$(id -u)":"$(id -g)" \
  -v "${PWD}":/usr/src/myapp -w /usr/src/myapp rust:${RUST_VERSION} \
  cargo build --release --target ${RUST_TARGET} # This line can be any cargo command

(cd target/aarch64-unknown-linux-gnu/release && mkdir -p lambda && cp graphql-server lambda/bootstrap)
chmod +x build.sh

test it out by running ./build.sh

  1. make a lambda out of it

Add runtime dependencies

[dependencies]
lambda_runtime = "0.4.1"
tokio = "1.14.0"
log = "0.4.14"
simple_logger = "1.15.0"
serde_json = "1.0.72"

Change main.rs

use lambda_runtime::{handler_fn, Error};
use serde_json::{Value};
use simple_logger::SimpleLogger;

#[tokio::main]
async fn main() -> Result<(), Error> {
    SimpleLogger::new().with_level(log::LevelFilter::Info).init().unwrap();

    let func = handler_fn(my_handler);
    lambda_runtime::run(func).await?;
    Ok(())
}

pub(crate) async fn my_handler(event: Value, _ctx: lambda_runtime::Context) -> Result<Value, Error> {
    Ok(event)
}

Run ./build

  1. update cdk project to use the created binary
    const server = new lambda.Function(this, "GraphQL-Server", {
      functionName: 'GraphQL-Server',
      runtime: lambda.Runtime.PROVIDED_AL2,
      code: lambda.Code.fromAsset("../binaries/graphql-server/target/aarch64-unknown-linux-gnu/release/lambda"),
      handler: 'not.required',
      architecture: lambda.Architecture.ARM_64
    });

cdk deploy

and test if the lambda still echos (and source no longer visible in console)

clubclouddemo's People

Contributors

eelviny avatar jacco avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.