In this example, a simulated slot machine browser-based game invokes a Lambda function that generates the random results of each slot pull, returning those results as the file names of images used to display the result. The images are stored in an Amazon S3 bucket that is configured to function as a static web host for the HTML, CSS, and other assets needed to present the application experience.
The example is taken from:
I would recommend the following as pre-requisite knowledge, all of which are covered in the course:
- Assigning Roles to Lambda
- S3 Static Web Sites
- Dynamo DB
- Use of Access Keys
The tutorial uses a config.json file configured with suitable credentials (an access key id and secret access key) to give permission to the supplied Node.js scripts to create the resources for the project. In the Architecting course we talk about the use of passwords for Console access, and access keys for the use of CLI and SDK. So this is an example of using keys along with the AWS SDK.
The tutorial assets are downloaded from GitHub. Most of the tutorial involves using supplied Node.js scripts to create and configure the resources.
At the end of the tutorial, you click on the S3 Static Web site URL:
Clicking on the red handle spins the wheels and invokes the Lambda function, which selects images to display at random. The names of the images come from a Dynamo DB table, and the actual images from S3.
Pre-requistes for the tutorial are:
- Install Node.js on your computer to run various scripts that help set up the resources
If you don’t want to install these on your computer you could use Cloud9
Its fairly easy to get started with Cloud9, and it already has Node.js installed.
The tutorial guides you through the following:
Create an Amazon S3 bucket to store all the browser assets. These include the HTML file, all graphics files, and the CSS file. The bucket is configured as a static website.
The Architecting course covers Cognito at a very high level. This is the course slide on Cognito:
Elsewhere I have a post about another project called “WildRydes”. That project also used a browser and a Lambda function, but the browser made a call to API Gateway which then invoked Lambda. The browser gained permission to invoke API Gateway by getting a token from a Cognito User Pool after signing up and logging in and then providing that token to API gateway. That scenario did not require Identity Pools.
Next we create an execution role for the Lambda function.
Next we create and populate the Dynamo DB table.
Next we edit the supplied Lambda function to reference the role and the bucket, create a zip file of the Lambda function and upload it to the Lambda service.
Finally, we access the S3 static web site URL to test the application.
Most of the tutorial uses Node.js scripts to create, configure and populate resources, but sometimes the required command is not explicitly supplied, because the command can vary depending if you are using Linux, Mac, Windows. For example, once we have edited the Lambda function to customise it, to create a zip file of the Lambda function:
zip slotpull.js.zip slotpull.js
Also, the instructions to copy some of the assets to the S3 bucket were missing. I downloaded the assets to my local windows machine, and then used the S3 console to copy the relevant files.
The tutorial dosn’t ask you to change the region in the various scripts. You can use any region as long as you are careful to spot any references to a region and edit the files.
To tear down, delete all the resources as usual. Alternatively, as it is all serverless, you could leave it in place.