AWS with Serverless Framework EC2 deployment - amazon-web-services

We are planning to deploy our API to AWS Lambda, and for local development, we use Serverless framework with Serverless Offline. Can we deploy the same serverless APP to dedicated server instances as we have some requirements where the same APP could be deployed in server as well. Can i use serverless-offline to run it. Or do we have any better mechanism . By the way we use Node JS.

This really depends how you have your code setup, we use hapi to map the requests from lambda to a specific handler and we can also use hapi to run a server. The only effort that we had was to include a middleware to convert the lambda event to a request so that we can use the same handlers.
Here's something that we based our approach:
http://www.carbonatethis.com/hosting-a-serverless-hapi-js-api-with-aws-lambda/
I wouldn't use serverless offline to be run in ec2 mainly because serverless offline does not work as a server and certain things don't work as expected when compared to lambdas.

You can deploy the same APIs to Lambda also, For that you need to change your main index file. Or you can create 2 files let say index.js, server.js, where in index file simple Hapi/express code can be used and in server file, lambda function can be used. Routing will be the same for both files. You can use index.js for your local use and for lambda deployment you can use server.js.

Related

How to deploy nuxt frontend with express backend on AWS?

I have a Nuxt v2 SSR application as the frontend running on port 3000
I have an express API as the backend running on port 8000
I have a python script that loads data from external APIs and needs to run continuously
Currently all of them are separate projects with their own package.json and what not
How do I deploy this to AWS?
The only thing I have figured out so far is that I may have to deploy express API as an Elastic Beanstalk application.
Should I have a separate docker-compose file for each because they are separate projects currently or should I merge them into one project with a single docker-compose file
I saw similar questions asked about React, could really appreciate some direction here in Nuxt
None of these similar questions are based on Nuxt
How to deploy separated frontend and backend?
How to deploy a React + NodeJS Express application to AWS?
How to deploy backend and frontend projects if they are separate?
There are a couple of approaches depending on your workload and budget. Let's cover what the options are and which ones apply to the work at hand.
Serverless Approach with Server-side rendering (SSR)
Tutorial
By creating an API Gateway route that invokes NuxtRenderer in a Lambda. The resulting generated HTML/JS/CSS would be pushed to S3 bucket. The S3 bucket acts as a Cloudfront origin. Use the CDN to cache the least updated items and use the API call to update the cache when needed. This is the cheapest way to deploy, but if you don't have traffic some customers may experience brief lag when cache updates hit. Calculator
Serverless Approach via static generation
This is the easiest way to get going. All the routes that do not have dynamic template generation can simply live in an S3 bucket. The simplest approach is to run nuxt generate and then upload the resulting contents of dist to the bucket. No node backend here, which is not the requirement in the question but its worth mentioning.
Well documented on NuxtJS.org and free tier.
Serverless w/ Elastic Beanstalk
IMO this approach is unnecessary and slightly dated for the offering AWS provides in 2022. It still works of course, but the cost to benefits aren't attractive.
To do this you need to use the eb command line tool and set NPM_CONFIG_UNSAFE_PERM=true. AFAIK there is nothing else special that needs to happen for eb to know what to do from there. Calculator
Server-ish Approach(es)
Another alternative is to use Lightsail and NodeJS server. Lightsail offers low (but not lower than serverless) cost to a full time NodeJS server. In this case you would want to clone your project to the server then setup systemd script to keep nodejs running.
Yet another way to do this and have some scalability is to use ECS and Docker. In this case you would create a Dockerfile that builds the containers, executes npm start, and exposes the port Nuxt runs on to the host. This example shows how to run it using Fargate, which is essentially a serverless version of EC2 machine. Calculator
There are some ways for you to deploy your stack in AWS. I can give you some options, but you're best shot if you want to save some costs is by using Lambda Functions as your backend, S3 for your front-end and a batch Lambda job for your python script.
For your backend - https://github.com/vendia/serverless-express
For your nuxt frontend - https://nuxtjs.org/deployments/amazon-web-services
For your python job - https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/RunLambdaSchedule.html
It's not way too simple to execute all of those, but with the following links you'll probably have an idea of how you may implement your solution.

AWS SAM Local vs Serverless-offline

We are a Terraform shop for standing up our infrastructure on AWS and I am using AWS SAM Local to:
Test AWS Lambdas locally without having to deploy on the cloud.
I can also run integration tests on locally running lambda function as it will call downstream services that are running in the cloud.
I am curious about serverless-offline. I don't have much experience with the npm serverless library and wondering if others have any experience how it compares to SAM Local? Does it have same capabilities that I am able to accomplish with AWS SAM Local?
The sam local cli command and the Serverless Offline plugin work in similar ways. Both run a Docker instance and emulate API Gateway and Lambda. Additionally, Serverless Framework supports other platforms, unlike SAM Local.
The biggest advantage of using one or another is the ability to test your Serveless functions locally with the tool that you are currently using. So if you are using AWS SAM, the sam local will be the best option, similar if you are using the Serverless Framework, as the best option will be using the Serverless-offline plugin.
Serverless Framework included offline testing long before SAM Local arrived, so maybe you can find options that are not available yet using SAM local. sam local can have some advantages, such as template validation.
Both systems use Node.js and support API Gateway and Lambda, but neither currently supports DynamoDB execution, so you need to work in a way to make your DynamoDb available locally.
If you want to decide if the best option for you is the AWS SAM or the Serveless Framework, you can take a look in comparison like this one: Comparing AWS SAM with the Serverless framework.

Run golang lambda function locally

I'm trying to develop a lambda that has to work with S3 and dynamoDB.
The thing is that because I am not familiar with the SDK of aws for go I will have lots of tests and tries.
Each time I will change the code is another time I have to compile the project and upload it to aws.
Is there any way to do it locally? pass some kind of configuration that lets me call the services of aws locally, from my computer?
Thanks!
This has to do mostly with golang, other languages like python can run directly on the aws lambda function page, and node has cloud9 support.
You can use the lambci docker image(s) to execute your code locally using the same Lambda runtimes that are used on AWS.
https://github.com/lambci/docker-lambda
You can also run dynamo DB locally in another container as well
https://hub.docker.com/r/amazon/dynamodb-local/
To simulate credentials/roles that would be available on Lambda, just pass in your Api creds VIA environment variables. ( for s3 access )
Cheers
-JH
You could use this aws-lambda-go-test module which can run lambda locally and can be used to test the actual response from lambda
full disclosure I forked and upgraded this module

Deploying an Angular 2 app built with webpack using Bitbucket

I have searched high and low for an answer to this question but have been unable to find one.
I am building an Angular 2 app that I would like hosted on an S3 bucket. There will be an EC2 (possibly) backend but that's another story. Ideally, I would like to be able to check my code into Bitbucket, and by some magic that alludes me I would like S3, or EC2, or whatever to notice via a hook, for instance, that the source has changed. Of course the source would have to be built using webpack and the distributables deployed correctly.
Now this seems like a pretty straightforward request but I can find no solution exception something pertaining to WebDeploy which I shall investigate right now.
Any ideas anyone?
Good news, AWS Lambda created for you.
You need to create following scenario and code to achieve your requirement.
1-Create Lambda function, this function should do the following steps:
1-1- Clone your latest code from GitHub or Bitbucket.
1-2- install grunt or another builder for your angular app.
1-3- install node modules.
1-4- build your angular app.
1-5- copy new build to your S3 bucket.
1-6- Finish.
2-Create AWS API gateway with one resource and one method point to your Lambda function.
3-Goto your GitHub or Bitbucket settings and add webhook with your API gateway.
4-Enjoy life with AWS.
;)
Benefits:
1-You only charge when you have the new build.
2-Not need any machine or server (EC2).
3-You only maintain one function on Lambda.
for more info:
https://aws.amazon.com/lambda/
https://aws.amazon.com/api-gateway/
S3 isn't going to listen for Git hooks and fetch, build and deploy your code. BitBucket isn't going to build and deploy your code to S3. What you need is a service that sits in-between BitBucket and S3 that is triggered by a Git hook, fetches from Git, builds, and then deploys your code to S3. You need to search for Continuous Integration/Continuous Deployment services which are designed to do this sort of thing.
AWS has CodePipeline. You could setup your own Jenkins or TeamCity server. Or you could look into a service like CodeShip. Those are just a few of the many services out there that could accomplish this task. I think any of these services will require a bit of scripting on your part in order to get them to perform the actual webpack and copy to S3.

Are there any emulator for AWS Lambda?

I want to run Go service on AWS Lambda by this instruction without actually using AWS Lambda service. Looking for a vagrant or vbox which can emulating Lambda. I know it's a weird request. Is it possible?
There are a few options (from damn simple to more complete)
Node Lambda – Command line tool to locally run and remotely deploy your node.js applications to Amazon Lambda.
Custom event used to simulate an input similar to what Lambda would get on AWS.
Allows testing locally, simulating AWS Lambda environment.
Deploy
grunt-aws-lambda – Grunt plugin to assist in developing functions for AWS Lambda.
All of the above
Function packaging (ZIP)
Separation of environments (dev, test, production)
Adds stripping and packaging of function before deploying (including only necessary packages).
serverless (formerly JAWS) – Complete application framework to build applications based on different services in AWS (that relies heavily on AWS Lambda).
All of the above
Includes boilerplate setup
Simulates an API gateway to test locally
Hope this is useful.