Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lbrenman/578a26c45345bf0f373952ac1a12378c to your computer and use it in GitHub Desktop.
Save lbrenman/578a26c45345bf0f373952ac1a12378c to your computer and use it in GitHub Desktop.
Getting Started with AWS Amplify for Serverless Functions

Getting Started with AWS Amplify for Serverless Functions

The Amplify Framework provides a set of libraries, UI components, and a command line interface to build a mobile backend and integrate with your iOS, Android, Web, and React Native apps.

This article will describe how to use Amplify to easily create serverless API's.

I'll follow this tutorial.

Installing & configuring the Amplify CLI

We'll use the Amplify CLI for this tutorial.

Install using:

npm install -g @aws-amplify/cli

Note that I needed sudo prefix

Configure using:

amplify configure

This video describes the configuration process well.

Initialize a New Amplify Project

In your project folder, enter the following CLI command:

amplify init

Here's what happens behind the scenes

Create Our First Lambda Function

Using:

amplify add api

The Amplify CLI has provisioned 2 resources: an AWS Lambda function & an API Gateway configuration which allows us to interact with it via an HTTP endpoint. In the folder amplify/backend, we should now see two new folders: api & function.

Create These Resources in Your AWS Account

Using:

amplify push

When the aplify push cli command finishes, there will be a lambda function (testapifunction-dev) and API GW API (testapi) in the AWS console as shown below:

  • Lambda:

  • API GW:

Test Our Lambda Function Locally

amplify function invoke testapifunction

Specify index.js and handler when prompted.

You will also see the following console log:

Running "lambda_invoke:default" (lambda_invoke) task

EVENT: {"key1":"value1","key2":"value2","key3":"value3"}
App started

Success!  Message:
------------------
{"statusCode":404,"body":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot GET /</pre>\n</body>\n</html>\n","headers":{"x-powered-by":"Express","access-control-allow-origin":"*","access-control-allow-headers":"Origin, X-Requested-With, Content-Type, Accept","content-security-policy":"default-src 'self'","x-content-type-options":"nosniff","content-type":"text/html; charset=utf-8","content-length":"139","date":"Tue, 02 Apr 2019 18:47:22 GMT","connection":"close"},"isBase64Encoded":false}

Done.
Done running invoke function.

At this point we can make an API call in another terminal as follows:

curl localhost:3000/items

With response:

{"success":"get call succeed!","url":"/items"}

Test Your Deployed API

Go to the AWS console and select API GW and select your API (testapi) and select Dashboard from the menu on the left to see the public URL for your API as shown below:

The base URL for my API is: https://jww5m1qwg3.execute-api.us-east-1.amazonaws.com/dev/

I can call this API as follows:

curl https://jww5m1qwg3.execute-api.us-east-1.amazonaws.com/dev/items

With the same response as before:

{"success":"get call succeed!","url":"/items"}

Make Changes

Your API definitions are in the amplify/backend/function/testapifunction/src/app.js.

Edit your project locally and use the following to push the changes up to AWS:

amplify push

Cleanup

You can remove your Lambda function and API GW API as follows:

amplify remove api
amplify remove function
amplify push

Alternatively, you can remove the entire project, including the local project files, by using:

amplify delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment