How to deploy to AWS from the Serverless Framework


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:

  1. Install the Serverless Framework globally
  2. Create a new user in AWS IAM
    1. With “programmatic access” only
    2. “Attach existing policies directly” – choose “AdministratorAccess”
    3. Create the user but DON’T CLOSE THE CREDENTIALS SCREEN
  3. Add the credentials to your computer (AWS CLI or manually edit the file)
  4. Create a project
  5. 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.

Adding a user

Click Next: Permissions and here we’re going to Attach existing policies directly choosing the AdministratorAccess policy.

Adding AdministratorAccess

Click Next until you get to the Create user screen that looks like this. Stop here and do not click close.

Getting the User Credentials

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.

output from the serverless deployment

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.

Sam Williams

Sam is a Serverless Obsessive who runs Complete Coding, helping developers learn Serverless and companies make the most of the competitive advantage that Serverless gives them. Previous projects include: - Designing a chat platform that currently resolves over 250,000 customer inquiries a month for international retailers and local government - Architecting a backend system to support a 3D clothing visualisation tool - Building a solution to manage millions of dollars of communication software - Designing a "Video Editing in the Cloud" platform, enabling everyone from movie studios to indie film makers to harness the power of the Cloud - without needing to be cloud experts. - Launching a profitable Serverless Startup in under 30 days He has also been teaching cloud-based software development for 5 years and has taught Serverless development to thousands of people. The Complete Coding Youtube channel now has over 15,000 subscribers and over 1 million views

Recent Posts