[Advanced] - AWS Intro, Set up AWS and Serverless, Lambda, API Gateway
Amazon Web Services(AWS) is a platform of cloud computing which provides a simple way to access servers, storage, databases and a broad set of applications. We are basically renting out AWS servers to host our application instead of having to maintain our own. There are many type of services in AWS, each performing a specific functionality.
- Low Cost - As developers, we pay end up paying less if we want to host our application on AWS servers. Since AWS has many active users, it drives down the cost of server use.
- Advanced security features - Expert security staff are continually upgrading and maintaining servers.
- Highly reliable - your data is matained in many different data centers just in case one goes down
AWS allows us to access the their platform through programmatic access or AWS management console access. In this course, we will be using programmatic access through the Serverless Framework which allows us to access AWS through the command line and code editor.
An AWS service that allows developers to run code without having to manage a server. No express server needed. A Lambda is basically a function in the cloud.
An AWS service that enables developers to create, publish, maintain, monitor, and secure APIs at any scale. You can create APIs that access AWS or other web services, as well as data stored in the AWS Cloud. It allows developers to create CRUD routes(GET, POST, PUT, DELETE).
Moving our application to the AWS cloud/servers. In our case, we will be deploying our Lambda function.
We will conifgure our AWS credentials and Serverless credentials, then will set up our Lambda function and API Gateway to deploy a GET
url enpoint for our frontend client.
- Your instructor should have provided you with a Access Key ID and a Secret Access Key. Please save them in a safe location.
- Open up your git-bash terminal
- Install aws-cli for windows
- Configure Access_ID, Secrect_Access_id, and region on our computer
- Type
aws configure
then hit enter- Input the following
- access_id
- access_secret_id
- region: us-west-2
- Input the following
- Type
- npm install Serveless Framework
npm install -g serverless
- Set Serverless provider credentials
serverless config credentials --provider aws --key YOUR_ACCESS_KEY --secret YOUR_SECRET_KEY
- Create a new directory/project
mkdir YOUR_PROJECT_NAME
- CD into your directory/project
cd YOUR_PROJECT_NAME
- Create boilerplate/template to use Serverless**
serverless create --template aws-nodejs
- Change service name in
serverless.yml
file - Uncomment the following:*
stage: dev
region: us-east-1
- Then set region to
us-west-2
- Deploy your code
serverlesss deploy
- Invoke deployed function
serverless invoke -f FUNCTION_NAME
- Change message in lambda function then redeploy function only
serverless deploy -f FUNCTION_NAME
- Invoke deployed function
serverless invoke -f FUNCTION_NAME
- Get logs from deployed function
serverless logs -f FUNCTION_NAME -t
- If need help with serverless commands
serverless --help
Create API Gateway GET
request
- Paste in the following code snippet in
serverless.yml
functions:
FUNCTION_NAME:
handler: handler.FUNCTION_NAME
events:
- http:
path: PATH_NAME
method: get
cors: true
- Paste in the following in your
response
object, above thebody
property
This allows CORS
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true
},
- Deploy your code
serverless deploy
- You should see a
GET
endpoint url underendpoints:
- Write your functions
- Use
serverless deploy
only when you've made changes toserverless.yml
- Use
serverless deploy function -f FUNCTION_NAME
to rapidly deploy changes when you are working on a specific AWS Lambda Function. - Use
serverless invoke -f FUNCTION_NAME
to test your AWS Lambda Functions on AWS. - Open up a separate tab in your console and stream logs in there via
serverless logs -f FUNCTION_NAME -t
.
- In your Lambda function, create a random food generator
- Create file structure in your root path for you frontend
serverless-demo
|
+ --public
|
+-- index.html
+-- styles.css
+-- app.js
- Use axios in your
app.js
to get a random food from your newly created GET endpoint - Render the following to your browser
Today, I will eat a ${INSERT_FOOD_VALUE}
- Your browser should render a different food everytime you refresh
How does Amazon Web Services (AWS) work? - Quora
Serverless Framework - AWS Lambda Guide - Quick Start Serverless Framework - CORS & API Gateway