The Serverless Framework is designed to make deploying Serverless applications to AWS as easy as possible. To deploy to AWS you need to set up some AWS credentials following these steps:
- Install the Serverless Framework globally
- Create a new user in AWS IAM
- With “programmatic access” only
- “Attach existing policies directly” – choose “AdministratorAccess”
- Create the user but DON’T CLOSE THE CREDENTIALS SCREEN
- Add the credentials to your computer (AWS CLI or manually edit the file)
- Create a project
- Run
npm i && sls deploy
That’s all you need to do. Now you can configure your serverless project with more API endpoints, DynamoDB tables and re-deploy it all with just a single sls deploy
command.
If you want some more detailed steps then here they are:
Install the Serverless Framework globally
To install the Serverless Framework you need to make sure that you have NodeJS and NPM installed.
Now all you need to do is run
npm install -g serverless
You can test that it has successfully installed by running
sls -v
Create a new user in AWS IAM
To create a user, log into your AWS console and search for IAM. Click on users
on the left menu and then Add users
.
Give your user profile a name and set it to have Access key - Programatic access
only.
Click Next: Permissions
and here we’re going to Attach existing policies directly
choosing the AdministratorAccess
policy.
Click Next
until you get to the Create user
screen that looks like this. Stop here and do not click close
.
Add AWS credentials to your computer
There are two ways add your AWS Credentials for the Serverless Framework, via the CLI or by manually editing the file.
Adding with CLI
With Serveless installed globally you can use their CLI to add your AWS credentials. Copy the access key ID
and Secret access key
from the user creation page.
serverless config credentials --provider aws --key ${Your access key ID} --secret ${Your secret access key} --profile serverlessUser
The last part of this command –profile serverlessUser is setting these credentials to a profile. This means you can add profile: serverlessUser
to your serverless config files. This tells it which credentials to use and therefore which AWS account to deploy to.
If you only have one AWS account you can remove this if you want. This will set your credentials to be default.
Manually editing the credentials file
Another way is to open the credentials file. This file is usually found at ~/.aws/credentials
. You may need to show hidden files in your file browser. For Mac press Command + Shift + .
Your file may be empty or may not even exist. If it doesn’t exist create an empty file. In that file you can now paste your credentials in this format. You can either set those credentials to be your default or give them a profile name.
[default]
aws_access_key_id=${Your access key ID}
aws_secret_access_key=${Your secret access key}
[serverlessUser]
aws_access_key_id=${Your secret access key}
aws_secret_access_key=${Your secret access key}
Create a project
Creating a project is super simple with Serverless. They have a set of templates that you can use. I would recommend starting with one of these three:
- aws-nodejs
- aws-nodejs-typescript
- aws-python3
Then you just need to run
serverless create --template ${Template Name} --path ${Project Name}
That will create a folder for your project and load the template configuration. It also gives you tips on how to deploy the project in the terminal output.
Deploying your project
To deploy your project you first need to navigate into the project and install the dependencies.
cd ${project Name} && npm i
Once all of the packages are installed you can deploy the project with a simple command
sls deploy
Or if you gave your credentials a profile
sls deploy --profile serverlessUser
After a short time for the app to deploy you should get an output like this.
You can then call your API and test your brand new Serverless Project
What Next
The next thing to do is to start building out your API. My Ultimate Guide to Serverless is a great place to start.