Skip to content

Commit

Permalink
Merge pull request #23 from NYPL-discovery/beanstalkify
Browse files Browse the repository at this point in the history
Beanstalkified application
  • Loading branch information
nonword authored Jun 8, 2017
2 parents 9270909 + b7e13f6 commit 640efc1
Show file tree
Hide file tree
Showing 8 changed files with 1,516 additions and 1,451 deletions.
5 changes: 5 additions & 0 deletions .ebextensions/00_environment.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
option_settings:
- option_name: LOCAL
value: true
- option_name: PORT
value: 8081
14 changes: 14 additions & 0 deletions .ebextensions/01_cloudwatch_agent_config.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
files:
'/etc/awslogs/config/application_log.conf' :
mode: "000444"
owner: root
group: root
content: |
[application_log]
file = /var/app/current/log/discovery-api.log*
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/app/current/log/discovery-api.log"]]}`
log_stream_name = {instance_id}

commands:
restart_awslogs:
command: service awslogs restart
5 changes: 5 additions & 0 deletions .ebextensions/02_enable_log_streaming.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
option_settings:
aws:elasticbeanstalk:cloudwatch:logs:
StreamLogs: true
DeleteOnTerminate: false
RetentionInDays: 180
3 changes: 3 additions & 0 deletions .ebextensions/03_nodecommand.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
option_settings:
aws:elasticbeanstalk:container:nodejs:
NodeCommand: "npm start"
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ node_modules

# node-lambda build zip file
build

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,43 @@ This will bypass the AWS Serverless Express package that wraps the API and allow

# AWS Services

Currently, the Discovery API is deployed as an AWS Lambda and the endpoints from the Express app are behind an AWS API Gateway called NYPL API - Lambda. Because the API Gateway has a specific endpoint structure, the Discovery API had to conform to that, specifically updating to `/api/v[VERSION_OF_API]/discovery/resources`. The Discovery API can still be run locally, or on a server, through the `node app.js` command.
The Discovery API can be deployed as an AWS Lambda or as an AWS Elastic Beanstalk Application.

The endpoints from the Express app are behind an AWS API Gateway called NYPL API - Lambda. Because the API Gateway has a specific endpoint structure, the Discovery API had to conform to that, specifically updating to `/api/v[VERSION_OF_API]/discovery/resources`. The Discovery API can still be run locally, or on a server, through the `node app.js` command.

### AWS Elastic Beanstalk

#### Initial Environment Creation

1. `.ebextensions` directory needed at application's root directory
2. `.ebextensions/00_environment.config` to store environment variables. For environment variables that needs to be hidden,
3. `.ebextensions/03_nodecommand.config` to start node app after deployment.
4. `eb init -i --profile <<your AWS profile>>`
5. Initial creation of instance on Beanstalk:

Please use the instance profile of _cloudwatchable-beanstalk_.
Which has all the permissions needed for a traditional or Docker-flavored Beanstalk
machine that wants to log to CloudWatch.

```bash
eb create discovery-api-dev
--instance_type t2.small
--instance_profile cloudwatchable-beanstalk
--cname discovery-api-dev
--vpc.id vpc-1e293a7b
--vpc.elbsubnets subnet-be4b2495,subnet-4aa9893d
--vpc.ec2subnets subnet-12aa8a65,subnet-fc4a25d7
--vpc.elbpublic
--tags Project=Discovery
--keyname dgdvteam
--scale 2
--envvars ELASTICSEARCH_HOST="xxx"
```

#### Deployment

For subsequent deployment, run:
`eb deploy <<environment name>> --profile <<your AWS profile>>`

### Lambda

Expand Down
8 changes: 5 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require('./routes/resources')(app)
require('./routes/misc')(app)

app.esClient = new elasticsearch.Client({
host: config['elasticsearch'].host
host: process.env.ELASTICSEARCH_HOST || config['elasticsearch'].host
})

app.all('*', function (req, res, next) {
Expand All @@ -49,9 +49,11 @@ app.get('/api/v0.1/discovery/swagger', function (req, res) {

// Only start the Express server locally:
if (process.env.LOCAL) {
const port = process.env.PORT || config['port']

require('./lib/globals')(app).then((app) => {
app.listen(config['port'], function () {
console.log('Server started on port ' + config['port'])
app.listen(port, function () {
console.log('Server started on port ' + port)
})
})
}
Expand Down
Loading

0 comments on commit 640efc1

Please sign in to comment.