Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions .github/workflows/deploy-luda-editor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: deploy-luda-editor
on:
push:
branches:
- "**__le**"
- "master"
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ap-northeast-2
jobs:
deploy-luda-editor:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: set BRANCH_NAME, STACK_NAME
run: |
BRANCH_NAME=$(echo ${GITHUB_REF##*/} | tr "[:upper:]" "[:lower:]" | sed -e 's/\//-/g' -e 's/|/-/g')
echo $BRANCH_NAME
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
STACK_NAME=$(echo ${GITHUB_REF##*/} | tr "[:upper:]" "[:lower:]" | tr -cd '[:alnum:]')
echo $STACK_NAME
echo "STACK_NAME=$STACK_NAME" >> $GITHUB_ENV

- run: echo ${{ env.BRANCH_NAME }};

- uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: wasm-pack install
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

- name: rustup wasm32-unknown-unknown
run: rustup target add wasm32-unknown-unknown

- uses: actions/cache@v3
id: namui-cli-rust-cache
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
namui-cli/target/
key: ${{ runner.os }}-cargo-${{ hashFiles('namui-cli/Cargo.lock') }}

- name: Get npm cache directory
id: npm-cache-dir
run: echo "::set-output name=dir::$(npm config get cache)"

- uses: actions/cache@v3
id: npm-cache
with:
path: ${{ steps.npm-cache-dir.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

- name: install cargo-lambda
run: pip3 install cargo-lambda

- name: install namui-cli
run: bash namui-cli/scripts/install.sh

- name: build client
working-directory: luda-editor/client
run: namui build wasm-unknown-web

- name: build server as lambda
working-directory: luda-editor/server
run: cargo lambda build --release --target x86_64-unknown-linux-gnu --output-format zip

- name: npm install in cdk
working-directory: luda-editor/cdk
run: npm ci

- name: bootstrap cdk
working-directory: luda-editor/cdk
run: npx cdk bootstrap

# Github client id and secret is not for production. please put it in github secrets on production branch.
- name: deploy cdk
working-directory: luda-editor/cdk
run: |
CDK_STACK_NAME=${{ env.STACK_NAME }} npx cdk deploy CdkStack --require-approval never \
--parameters lambdaFunctionName=${{ env.BRANCH_NAME }} \
--parameters serveStaticS3Bucket=luda-editor \
--parameters serveStaticS3KeyPrefix=${{ env.BRANCH_NAME }}/www \
--parameters githubClientId=abd04a6aeba3e99f5b4b \
--parameters githubClientSecret=501a915ca627e24d2088cf01416fe836db470dba \
--parameters s3BucketName=luda-editor \
--parameters s3KeyPrefix=${{ env.BRANCH_NAME }}

- name: inject client setting
working-directory: luda-editor/client
run: |
echo '{ \
"endpoint": "$(aws cloudformation describe-stacks \
--stack-name ${{ env.STACK_NAME }}
--query "Stacks[0].Outputs[?OutputKey=='FunctionUrl'].OutputValue" \
--output text)" \
}' > target/namui/wasm_unknown_web/bundle/setting.json

- name: deploy client to s3
working-directory: luda-editor/client
run: aws s3 cp target/namui/wasm_unknown_web s3://luda-editor/${{ env.BRANCH_NAME }}/www --recursive
8 changes: 8 additions & 0 deletions luda-editor/cdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.js
!jest.config.js
*.d.ts
node_modules

# CDK asset staging directory
.cdk.staging
cdk.out
6 changes: 6 additions & 0 deletions luda-editor/cdk/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.ts
!*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out
14 changes: 14 additions & 0 deletions luda-editor/cdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Welcome to your CDK TypeScript project

This is a blank project for CDK development with TypeScript.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

## Useful commands

* `npm run build` compile typescript to js
* `npm run watch` watch for changes and compile
* `npm run test` perform the jest unit tests
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk synth` emits the synthesized CloudFormation template
19 changes: 19 additions & 0 deletions luda-editor/cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env node
import "source-map-support/register";
import * as cdk from "aws-cdk-lib";
import { CdkStack } from "../lib/cdk-stack";

const app = new cdk.App();
new CdkStack(app, "CdkStack", {
stackName: process.env.CDK_STACK_NAME,
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */
/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
42 changes: 42 additions & 0 deletions luda-editor/cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"app": "npx ts-node --prefer-ts-exts bin/cdk.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
},
"context": {
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
"@aws-cdk/core:stackRelativeExports": true,
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
"@aws-cdk/core:checkSecretUsage": true,
"@aws-cdk/aws-iam:minimizePolicies": true,
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
"@aws-cdk/core:enablePartitionLiterals": true,
"@aws-cdk/core:target-partitions": [
"aws",
"aws-cn"
]
}
}
8 changes: 8 additions & 0 deletions luda-editor/cdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
105 changes: 105 additions & 0 deletions luda-editor/cdk/lib/cdk-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import * as path from "path";

export class CdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const lambdaFunctionName = new cdk.CfnParameter(
this,
"lambdaFunctionName",
{
type: "String",
description: "The name of the Lambda function",
},
);
const githubClientId = new cdk.CfnParameter(this, "githubClientId", {
type: "String",
description: "Github Client Id",
});
const githubClientSecret = new cdk.CfnParameter(
this,
"githubClientSecret",
{
type: "String",
description: "Github Client Secret",
},
);
const serveStaticS3Bucket = new cdk.CfnParameter(
this,
"serveStaticS3Bucket",
{
type: "String",
description: "The S3 Bucket of the static file to serve",
},
);
const serveStaticS3KeyPrefix = new cdk.CfnParameter(
this,
"serveStaticS3KeyPrefix",
{
type: "String",
description: "The S3 Key Prefix of the static file to serve",
},
);
const s3BucketName = new cdk.CfnParameter(this, "s3BucketName", {
type: "String",
description: "The name of the S3 bucket",
});
const s3KeyPrefix = new cdk.CfnParameter(this, "s3KeyPrefix", {
type: "String",
description: "The prefix of the S3 key",
});

const dynamoDb = new cdk.aws_dynamodb.Table(this, "DynamoDb", {
tableName: props?.stackName,
billingMode: cdk.aws_dynamodb.BillingMode.PAY_PER_REQUEST,
partitionKey: {
name: "__partition_key__",
type: cdk.aws_dynamodb.AttributeType.STRING,
},
sortKey: {
name: "__sort_key__",
type: cdk.aws_dynamodb.AttributeType.STRING,
},
});

const lambda = new cdk.aws_lambda.Function(this, "Server", {
runtime: cdk.aws_lambda.Runtime.PROVIDED_AL2,
handler: "bootstrap",
code: cdk.aws_lambda.Code.fromAsset(
path.join(
__dirname,
"../../server/target/lambda/server/bootstrap.zip",
),
),
functionName: lambdaFunctionName.valueAsString,
environment: {
SERVE_STATIC_S3_BUCKET: serveStaticS3Bucket.valueAsString,
SERVE_STATIC_S3_KEY_PREFIX:
serveStaticS3KeyPrefix.valueAsString,
GITHUB_CLIENT_ID: githubClientId.valueAsString,
GITHUB_CLIENT_SECRET: githubClientSecret.valueAsString,
DYNAMODB_TABLE_NAME: dynamoDb.tableName,
S3_BUCKET_NAME: s3BucketName.valueAsString,
S3_KEY_PREFIX: s3KeyPrefix.valueAsString,
},
initialPolicy: [
new cdk.aws_iam.PolicyStatement({
resources: [
`arn:aws:s3:::${s3BucketName.valueAsString}/${s3KeyPrefix.valueAsString}*`,
],
actions: ["s3:GetObject"],
}),
],
});

const fnUrl = lambda.addFunctionUrl({
authType: cdk.aws_lambda.FunctionUrlAuthType.NONE,
});

new cdk.CfnOutput(this, "FunctionUrl", {
value: fnUrl.url,
});
}
}
Loading