Deploy your application with VPC + EC2 + Docker.

Introduction

Based on the AWS Hands-on material held on 08/08/2019, I sometimes deployed the Nuxt app using AWS, so I will summarize it as a memorandum. Since I touched AWS for the first time this time, there may be mistakes, but I would appreciate your advice at that time. In addition, this time we aim to deploy the Nuxt app to the AWS environment as an example.

Target audience

Overall structure

It is like this.

Qiita AWS構成図-1.png

It may seem difficult, but it's very easy to understand if you look at each one, so let's take a look at it little by little.

VPC

What is VPC

** VPC ** is an abbreviation for ** Amazon Web Service Virtual Private Cloud **, which is a service that provides a private virtual cloud environment. On top of this, a virtual server ** EC2 ** (explained later) and a database are set up and used.

Creating a VPC

First, select "** VPC " from " Search for services " on the AWS top screen. スクリーンショット 2020-11-08 22.32.33.png Click " Start VPC Wizard **". スクリーンショット 2020-11-08 22.34.03.png ** * What is VPC Wizard ** The ** VPC Wizard ** is a feature that makes it easy to create ** VPC ** and ** NAT instances **. ** VPC ** is divided into a part called ** public subnet ** that can be accessed from the Internet and a part called ** private subnet ** that cannot be accessed directly from the Internet. A ** NAT instance ** can be simply a nuance that connects a private subnet and a public subnet. I don't know the details either. For more information, please read Official.

Press "** Select **". スクリーンショット 2020-11-08 22.49.21.png

Fill in the following and press "** Create VPC **". スクリーンショット 2020-11-08 22.50.40.png ** * What is Availability Zone (AZ) ** ** AZ ** is a group of facilities (data centers) that manage physical servers in the selected region. By using multiple ** AZ ** with ** ELB ** (not explained this time), you can continue to provide services even if the server goes down in the event of some kind of failure. Can be

Press "** OK " スクリーンショット 2020-11-08 22.50.53.png If the following is displayed, the creation is successful. スクリーンショット 2020-11-10 13.12.07.png Let's take a look at the subnet. Select " Subnet " from the menu on the left. Check the subnet you created earlier (this time, "Qiita-Public Subnet-1a"). Check if the availability zone is "ap-northeast-1a (apne1-az4)" and IPv4 is "10.0.0.0/24". スクリーンショット 2020-11-10 13.17.41.png Press " Route Table **". Make sure that the default (0.0.0.0/0) route points to the ** Internet gateway (igw-xxx ...) **. スクリーンショット 2020-11-10 13.33.48.png ** * What is Internet Gateway (IGW)? ** A function that allows ** subnets ** created within ** VPC ** to connect to the outside world (connect to global IP).

Route table settings

[This article](https://qiita.com/chro96/items/21863e0960ba4ac72470#:~:text=%E3%82%B5%E3%83%96%E3%83%8D%E3%83%83%E3 % 83% 88% E5% 86% 85% E3% 81% AB% E3% 81% 82% E3% 82% 8B% E3% 82% A4% E3% 83% B3% E3% 82% B9% E3% 82 % BF% E3% 83% B3% E3% 82% B9,% E9% 80% 81% E3% 82% 89% E3% 81% AA% E3% 81% 84% E3% 81% AE% E3% 81% A7%E3%80%81%E9%80%9A%E4%BF%A1%E3%81%A7%E3%81%8D%E3%81%BE%E3%81%9B%E3%82%93% According to E3% 80% 82) What is ** route table **?

It defines the rules for where the instances in the subnet go to communication. In other words, the route table is a table that looks at the destination (IP address) of the packet and describes where to send the communication. Since it carries packets by looking at this table, it cannot communicate with destinations that are not in the table because it does not send packets. The point is that it defines where communication can be performed for each subnet.

... apparently ...

This time, I haven't set the route table yet, so I'll set it.

Select "** Route Table " from the " VPC " menu. Select the ** route table ** with the ** VPC ID ** to be used this time, and press " Edit route " in " Route **". スクリーンショット 2020-11-10 15.52.25.png

Click "** Add route ". After that, select " Destination " as "0.0.0.0/0" and " Target " as " Internet GateWay ** → ** igw-xxx ..... ". .. After that, press " Save route **". スクリーンショット 2020-11-10 16.07.30.png

Click "** Edit Subnet Association " from " Subnet Association **". スクリーンショット 2020-11-10 16.12.53.png

Select the ** subnet ** to be used this time and press "** save **".

スクリーンショット 2020-11-10 16.13.05.png

VPC creation result

So far, we have been able to design as follows. Qiita AWS構成図-1.png

EC2

What is EC2

** EC2 ** is an abbreviation for ** Amazon Elastic Compute Cloud **, which is a service that builds a virtual server. Since you can freely decide the OS and specifications at your own discretion, you can realize redundant services.

Creating EC2

First, select "** EC2 " from " Select service **" on the AWS top screen. スクリーンショット 2020-11-10 15.01.49.png

Press "** Launch Instance **". スクリーンショット 2020-11-10 15.03.42.png

This time, press "** Select " of " Amazon Linux 2 AMI .... **". スクリーンショット 2020-11-10 15.04.48.png

This time, after selecting "** t2.micro ", press " Next step: Instance details **". スクリーンショット 2020-11-10 15.06.28.png

Select the ** VPC , ** subnet ** created earlier and change " auto-assigned public IP " to " enabled ". After that, click " Next step: Add storage **". スクリーンショット 2020-11-10 15.17.32.png

Just press "** Next step: Add tag **". スクリーンショット 2020-11-10 15.20.23.png

Just press "** Next step: Security group settings **". スクリーンショット 2020-11-10 15.21.31.png

After pressing "** Add rule ", select " Custom TCP ", " 3000 ", and " 0.0.0.0/0 " respectively. After that, press " Confirmation and Creation **". スクリーンショット 2020-11-10 15.23.09.png

Just press "** Start **". スクリーンショット 2020-11-10 15.26.02.png

Select "** Create new key pair ", enter " Key pair name " (this time " Qiita-example "), and press " Key pair download " And save the key pair wherever you like. After that, press " Create instance **". スクリーンショット 2020-11-10 15.26.32.png Be sure to save the ** key pair ** in a place you know, as you will use it later.

Click "** Instance display **". スクリーンショット 2020-11-10 15.33.14.png

If it is displayed like this, the creation is completed. You can change the name by pressing the red frame part of the image, so let's change it for the time being. スクリーンショット 2020-11-10 15.35.14.png

EC2 creation result

So far, we have been able to design as follows. Qiita AWS構成図-1.png

Deploy

Finally, we will deploy the Nuxt app. [This article](https://qiita.com/TakumaKurosawa/items/e67315583009257cd1ea#ec2%E3%82%A4%E3%83%B3%E3%82%B9%E3%82%BF%E3%83%B3 % E3% 82% B9% E3% 81% ABssh% E6% 8E% A5% E7% B6% 9A% E3% 81% 97% E3% 82% 88% E3% 81% 86) was very easy to understand. , I will go based on this.

ssh connection

Select "** Instance " from the " EC2 **" menu. Select your ** instance ** from the list of ** instances . Copy the value of " Public IPv4 Address **" from the menu below. スクリーンショット 2020-11-10 16.20.39.png

Start the terminal and enter the following command

#Downloaded aws-tutorial.Move to the directory where the pem is
$ cd ~/Where you saved the pair key

#pem file~/.Move to ssh
$ mv Qiita-example.pem ~/.ssh

#Grant access to the pem file
$ chmod 400 ~/.ssh/Qiita-example.pem

#ssh connection
$ ssh -i "~/.ssh/Qiita-example.pem" ec2-user@[IP address (paste) copied earlier]
Are you sure you want to continue connecting (yes/no)?Enter yes

If successful, the following screen will be displayed.


       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
8 package(s) needed for security, out of 17 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-****** ~]$

This completes the SSH connection. Now that you can execute commands within your Linux instance, install the required packages.

#update yum
$ sudo yum update -y

#Install Git
$ sudo yum install git -y

#Docker installation
$ sudo yum install -y docker

#Start Docker
$ sudo service docker start
$ sudo systemctl enable docker.service
$ sudo service docker status

#OK if you get the following output result
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running)since day 2019-12-22 03:26:02 UTC; 10s ago
     Docs: https://docs.docker.com
 Main PID: 12952 (dockerd)
   CGroup: /system.slice/docker.service
           └─12952 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd....

#Enable docker command
$ sudo usermod -a -G docker ec2-user

#Exit once
$ exit

#Ssh connection again
#Switch to superuser
$ sudo -i

#Download the required files
curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

#Give authority
chmod +x /usr/local/bin/docker-compose

#Return to normal user
exit

Now that both Git and Docker are working, I'll start selecting the project I want to deploy from my repository. This time we'll assume you've cloned the Nuxt app.

Connect with ssh again and

git clone [URL of your Git repository]

cd [Projects that have been cloned]

docker-compose up

The deployment is now successful.

http://[Public IPv4 address of VPC]:3000

When you access, you will see the screen of your app. https___qiita-image-store.s3.ap-northeast-1.amazonaws.com_0_317034_057c6c11-24a7-42e6-ae7c-ca6490478ff9.png

Thank you for your hard work.

Reference material

Recommended Posts

Deploy your application with VPC + EC2 + Docker.
Deploy a Docker application with Greengrass
Java: Start WAS with Docker and deploy your own application
Rails deploy with Docker
Deploy your application to WildFly
Web application built with docker (1)
Deploy to EC2 with CircleCi + Capistrano
Deploy SpringBoot application to AWS EC2
Deploy to heroku with Docker (Rails 6, MySQL)
heroku: docker: deploy
Deploy Line bot with rails5 + Docker + Heroku Note
EC2 ✕ Manual deployment with Docker / docker-compose / Asset compilation
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (5)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (6)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (3)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (2)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (1)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (4)
Deploy laravel using docker on EC2 on AWS ① (Create EC2 instance)
[Note] Build a Python3 environment with Docker in EC2
Display ROS application on Docker with GUI on host side
Launch MariaDB with Docker
Run Pico with docker
Explode Docker with WSL2
Use Puphpeteer with Docker
Operate Emby with Docker
Deploy RAILS on EC2
Try WildFly with Docker
Use ngrok with Docker
Run Payara with Docker
[Docker] Connection with MySQL
Php settings with Docker
Getting Started with Docker
Disposable PHP with Docker
Install Composer with Docker
Deploy laravel using docker on EC2 on AWS ② (Elastic IP acquisition-linking)
Guide: Deploy your application using the Alibaba Cloud Toolkit plugin
Register your own Docker image with ECR using AWS CLI
Deploy laravel using docker on EC2 on AWS ④ (git clone ~ deploy, migration)
Time is wrong with the application launched on the Docker container