An open API service indexing awesome lists of open source software.

https://github.com/rproenza86/aws-infrastructure-code-serverless-webapp

Serverless Web Application using Amazon DynamoDB, Lambda, API Gateway and S3.
https://github.com/rproenza86/aws-infrastructure-code-serverless-webapp

amazon-dynamodb api-gateway aws aws-lambda sdk serverless terraform

Last synced: 12 months ago
JSON representation

Serverless Web Application using Amazon DynamoDB, Lambda, API Gateway and S3.

Awesome Lists containing this project

README

          

# Serverless Web Apps using Amazon DynamoDB

> Terraform scripts to build the AWS environment where to deploy interactive serverless web applications which uses Amazon API Gateway calls to send requests to AWS Lambda backends that query Amazon DynamoDB data.

```
Tested using:

Terraform v0.11.10
+ provider.aws v1.42.0
```

## Use case description
Build and run an application and services without needing to provision, manage, and scale infrastructure.

Design, build, and deploy an interactive serverless web application, using a simple HTML/JavaScript web interface which uses Amazon API Gateway calls to end requests to AWS Lambda backends that query Amazon DynamoDB data.

### Infrastructure objectives

- Create Amazon DynamoDB
> Non-sql database to store the application data
- Create the necessary IAM roles and polices
> To access the application data
- Create AWS Lambda
> It will interact with DynamoDB and retrieve in several different way
- Create custom Lambda test
- Create Amazon API Gateway
> It will expose the Lambda function to the Internet as configured API
- Public web site
> UI to retrieve information from DynamoDB via Lambda functions
- Tie the three services together to build a functional serverless web app, and deploy it live on the web.

## How to use

### Pre-requirement: Set AWS environment variables

```
$ export TF_VAR_access_key=

$ export TF_VAR_secret_key=

$ export TF_VAR_region=
```

### Initialize a working directory

`$ cd serverless_intrastructure/`

`$ terraform init`

### Create an execution plan

`$ terraform plan`

### Apply the changes required to reach the desired state of the configuration

`$ terraform apply`

### Delete infrastructure

`$ terraform destroy`

## How to test?

When applied the configuration `terraform` will show us the next output:

```
Outputs:

Get heroes list API endpoint = https://xdlugc2266.execute-api.us-east-1.amazonaws.com/DeploySuperAPI/getheroeslist

Get heroes mission details API endpoint = https://xdlugc2266.execute-api.us-east-1.amazonaws.com/DeploySuperAPI/getmissiondetails

api_gateway_base_url = https://xdlugc2266.execute-api.us-east-1.amazonaws.com/DeploySuperAPI

dynamoDb_created_table_name = SuperMission
```

Use the `endpoint` info to test your api directly using this curl calls:

> Get heroes list

```bash
curl -X POST \
\
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 84ac228b-d6bc-4768-9a28-a21348f68275' \
-d '{}'
```

> Get missing details for an specific heroe

```bash
curl -X POST \
\
-H 'Access-Control-Request-Headers: content-type' \
-H 'Access-Control-Request-Method: POST' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Origin: null' \
-H 'Postman-Token: 8c2ee690-1e93-4784-9981-654cc7d6515c' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3604.0 Safari/537.36' \
-d '{
"superhero": "Batman"
}'
```

### Generate the SDK For Your API

On the `Stage` editor of your `ServerlessSuperHeroesApi` API Gateway, click the `SDK Generation` tab, then configure:



  • Platform: Javascript

  • Click Generate SDK

Next:


  • Save the zip file to a location on your computer.



  • Extract the content of the JavaScript zip file you downloaded in the serverless_intrastructure/files/apiGateway-js-sdk/ directory.


    There, you will have an HTML page that you will use to test your API.



After that:



  • Open index.html located on your serverless_intrastructure/files/apiGateway-js-sdk/ directory in your browser.


    You will notice that the drop-down list uses the API Gateway resource getheroeslist to invoke the getheroeslistFunction Lambda function.



  • Using the index web page, retrieve mission details.


  • Review the output.


Note To retrieve mission details using the index web page, select the Super Hero name in the drop down list and then click GO to display the Mission Status and Mission Dossier