— nodejs, typescript, yarn — 1 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
).
1brew install awsebcli
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 directories2eb init --platform node.js --region ap-southeast-23
4# deploy our app to an environment named 'nick'5eb create nick6
7# open website in browser8eb open9
10# N.B. ElasticBeanstalk only deploys GIT COMMITted resources!11eb deploy12
13# destroy environments14eb terminate nick15
16# delete everythign (incl application)17eb terminate nick --force --all --ignore-links
We didn't delve into it too far yet, but basically you can manage the config per environment.
1# use a given configuration name2eb use nick3
4# save config to local disk5eb config save --cfg nick6
7# push config8eb 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.47OptionSettings:8 aws:elasticbeanstalk:command:9 BatchSize: "30"10 BatchSizeType: Percentage11 aws:elb:policies:12 ConnectionDrainingEnabled: "true"13 aws:elb:loadbalancer:14 CrossZone: "true"15 aws:elasticbeanstalk:environment:16 ServiceRole: aws-elasticbeanstalk-service-role17 aws:elasticbeanstalk:application:environment:18 AWS_REGION: '`{"Ref" : "AWS::Region"}`'19 TEST_NICK: "Rocks"20 aws:elasticbeanstalk:healthreporting:system:21 SystemType: enhanced22 aws:autoscaling:launchconfiguration:23 IamInstanceProfile: aws-elasticbeanstalk-ec2-role24 aws:autoscaling:updatepolicy:rollingupdate:25 RollingUpdateType: Health26 RollingUpdateEnabled: "true"27EnvironmentTier:28 Type: Standard29 Name: WebServer30AWSConfigurationTemplateVersion: 1.1.0.0
.elasticbeanstalk/config.yml
1branch-defaults:2 dev:3 environment: nick-dev4 main:5 environment: null6 group_suffix: null7global:8 application_name: aws-elasticbeanstalk-vanilla9 branch: null10 default_ec2_keyname: null11 default_region: ap-southeast-212 default_platform:13 arn:aws:elasticbeanstalk:ap-southeast-2::platform/Node.js 14 running14 on 64bit Amazon Linux 2/5.4.415 include_git_submodules: true16 instance_profile: null17 platform_name: null18 platform_version: null19 profile: null20 repository: null21 sc: git22 workspace_type: Application
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-2b2aws ec2 create-default-subnet --availability-zone ap-southeast-2c
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 running4 on 64bit Amazon Linux 2/5.4.4