Skip to content
MountainPass Blog

Adventures with ElasticBeanstalk

nodejs, typescript, yarn1 min read

- by Nick Grealy and Sam Wei

Background:

We wanted to setup a simple NodeJS project in ElasticBeanstalk. We followed these instructions which seem to be a bit outdated.

The .ebextensions/*.config appears to be the old way of doing things. Documentation isn't centrally located... so you end up looking all over for it. Our suggestion - use config.yml and environments (e.g. dev.cfg.yml).

Steps to deploy

1. Install CLI

1brew install awsebcli

Read more about cli.

2. Deploy app

Notes: when deploying your app, AWS will use package.json by convention for it's configuration.

For instance, it will look for a npm start script to execute your app, and will use the engines block to determine which version of Node to run.

package.json

1{
2 "name": "myapp",
3 "version": "1.0.0",
4 "private": true,
5 "engines": {
6 "node": "14.16.0"
7 },
8 "scripts": {
9 "start": "node index.js"
10 },
11 "dependencies": {
12 "express": "~4.16.1"
13 }
14}

Some deployment commands...

1# creates the local eb config directories
2eb init --platform node.js --region ap-southeast-2
3
4# deploy our app to an environment named 'nick'
5eb create nick
6
7# open website in browser
8eb open
9
10# N.B. ElasticBeanstalk only deploys GIT COMMITted resources!
11eb deploy
12
13# destroy environments
14eb terminate nick
15
16# delete everythign (incl application)
17eb terminate nick --force --all --ignore-links

3. Manage env config

We didn't delve into it too far yet, but basically you can manage the config per environment.

1# use a given configuration name
2eb use nick
3
4# save config to local disk
5eb config save --cfg nick
6
7# push config
8eb config --update file://`pwd`/.elasticbeanstalk/saved_configs/nick.cfg.yml

.elasticbeanstalk/saved_configs/yourenv.cfg.yml

1EnvironmentConfigurationMetadata:
2 Description: Configuration created from the EB CLI using "eb config save".
3 DateCreated: "1629899407000"
4 DateModified: "1629899407000"
5Platform:
6 PlatformArn: arn:aws:elasticbeanstalk:ap-southeast-2::platform/Node.js 14 running on 64bit Amazon Linux 2/5.4.4
7OptionSettings:
8 aws:elasticbeanstalk:command:
9 BatchSize: "30"
10 BatchSizeType: Percentage
11 aws:elb:policies:
12 ConnectionDrainingEnabled: "true"
13 aws:elb:loadbalancer:
14 CrossZone: "true"
15 aws:elasticbeanstalk:environment:
16 ServiceRole: aws-elasticbeanstalk-service-role
17 aws:elasticbeanstalk:application:environment:
18 AWS_REGION: '`{"Ref" : "AWS::Region"}`'
19 TEST_NICK: "Rocks"
20 aws:elasticbeanstalk:healthreporting:system:
21 SystemType: enhanced
22 aws:autoscaling:launchconfiguration:
23 IamInstanceProfile: aws-elasticbeanstalk-ec2-role
24 aws:autoscaling:updatepolicy:rollingupdate:
25 RollingUpdateType: Health
26 RollingUpdateEnabled: "true"
27EnvironmentTier:
28 Type: Standard
29 Name: WebServer
30AWSConfigurationTemplateVersion: 1.1.0.0

.elasticbeanstalk/config.yml

1branch-defaults:
2 dev:
3 environment: nick-dev
4 main:
5 environment: null
6 group_suffix: null
7global:
8 application_name: aws-elasticbeanstalk-vanilla
9 branch: null
10 default_ec2_keyname: null
11 default_region: ap-southeast-2
12 default_platform:
13 arn:aws:elasticbeanstalk:ap-southeast-2::platform/Node.js 14 running
14 on 64bit Amazon Linux 2/5.4.4
15 include_git_submodules: true
16 instance_profile: null
17 platform_name: null
18 platform_version: null
19 profile: null
20 repository: null
21 sc: git
22 workspace_type: Application

Read more about configuration

More Reading

Blue / Green deployments

Errors

1. Default subnet in ap-southeast-2b not found

12021-08-25 12:05:52 ERROR Creating load balancer failed Reason: Default subnet in ap-southeast-2b not found (Service: AmazonElasticLoadBalancing; Status Code: 409; Error Code: InvalidConfigurationRequest; Request ID: 23078f28-f251-4f01-843a-bf1992e5ab64; Proxy: null)
22021-08-25 12:06:07 ERROR Stack named 'awseb-e-7rwhhxm3gp-stack' aborted operation. Current state: 'CREATE_FAILED' Reason: The following resource(s) failed to create: [AWSEBLoadBalancer].

Solution: Create a default subnet for your region:

1aws ec2 create-default-subnet --availability-zone ap-southeast-2b
2aws ec2 create-default-subnet --availability-zone ap-southeast-2c

2. Your environment is using a deprecated platform branch. It might not be supported in the future.

Solution:

a) Remove aws:elasticbeanstalk:container:nodejs setting from .ebextensions/<whatever>.config

Delete this...

1option_settings:
2 aws:elasticbeanstalk:container:nodejs:
3 NodeCommand: "npm start"

b) Specify latest platform in config.yml.

Update this...

1global:
2 default_platform:
3 arn:aws:elasticbeanstalk:ap-southeast-2::platform/Node.js 14 running
4 on 64bit Amazon Linux 2/5.4.4
© 2021 by MountainPass Blog. All rights reserved.
Theme by LekoArts