Astro 2.0 on AWS Amplify - node.js

I'm trying to use the SSR with AWS Amplify but when I activate the Node.js and change the output type to server. When I deploy to server I got an 404 error page.
I tried to build the project and I have to run two npm commands: npm run build and after that the npm run server. But the deploy is not working.
frontend:
phases:
preBuild:
commands:
- npm ci
build:
commands:
- npm run build
postBuild:
commands:
- npm run server
artifacts:
baseDirectory: /dist
files:
- '**/*'
cache:
paths:
- node_modules/**/*

Astro has, adapters for each SSR cloud solution, and I haven't seen AWS listed
you could use vercel or cloudflare
and install the adapter of those server
npx astro add cloudflare
on your option I think amplify is needing for nodejs adapter and its already exist
use this instead
npx astro add node

Related

Hoe to run the node js code in aws amplify

I have a React/Node app which i am trying to host on AWS amplify. first try, my app deployed but i saw some pages/buttons are not working because of node js. Then i did some search and i saw that i need to modify "amplify.yml" file to:
version: 1
backend:
phases:
build:
commands:
- '# Execute Amplify CLI with the helper script'
- amplifyPush --simple
artifacts:
baseDirectory: build
files:
- '**/*'
frontend:
phases:
preBuild:
commands:
- yarn install
build:
commands:
- yarn build
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
Getting build issues(Build time out) with the above build settings.
Make sure you have created a user with AdministratorAccess-Amplify privileges in IAM.
Then it is necessary to replace line 6 of the Hands-On amplify.yml with
npm install -g #aws-amplify/cli.
The code should now display correctly to complete Hands-On

Deploy NodeJS Express on amplify

I am trying to deploy an app on AWS Amplify.
The app is React front and and NodeJS Express backend.
The frontend works fine, but the backend is just stuck without any reasonable explanation
My YML file is
version: 1
backend:
phases:
build:
commands:
- npm run build-backend
postBuild:
commands:
- cd ..
frontend:
phases:
preBuild:
commands:
- npm ci
build:
commands:
- npm run build-frontend
artifacts:
baseDirectory: ./client/build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
build backend-script :
"build-backend": "cd server && npm run start",
npm run start script:
"start": "npm install && node index.js"
The build is getting stuck on the npm install and after 10-20 minutes just "gives up" without the following log
2021-04-22T11:49:20.693Z [INFO]: > server#1.0.0 start /codebuild/output/src650104622/src/myBlog/server
> npm install && node index.js
2021-04-22T11:49:26.976Z [INFO]: > bcrypt#5.0.0 install /codebuild/output/src650104622/src/myBlog/server/node_modules/bcrypt
> node-pre-gyp install --fallback-to-build
Thanks
I came across this thread while looking for a solution for my project. And because there are no more answers here, I'll tell you what I could find myself.
Amplify works fine with SSG web applications (Gatsby, etc.), with SSR (React, NextJS, NuxtJS, etc.) and with simple NodeJS and ExpressJS applications (which only run on requests because Amplify uses Lambda-functions to handle it). So:
If you need a simple ExpressJS application for your API you can
use the following serverless Ampl solution:
https://docs.amplify.aws/guides/api-rest/express-server/q/platform/js/
If you need a socket.io application (or another constantly
running application) you need to use AWS Fargate (uses
docker images) or AWS EC2 (works on simple virtual machine
with access by SSH) solutions.
P.S. If you have any other information on this subject, please post it here.

Problem to run nodejs app on Gitlab Pages

I'm trying to run my nodejs app on gitlab pages. I use a gitlab-ci.yml file for this where I deploy and run the nodejs app. Unfortunately the pipeline kills the process after 1 hour because the pipeline thinks running the nodejs app is part of the build script. I have two questions:
- Can you run a nodejs app on gitlab pages?
- If so, what is the best way to start the app?
Below you find the gitlab-ci.yml file.
Thanks!
image: node:latest
stages:
- build
cache:
paths:
- node_modules/
install_dependencies:
stage: build
script:
- npm install
- npm install -g nodemon
- NODE_ENV=production nodemon app.js
artifacts:
paths:
- node_modules/
Can you run a nodejs app on gitlab pages?
No! Gitlab pages allow you to host only static websites: https://about.gitlab.com/product/pages/
If so, what is the best way to start the app?
If your app is static, try to use a static site generator! If you wanna play with nodejs, try other hosting platforms like heroku or clever cloud

.ebextensions with CodePipeline and Elastic Beanstalk

I started working on my first CodePipeline with node.js app which is hosted on github. I would like to create simple pipe as follow:
Github repo triggers pipe
Test env (Elastic Beanstalk app) is built from S3 .zip file
Test env runs npm test and npm lint
If everything is OK then QA env (another EB app) is built
For above pipe I've created .config files under .ebextensions directory:
I would like to use npm install --production for QA and PROD env, but it seems that EC2 can't find node nor npm. I checked logs and EC2 triggered npm install by default in temporary folder, then it fails on my first script and app catalogue is always empty.
container_commands:
install-dev:
command: "npm install"
test: "[ \"$NODE_ENV\" = \"TEST\" ]"
ignoreErrors: false
install-prod:
command: "npm install --production"
test: "[ \"$NODE_ENV\" != \"TEST\" ]"
ignoreErrors: false
Is it posible to run unit tests and linting without jenkins?
container_commands:
lint:
command: "npm run lint"
test: "[ \"$NODE_ENV\" = \"TEST\" ]"
ignoreErrors: false
test:
command: "npm run test"
test: "[ \"$NODE_ENV\" = \"TEST\" ]"
ignoreErrors: false
I set NODE_ENV for each Elastic Beanstalk instance. No matter what I will do every time my pipe fails because of npm is not recognized, but how is it possible if I'm running 64bit Amazon Linux with node.js ? What's more I cannot find any examples about CodePipeline with node.js in AWS Docs. Thanks in advance!
If you're using AWS for CI/CD, you can use CodeBuild. However, Github provides a great feature called Actions for running Unit Tests, which I find much simpler than AWS. Anyway, I will walk you through both examples:
Using AWS for running Unit Tests
Essentially, you could create a new stage into your CodePipeline, and configure the CodeBuild for running Unit Tests, e.g.
First, add a buildspec.yml file in the root folder of your app so you can use the following example:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 10
commands:
- echo Installing Mocha globally...
- npm install -g mocha
pre_build:
commands:
- echo Installing dependencies...
- npm install
- npm install unit.js
build:
commands:
- echo Build started on `date`
- echo Run Unit Tests and so on
- npm run test
- npm run lint
post_build:
commands:
- echo Build completed on `date`
# THIS IS OPTIONAL
artifacts:
files:
- app.js
- package.json
- src/app/*
- node_modules/**/*
You can find everything you need in the BackSpace Academy, this course is for free:
AWS DevOps CI/CD - CodePipeline, Elastic Beanstalk and Mocha
Using Github for running Unit Tests
You could create your custom actions using Github, it will automatically set up everything you need in your root folder, e.g.
After choosing the appropriate workflow, it will automatically generate a folder/file ".github > workflow > nodejs.yml".
So it will look like this:
name: Node CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [8.x, 10.x, 12.x]
steps:
- uses: actions/checkout#v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm test
env:
CI: true
I hope you could find everything you need in this answer. Cheers
Have you incorporated CodeBuild into your pipeline?
You should
1) Create a pipeline whose source is your github account. Go through the setup procedure so that commits on a particular branch trigger the Codepipeline
2) Create a test stage in your Codepipeline which leverages the CodeBuild service. In order to run your Node tests, you might need to provide a configured build environment. And you probably also need to provide a build spec file that specifies the tests to run etc.
3) Assuming that the test stage passes, you can determine if the pipeline continues to another stage which is linked to an elasticbeanstalk app environment which supports the Node platform. These environments are purely for artifacts that have passed testing, so I see no need to have the .ebextensions commands written above.
Have a read of what CodeBuild can do to help you run tests for Node,
https://aws.amazon.com/codebuild/
Good luck!

Deploying to Firebase Hosting using CircleCI

I'm trying to figure out how to deploy to Firebase Hosting using CircleCI. As far as I know, there is no way to setup deployment with an SSH key, so I'm trying to find a way of logging into Firebase during deployment and push the code. What I have tried so far is the following in my circle.yml:
// circle.yml
deployment:
production:
branch: circle-deploy
commands:
- npm install -g firebase-tools
- firebase login | echo -e "${FIREBASE_EMAIL}\n${FIREBASE_PASSWORD}"
- firebase deploy
However, I keep getting the following error and I'm not sure how to remedy it.
stream.js:94
throw er; // Unhandled stream error in pipe.
^
Error: write EPIPE
at errnoException (net.js:904:11)
at Object.afterWrite (net.js:720:19)
I just had to do this and there is a easier way
On your machine, you can get your access token by typing
firebase login:ci
Save that token as an environment variable in circleci, $FIREBASE_TOKEN
For your deploy step, you can skip the login:
deployment:
production:
branch: master
commands:
- firebase deploy --token=$FIREBASE_TOKEN --non-interactive
A small addition to the other answers above...
In order to avoid installing firebase-tools globally in circle ci on every build:
Modify your package.json file to include firebase-tools as a dev dependency like so:
npm install --save-dev firebase-tools
Then in your circle.yml file:
deployment:
production:
branch: master
commands:
- ./node_modules/.bin/firebase deploy --token=$FIREBASE_TOKEN --non-interactive
For anyone else who stumbles upon this question, these are the steps I had to take to get CircleCI (and presumably any other CI) working with Firebase Hosting.
Generate a CI token: firebase login:ci
Save that token as an ENV var (FIREBASE_TOKEN)
Use the token in your deploy script: firebase deploy --token=$FIREBASE_TOKEN --non-interactive
Firebase added the login:ci recently to prevent people from using personal deploy tokens for CI services.
This is my initial setup, deploy only master, skip tests
run npm install -g firebase-tools on your local machine
run firebase login:ci to get the token on your local machine
run firebase init. This will create firebase.json make sure it is committed
configure FIREBASE_TOKEN in Environment Variables in BUILD SETTINGS of the project on circileci
//circle.yml
general:
branches:
only:
- master
test:
override:
- echo "test"
deployment:
production:
branch: master
commands:
- npm install -g firebase-tools
- firebase deploy --token=$FIREBASE_TOKEN --non-interactive
Here is the process we followed to deploy to CircleCi.
Store your username and password as environment variables at the project level in CircleCi.
Edit your circle.yml
deployment:
production:
branch: your_branch
commands:
- npm install -g firebase-tools
- firebase login --email $FIREBASE_USERNAME --password $FIREBASE_PASSWORD
- firebase deploy
Push to your branch
Seems to work fine.

Resources