|
1 |
| -# DevOps |
2 |
| -In this unit, you'll be deploying a containerized version of an existing full stack React/Redux application configured with live reloading/HMR and a separate node api server to AWS using Travis-CI for continuous integration and deployment. This will be a fair bit of work to set up initially, but once that's done, the impact on developer workflow is nominal and the benefits are well worth it. |
| 1 | +# Deploy application in AWS Beanstalk |
3 | 2 |
|
4 |
| - |
| 3 | +## Setup |
5 | 4 |
|
6 |
| -The setup for this unit will be a little different than what you're used to. In order to clearly show the advantages of containerized applications, we want to share the same codebase. |
| 5 | +1. Install Docker and set up docker hub account |
7 | 6 |
|
8 |
| -So you'll only fork this repo to one partner's account. After you've forked it, go to your forked version and find the 'Settings' tab at the top of the repo. Select that and then select 'Collaborators and Teams'. Scroll down to the bottom and add the other partner as a Collaborator by entering their github name and setting their access to 'write'. |
| 7 | +2. If you want to install Chronos as dependency (optional) |
| 8 | + note that for AWS models, current version of Chronos do NOT require |
| 9 | + - add a .env file to the server folder that contains the following key/value pairs: |
9 | 10 |
|
10 |
| -### Verify Megamarkets App |
| 11 | + - CHRONOS_DB: MongoDB or PostgreSQL |
| 12 | + - CHRONOS_URI: The URI to the desired MongoDB or PostgreSQL database to save health metrics via Chronos |
| 13 | + - Then look at the package.json file in the server folder and note how @chronosmicro/tracker is included as a dependency: |
11 | 14 |
|
12 |
| -Before we get started, let's just make sure that the application loads with live updates/HMR without any kind of containerization. We won't have full functionality, as we don't have a database hooked up yet, but don't worry, we will! |
| 15 | + - If the @chronosmicro/tracker dependency is listed as a remote npm package (i.e. "@chronosmicro/tracker": "^8.0.1"), no further work is needed. |
13 | 16 |
|
14 |
| -1. Clone this repo |
15 |
| -2. Run `npm install` |
16 |
| -3. Run `npm run dev:hot` |
| 17 | + - If the @chronosmicro/tracker dependency is listed as a local npm package (i.e. "@chronosmicro/tracker": "file:./chronos_npm_package"), the Docker build will require that the the Chronos code is in this folder. Copy the chronos_npm_package folder in manually. |
17 | 18 |
|
18 |
| -That will start up your application, and open an unusual looking window in your browser. You can check out what this window is by looking in `webpack.config`.Navigate to the application on localhost in your browser to see megamarkets running. To verify that it's working, hop over to client/styles.css and change the color of the text to something exciting. You should see your change immediately reflected in the browser. Yes? Good. No? Send a help desk :smiley: |
19 | 19 |
|
20 |
| -Now that we have our baseline application working, let's containerize it. We'll begin by deleting the node_modules. |
21 | 20 |
|
22 |
| -`rm -rf node_modules/` |
| 21 | +## Part 1 - Docker |
23 | 22 |
|
24 |
| -**...and we'll never npm install in this directory again!** We'll be sourcing our node_modules from an image the whole team can share. |
| 23 | +1. build image for mm-prod |
| 24 | +``` |
| 25 | + docker build -t [orgname]/mm-prod . |
| 26 | +``` |
| 27 | +2. Build the docker image from Dockerfile |
25 | 28 |
|
26 |
| -Now, let's get this app containerized. Go ahead and open up the [README-DOCKER](https://github.com/CodesmithLLC/unit-13-devops/blob/master/README-DOCKER.md) file and get started. |
| 29 | + `docker build -t mm-prod .` |
| 30 | + |
| 31 | +3. Build the docker image from Dockerfile-dev |
| 32 | + |
| 33 | + `docker build -t mm-dev -f Dockerfile-dev .` |
| 34 | + |
| 35 | +4. Build the docker image from Dockerfile-postgres |
| 36 | + `docker build -t [orgname]/mm-postgres -f Dockerfile-postgres .` |
| 37 | + |
| 38 | + |
| 39 | +5. Run the container using docker-compose |
| 40 | + |
| 41 | + `docker-compose -f docker-compose-dev-hot.yml up` |
| 42 | + |
| 43 | +- Check out your running application at localhost:8080. Then use `docker compose down` to stop containers |
| 44 | + |
| 45 | +6. Testing |
| 46 | + `docker-compose -f docker-compose-test.yml up` |
| 47 | + - Check to see application passes tests |
| 48 | + |
| 49 | +7. Uploading Images to private repo: images mm-postgres, mm-dev and mm-prod |
| 50 | + - if you use docker hub, use `docker push [imagename:tag]` |
| 51 | + - if you use ECR, use push commend to upload images to private repositories. |
| 52 | + |
| 53 | +8. Update docker-compose files images URI (docker hub or ECR URI) |
| 54 | + |
| 55 | +9. Zip application code into an archive file |
| 56 | +``` |
| 57 | +git archive -v -o myMM.zip --format=zip HEAD |
| 58 | +``` |
| 59 | + |
| 60 | +## Part 2 - AWS Beanstalk, IAM, EC2, RDS, ECR |
| 61 | + |
| 62 | +1. Create user wiht `AdministratorAccess` policy and log in. |
| 63 | + |
| 64 | +2. Start Elastic Beanstalk with following settings: |
| 65 | + - Platform: Docker running on 64bit Amazon Linux 2 |
| 66 | + |
| 67 | +3. Upload zipped file (from previous step 9) |
| 68 | + |
| 69 | +4. Go to EC2 under the Services menu and select 'Key Pairs'. |
| 70 | + |
| 71 | +5. Create a new key pair and give it a meaningful name like 'mm-ec2-key' and select "pem" as the file format. |
| 72 | + |
| 73 | +6. Open the Dashboard for your new environment and follow the URL at the top to see your application running in the cloud. Then Select Configuration -> Security and set the new mm-ec2-key up as the EC2 key pair and wait for the environment to update. |
| 74 | + |
| 75 | + |
| 76 | +7. Create a new RDS database |
| 77 | + - set the "Master username" to mmadmin |
| 78 | + - Check 'Include previous generation classes'. |
| 79 | + - Keep the defaults for DB instance size (instance class should be db.t2.micro), Storage type (General Purpose SSD), and Allocated storage (20Gb). |
| 80 | + - Uncheck "Enable storage autoscaling" to ensure that we stay in the free tier |
| 81 | + - Under "Connectivity", expand the "Additional connectivity configuration" |
| 82 | + - Under "VPC security group", select "Create new" and give the VPC Security group the name mm-db-sg. Then select the availability zone closest to you. Leave the Database port as the default for PostgresQL databases (5432). |
| 83 | + - Finally, expand the "Additional Configuration" tab. Name your database mmdb under "Initial Database Name". Additionally, Uncheck "Enable automatic backups" to ensure that you stay within the free tier. |
| 84 | + - Hit "Create Database" at the bottom |
| 85 | + |
| 86 | +7. Go to EC2 instances |
| 87 | + - Services -> EC2 -> Security Groups There should be at least three security groups: 1. "default" (your default VPC's security group), 2. a randomly generated string of characters (this is the security group of your Elastic Beanstalk EC2 application), and 3. "mm-db-sg" which is the RDS security group we just created. |
| 88 | + - tag your RDS security group with a 'Name' and set it to mm-db-prod-sg. You can also set your EC2 security group to mm-prod-sg |
| 89 | + - new inbound rule for our 'mm-db-prod-sg' security group that allows postgres traffic from our 'mm-prod-sg' security group. |
| 90 | + - Go to Security group mm-db-prod-sg -> Inbound rules -> Edit Inbound rules |
| 91 | + - "Add Rule" |
| 92 | + - "Type" - "PostgresQL" |
| 93 | + - "Source" - "Custom" |
| 94 | + -"Source" textbox - Scroll down to the "Security Groups" subsection, and select the security group associated with your Elastic Beanstalk EC2 environment. (If you have more than one security group for your EB environment, add all of them to this rule) |
| 95 | + -"Save rules" |
| 96 | + |
| 97 | +8. Select the EC2 for the new enviroment and click "connect" - "SSH client", follow instruction to ssh into EC2 instance. Run the db creation script from EC2 |
| 98 | +``` |
| 99 | + psql -h [RDS instance endpoint] mmdb -U mmadmin -f /var/app/current/scripts/db_init_prod.sql |
| 100 | +``` |
| 101 | + |
| 102 | +9. Set up enviroment variables in application enviroment in Beanstalk |
| 103 | + - Configuration -> Software \ |
| 104 | + - Add the following environment variables |
| 105 | + - NODE_ENV : production |
| 106 | + - RDS_HOSTNAME : [RDS 'endpoint'] |
| 107 | + - RDS_DB_NAME : mmdb |
| 108 | + - RDS_USERNAME : mmadmin |
| 109 | + - RDS_PASSWORD : [your password] |
| 110 | + - RDS_PORT : 5432 |
| 111 | + |
| 112 | +10. Open the Dashboard for application environment and follow the URL at the top to see the application running in the cloud. |
27 | 113 |
|
0 commit comments