└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Elastic Beanstalk CLI Cheat Sheet 2 | Elastic Beanstalk brings the benefits of PaaS (Platform as a service) providers such as Heroku to AWS infrastructure. This is a reference for the EB CLI, which is a commandline interface that is more user-friendly than standard AWS CLI commands. 3 | 4 | ## Quick Start 5 | 6 | Install / Update EB CLI 7 | 8 | ``` 9 | // Linux 10 | $ pip install --upgrade --user awsebcli 11 | 12 | // Mac 13 | $ brew install awsebcli 14 | ``` 15 | 16 | Initialize `eb cli` in the working directory. You will need to choose `Application`->`Environment`. 17 | 18 | ``` 19 | $ eb init 20 | ``` 21 | 22 | This will create and deploy the new environment at the same time. Then open in your browser. 23 | 24 | $ eb create 25 | $ eb open 26 | 27 | Subsequent deployments can be made through a simple deploy subcommand. Uncommitted staged files can also be added with the `--staged` flag. 28 | 29 | $ eb deploy 30 | $ eb deploy --staged 31 | 32 | ## Basic Configuration 33 | 34 | Edit document root 35 | 36 | $ eb config 37 | 38 | View and Set Environment variables 39 | 40 | ``` 41 | $ eb printenv 42 | $ eb setenv ExampleVar=ExampleValue KEY1=VALUE1 ... 43 | ``` 44 | 45 | ## Common Tasks 46 | 47 | List environments and show current 48 | 49 | $ eb list 50 | 51 | Change current environment 52 | 53 | $ eb use [environment] 54 | 55 | Browse log files 56 | 57 | $ eb logs 58 | 59 | List, get, save locally and update configuration. 60 | 61 | ``` 62 | $ eb config list 63 | $ eb config get [config] 64 | $ eb config save 65 | $ eb config put [config] 66 | ``` 67 | 68 | Apply configuration on current environment. 69 | 70 | $ eb config --cfg [config] 71 | 72 | Deploy using saved configuration 73 | 74 | $ eb create [environment] --cfg gateway 75 | 76 | Deploy worker environment 77 | 78 | $ eb create [environment] -t worker 79 | 80 | Initialize with alternate AWS credentials from ~/.aws/credentials 81 | 82 | $ eb init --profile user2 83 | 84 | ## Common Errors 85 | 86 | >Failed to run npm install 87 | 88 | >EACCES: permission denied 89 | 90 | ``` 91 | # Force npm to run node-gyp also as root, preventing permission denied errors in AWS with npm@5 92 | # Add this line to file named .npmrc in project root (may need to create) 93 | unsafe-perm=true 94 | # Add this line to file named .ebignore in project root (may need to create) 95 | node_modules/ 96 | ``` 97 | 98 | >Cannot find Module 99 | 100 | >ENOENT: no such file or directory 101 | 102 | ``` 103 | # devdependencies not installed due to npm install run with --production by default 104 | # force installation of devdependencies with the following command 105 | eb setenv NPM_USE_PRODUCTION=false 106 | ``` 107 | 108 | ## Node 109 | 110 | https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs.container.html 111 | 112 | ## All commands 113 | 114 | ``` 115 | abort Cancels an environment update or deployment. 116 | clone Clones an environment. 117 | config Edits the environment configuration settings or manages saved configurations. 118 | console Opens the environment in the AWS Elastic Beanstalk Management Console. 119 | create Creates a new environment. 120 | deploy Deploys your source code to the environment. 121 | events Gets recent events. 122 | health Shows detailed environment health. 123 | init Initializes your directory with the EB CLI. Creates the application. 124 | labs Extra experimental commands. 125 | list Lists all environments. 126 | local Runs commands on your local machine. 127 | logs Gets recent logs. 128 | open Opens the application URL in a browser. 129 | platform Manages platforms. 130 | printenv Shows the environment variables. 131 | scale Changes the number of running instances. 132 | setenv Sets environment variables. 133 | ssh Opens the SSH client to connect to an instance. 134 | status Gets environment information and status. 135 | swap Swaps two environment CNAMEs with each other. 136 | terminate Terminates the environment. 137 | upgrade Updates the environment to the most recent platform version. 138 | use Sets default environment. 139 | ``` 140 | 141 | ## Advanced 142 | 143 | **Change to Root User** 144 | 145 | Sometimes you may need to change to the root user. Run: 146 | 147 | ``` 148 | sudo su - 149 | ``` 150 | 151 | This works because by default `ec2-user` is in the wheel group. 152 | 153 | **Change NodeJs Files Directly in Elastic Beanstalk for Testing** 154 | 155 | In the horrible event you decide to test something in production, simply make changes to your files and restart node. 156 | 157 | Elastic Beanstalk will automatically restart your service by re-running your start command, specified in your Elastic Beanstalk config. 158 | 159 | ``` 160 | eb ssh 161 | 162 | # go edit your files in 163 | cd /var/app/current 164 | 165 | # like: vi dist/server.js 166 | 167 | sudo pkill -f node 168 | ``` 169 | 170 | _[source](https://superuser.com/questions/1036374/in-elasticbeanstalk-can-i-ssh-in-and-manually-restart-the-node-app)_ 171 | 172 | **Run NPM or NODE** 173 | 174 | Need to install something for testing like `npm install ssh2-sftp-client`, or `node -v` ? 175 | 176 | Simply put the node bin directory into your path. I change to root user first: `sudo su - root` 177 | 178 | Then: 179 | 180 | ``` 181 | export PATH="$PATH:/opt/elasticbeanstalk/node-install/node-v10.20.1-linux-x64/bin" 182 | ``` 183 | 184 | You will see there are many versions to pick from in node-install. In our case, we chose v10.20.1 185 | 186 | After running this command, you can do `node -v` and check that it is equal to the version you specified. 187 | 188 | 189 | **Give the Web User a Shell** 190 | 191 | 192 | ElasticBeanstalk gives you a nodejs user but may also give other users. A php app is run by the `webapp` user. To see your users, run: 193 | 194 | ``` 195 | cat /etc/passwd 196 | ``` 197 | 198 | As you can see, `nodejs` and `webapp` by default do not have a shell, meaning you cannot switch to them while ssh'd in. 199 | 200 | For testing, it may also become neccesary to switch to your web user, but that user doesn't have a shell by default. So to give it a shell, run: 201 | 202 | ``` 203 | # first switch to root user 204 | # then... 205 | usermod -s /bin/sh nodejs 206 | ``` 207 | 208 | Once they have a shell, you can switch to them from the root user: 209 | 210 | ``` 211 | su - nodejs 212 | # or 213 | su - webapp 214 | ``` 215 | --------------------------------------------------------------------------------