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
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.
- 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.
$ export TF_VAR_access_key=<your_key>
$ export TF_VAR_secret_key=<your-secret>
$ export TF_VAR_region=<your_region>
$ cd serverless_intrastructure/
$ terraform init
$ terraform plan
$ terraform apply
$ terraform destroy
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
curl -X POST \
<Use your "Get heroes list API endpoint" output value> \
-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
curl -X POST \
<Use your "Get heroes mission details API endpoint" output value> \
-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"
}'
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