One small step forward w/ Elastic Beanstalk
Tags: aws, elasticbeanstalk
Published: 2022-05-05 14:25:38

Finally got my PHP app running & setup (just the code bits .. still need to work on other configs + database), but wanted to share some of my findings in the case it may be helpful to others.

## Setup SSH

This is key (pun intended ;)) because it can help you see what's going on, versus making a change, uploading it, see if it works, if not, request the logs, and go through them. There are a few steps to get SSH working:

1. Create key pair: `ssh-keygen -b 2048 ~/.ssh/elastic-beanstalk`
2. Create key file config in .ebextensions (I called mine `.ebextensions/02_setup_ssh.config`) and it looks like #1 below.
3. Open the SSH port. This is another config file (I called mine `.ebextensions/03_open_sshport.config`) and it looks like #2 below.
4. Find the IP address from your EC2 instances to connect to and then connect: `ssh -i "~/.ssh/elastic-beanstalk" root@[IP ADDRESS]`

## Container commands
One thing that was tripping me up was that my PHP app (which is a Codeigniter v4 framework app) has a different project layout, where the root folder has various config stuff and then the CodeIgniter app is installed in a `ci4` subdirectory (which is where the `package.json` and `composer.json` files were), and the public webroot is a subdirectory from that, too.

So after the code is extracted, but before deployment, I need to do the `composer install` and `npm install`. So I had another .ebextensions config to run container commands to do those, but I was using the static path of `/var/app/current/ci4`, which was affecting the current deployment and not the staged code. So then when EB deployed the staged code, it didn't have the expected `node_modules` or `vendor` directories. This took a *lot* of back & forth, but eventually, I got it working and it looks like #3 below. Oh, and as a bonus with #3, it also gets nodejs (which includes npm) installed, which was something I was wrestling with earlier.

\#1:
```
files:
  "/root/.ssh/authorized_keys" :
    mode: "000600"
    owner: root
    group: root
    content: |
      [contents from your elastic-beanstalk.pub file]
```

\#2:
```
Resources:
  sslSecurityGroupIngress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: {"Fn::GetAtt" : ["AWSEBSecurityGroup", "GroupId"]}
      IpProtocol: tcp
      ToPort: 443
      FromPort: 443
      CidrIp: 0.0.0.0/0
  sshSecurityGroupIngress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: {"Fn::GetAtt" : ["AWSEBSecurityGroup", "GroupId"]}
      IpProtocol: tcp
      ToPort: 22
      FromPort: 22
      CidrIp: 0.0.0.0/0
```

(I went ahead and added 443 for setting up HTTPS later)

\#3:
```
commands:
  00_install_curl:
    command: sudo yum -y install curl
  01_nodejs_setup:
    command: curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
  02_install_nodejs:
    command: sudo yum -y install nodejs
  03_enable_epel:
    command: sudo amazon-linux-extras install epel

container_commands:
  01_npm_install:
    command: 'cd ci4 && npm install'
    test: '[ ! -d ci4/node_modules ] && echo "node_modules files not installed"'
  02_composer_install:
    command: 'cd ci4 && /usr/bin/composer.phar install'
    test: '[ ! -d ci4/vendor ] && echo "vendor files not installed"'
```