Skip to content

Commit

Permalink
Merge pull request #31 from aws-samples/hb-dev-update
Browse files Browse the repository at this point in the history
updated sample app ebextensions, updated syntax and conventions
  • Loading branch information
hacbrown authored Mar 1, 2023
2 parents 4f0f08f + d38133a commit 093dd30
Show file tree
Hide file tree
Showing 9 changed files with 1,850 additions and 432 deletions.
2 changes: 1 addition & 1 deletion .ebextensions/options.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ option_settings:
NEW_SIGNUP_TOPIC: '`{"Ref" : "NewSignupTopic"}`'
aws:elasticbeanstalk:container:nodejs:
ProxyServer: nginx
aws:elasticbeanstalk:container:nodejs:staticfiles:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: /static
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

# Elastic Beanstalk CLI Files
.elasticbeanstalk/*
node_modules/*
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
# AWS Elastic Beanstalk Express Sample App
# AWS Elastic Beanstalk Express Sample App with Dynamo
This sample application uses the [Express](https://expressjs.com/) framework and [Bootstrap](http://getbootstrap.com/) to build a simple, scalable customer signup form that is deployed to [AWS Elastic Beanstalk](http://aws.amazon.com/elasticbeanstalk/). The application stores data in [Amazon DynamoDB](http://aws.amazon.com/dynamodb/) and publishes notifications to the [Amazon Simple Notification Service (SNS)](http://aws.amazon.com/sns/) when a customer fills out the form.

## Features
### Themes
This example cannot be run locally.

You can get started using the following steps:
1. Install the [AWS Elastic Beanstalk Command Line Interface (CLI)](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html).
2. Add policies to the [default instance profile](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/iam-instanceprofile.html) to grant the EC2 instances in your environment permission to access DynamoDB and Amazon SNS:
- Open the [Roles](https://console.aws.amazon.com/iam/home#roles) page in the IAM console.
- Choose `aws-elasticbeanstalk-ec2-role`.
- On the Permissions tab, choose Attach policies.
- Select the managed policy for the additional services that your application uses. For this specific example, add `AmazonSNSFullAccess` and `AmazonDynamoDBFullAccess`.
- Choose Attach policy.
3. Run `eb init --platform node.js --region <region>` to initialize the folder for use with the CLI. Replace `<region>` with a region identifier such as `us-east-2` (see [Regions and Endpoints](https://docs.amazonaws.cn/en_us/general/latest/gr/rande.html#elasticbeanstalk_region) for a full list of region identifiers).
4. Run `eb create --sample nodejs-example-express-dynamo` to begin the creation of a sample application that contains a load-balanced environment with the default settings for the Node.js platform.
5. Once the environment creation process completes, run `eb open` to load the sample environment in your browser to verify the deployment has succeeded and is accessible.
6. Deploy the source in this bundle using `eb deploy`.
7. Once the deployment of this source bundle completes, run `eb open` to interact with the new webpage.
8. Run `eb terminate --all` to clean up.


## Themes
The code includes several Bootstrap themes from [bootswatch.com](http://bootswatch.com/). You can dynamically change the active theme by setting the THEME environment variable in the [Elastic Beanstalk Management Console](https://console.aws.amazon.com/elasticbeanstalk):

![](misc/theme-flow.png)
Expand All @@ -13,20 +30,4 @@ Installed themes include:
* [default](http://bootswatch.com/default)
* [flatly](http://bootswatch.com/flatly)
* [slate](http://bootswatch.com/slate)
* [united](http://bootswatch.com/united)

You can get started using the following steps:
1. [Install the AWS Elastic Beanstalk Command Line Interface (CLI)](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html).
2. Create an IAM Instance Profile named **aws-elasticbeanstalk-sample-role** with the policy in [iam_policy.json](iam_policy.json). For more information on how to create an IAM Instance Profile, see [Create an IAM Instance Profile for Your Amazon EC2 Instances](https://docs.aws.amazon.com/codedeploy/latest/userguide/how-to-create-iam-instance-profile.html).
3. Run `eb init -r <region> -p "Node.js"` to initialize the folder for use with the CLI. Replace `<region>` with a region identifier such as `us-west-2` (see [Regions and Endpoints](https://docs.amazonaws.cn/en_us/general/latest/gr/rande.html#elasticbeanstalk_region) for a full list of region identifiers). For interactive mode, run `eb init` then,
1. Pick a region of your choice.
2. Select the **[ Create New Application ]** option.
3. Enter the application name of your choice.
4. Answer **yes** to *It appears you are using Node.js. Is this correct?*.
7. Choose whether you want SSH access to the Amazon EC2 instances.
*Note: If you choose to enable SSH and do not have an existing SSH key stored on AWS, the EB CLI requires ssh-keygen to be available on the path to generate SSH keys.*
4. Run `eb create --instance_profile aws-elasticbeanstalk-sample-role` to begin the creation of your environment.
1. Enter the environment name of your choice.
2. Enter the CNAME prefix you want to use for this environment.
5. Once the environment creation process completes, run `eb open` to open the application in a browser.
6. Run `eb terminate --all` to clean up.
* [united](http://bootswatch.com/united)
30 changes: 15 additions & 15 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Include the cluster module
var cluster = require('cluster');
const cluster = require('cluster');

// Code to run if we're in the master process
if (cluster.isMaster) {

// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
const cpuCount = require('os').cpus().length;

// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
for (let i = 0; i < cpuCount; i += 1) {
cluster.fork();
}

Expand All @@ -23,18 +23,18 @@ if (cluster.isMaster) {

// Code to run if we're in a worker process
} else {
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
const AWS = require('aws-sdk');
const express = require('express');
const bodyParser = require('body-parser');

AWS.config.region = process.env.REGION

var sns = new AWS.SNS();
var ddb = new AWS.DynamoDB();
const sns = new AWS.SNS();
const ddb = new AWS.DynamoDB();

var ddbTable = process.env.STARTUP_SIGNUP_TABLE;
var snsTopic = process.env.NEW_SIGNUP_TOPIC;
var app = express();
const ddbTable = process.env.STARTUP_SIGNUP_TABLE;
const snsTopic = process.env.NEW_SIGNUP_TOPIC;
const app = express();

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
Expand All @@ -49,7 +49,7 @@ if (cluster.isMaster) {
});

app.post('/signup', function(req, res) {
var item = {
const item = {
'email': {'S': req.body.email},
'name': {'S': req.body.name},
'preview': {'S': req.body.previewAccess},
Expand All @@ -62,7 +62,7 @@ if (cluster.isMaster) {
'Expected': { email: { Exists: false } }
}, function(err, data) {
if (err) {
var returnStatus = 500;
let returnStatus = 500;

if (err.code === 'ConditionalCheckFailedException') {
returnStatus = 409;
Expand All @@ -89,9 +89,9 @@ if (cluster.isMaster) {
});
});

var port = process.env.PORT || 3000;
const port = process.env.PORT || 3000;

var server = app.listen(port, function () {
const server = app.listen(port, function () {
console.log('Server running at http://127.0.0.1:' + port + '/');
});
}
15 changes: 0 additions & 15 deletions iam_policy.json

This file was deleted.

Loading

0 comments on commit 093dd30

Please sign in to comment.