aws v2.2.exe
by Evrim Persembe
通過埃夫里姆·佩塞姆貝
如何在AWS Elastic Beanstalk上部署Rails 5.2 PostgreSQL應用 (How to deploy a Rails 5.2 PostgreSQL app on AWS Elastic Beanstalk)
It’s official, using Heroku for all my Rails projects so far has spoiled me rotten. After receiving some AWS credits thanks to a pitch competition, I decided to deploy my latest project on Elastic Beanstalk (AWS’ Heroku competitor). All I have to say is that I miss Heroku.
官方的說法 ,到目前為止,在我所有的Rails項目中使用Heroku都使我很爛。 在一次激烈的競爭中獲得了一些AWS積分后,我決定在Elastic Beanstalk ( AWS的Heroku競爭對手)上部署我的最新項目。 我只想說我想念Heroku。
Alas, if you are in a similar situation, here are step-by-step instructions to deploying your Rails 5.2 / PostgreSQL app on Elastic Beanstalk.
las,如果您的情況類似,這里是在Elastic Beanstalk上部署Rails 5.2 / PostgreSQL應用程序的分步說明。
安裝Elastic Beanstalk CLI (Installing the Elastic Beanstalk CLI)
We will use the terminal in this tutorial. Let’s begin with installing the “Elastic Beanstalk Command Line Interface.” Here is how to do it on macOS using Homebrew:
我們將在本教程中使用終端。 讓我們從安裝“ Elastic Beanstalk命令行界面”開始。 這是使用Homebrew在macOS上執行操作的方法:
brew install awsebcli
If you are using another platform, googling “how to install awsebcli on [your platform]” should lead you in the right direction.
如果您使用的是其他平臺,則搜索“如何在[您的平臺]上安裝awsebcli”可以引導您朝正確的方向發展。
初始化Elastic Beanstalk (Initializing Elastic Beanstalk)
I will assume that you already have an Amazon Web Services account, if not go ahead and create one. Now, go into the directory of your project, and initialize Elastic Beanstalk:
我將假設您已經有一個Amazon Web Services帳戶,如果沒有,請繼續創建一個。 現在,進入項目目錄,并初始化Elastic Beanstalk:
cd my_projecteb init
Then the EB CLI will ask you a few questions to initialize the Elastic Beanstalk application. The initialization part is straightforward. If you get stuck anywhere you can check out the “Configure the EB CLI” page from the documentation.
然后,EB CLI將詢問您一些問題來初始化Elastic Beanstalk應用程序。 初始化部分很簡單。 如果您卡在任何地方,都可以從文檔中查看“配置EB CLI”頁面。
創造新環境 (Creating a new environment)
As you already know, your application can have many environments (think of them as different configurations). For example, you might have a “production” environment. This is the environment that you use for the user-facing version of your app. But you might want to have another environment named “staging.” This is where you try new versions of your app, before pushing it to the production environment.
如您所知,您的應用程序可以具有許多環境(將它們視為不同的配置)。 例如,您可能有一個“生產”環境。 這是您用于應用程序的面向用戶版本的環境。 但是您可能想要另一個名為“ staging”的環境。 在將應用推送到生產環境之前,您可以在這里嘗試新版本的應用。
We can create an environment using the command below:
我們可以使用以下命令創建環境:
eb create production
部署到Elastic Beanstalk (Deploying to Elastic Beanstalk)
Assuming you are using Git, commit your changes before deploying your application. The EB CLI deploys your last commit. If you deploy before committing, you will deploy an earlier version of your app.
假設您正在使用Git,請在部署應用程序之前提交更改。 EB CLI會部署您的最后一次提交。 如果在提交之前進行部署,則將部署應用程序的早期版本。
After committing your changes, deploy using the following:
提交更改后,請使用以下內容進行部署:
eb deploy
So far so good, now we need to set a few things before our app actually starts working.
到目前為止,到目前為止,我們需要在應用程序實際開始工作之前進行一些設置。
設置主密鑰 (Setting up the master key)
You can use the CLI for this purpose as well, but I prefer using the web panel for this. Here is how:
您也可以為此使用CLI,但是我更喜歡使用Web面板。 方法如下:
- Go to AWS, choose “Services -> Elastic Beanstalk,” then click on your environment. 轉到AWS,選擇“服務-> Elastic Beanstalk”,然后單擊您的環境。
- Open the “Configuration” tab, and click “Modify” under the box titled “Software.” 打開“配置”選項卡,然后在標題為“軟件”的框下單擊“修改”。
Under “Environment properties,” add a new key named
RAILS_MASTER_KEY
. Set its value to the content of your “master.key” file. You can find this file in the “config” directory of your Rails app.在“環境屬性”下,添加一個名為
RAILS_MASTER_KEY
的新密鑰。 將其值設置為“ master.key”文件的內容。 您可以在Rails應用程序的“ config”目錄中找到此文件。- Click on the “Apply” button at the bottom of the page. 單擊頁面底部的“應用”按鈕。
設置PostgreSQL數據庫 (Setting up a PostgreSQL database)
Elastic Beanstalk provides an easy way to set up a database, which you can reach through “Configuration -> Database.” I prefer not to use that because if you need to rebuild your Elastic Beanstalk environment, your database will be deleted. So, we will set up the database separate from our Elastic Beanstalk environment.
Elastic Beanstalk提供了一種建立數據庫的簡便方法,您可以通過“配置->數據庫”來訪問它。 我不想使用它,因為如果您需要重建Elastic Beanstalk環境,則數據庫將被刪除。 因此,我們將與Elastic Beanstalk環境分開設置數據庫。
在RDS上創建PostgreSQL數據庫 (Creating a PostgreSQL database on RDS)
- Go to AWS, choose “Services -> RDS.” 轉到AWS,選擇“服務-> RDS”。
- Choose “Create database.” 選擇“創建數據庫”。
- Choose “PostgreSQL,” and click “Next.” 選擇“ PostgreSQL”,然后單擊“下一步”。
- Select your use case, “Production” or “Dev/Test,” and click “Next.” 選擇您的用例“生產”或“開發/測試”,然后單擊“下一步”。
Here, you can try different options, and see what the estimated monthly costs are. Settle with something that is within your budget. You can start with a
db.t2.micro
instance, no multi-AZ deployment and a general purpose SSD.在這里,您可以嘗試其他選項,并查看估計的每月費用。 用您預算范圍內的東西解決。 您可以從
db.t2.micro
實例開始,無需多db.t2.micro
區部署和通用SSD。- Choose an instance identifier, this is sort of a “namespace.” 選擇一個實例標識符,這就是一種“命名空間”。
- Choose a username and password, keep these handy for now, click “Next.” 選擇一個用戶名和密碼,暫時保留它們,單擊“下一步”。
- On the “Configure advanced settings” section, the important thing is the security groups. Select “Choose existing VPC security groups,” and select the security group that looks like “…-AWSEBSecurityGroup-…” 在“配置高級設置”部分,重要的是安全組。 選擇“選擇現有的VPC安全組”,然后選擇類似于“ ...- AWSEBSecurityGroup -...”的安全組。
Pick a database name, such as
my_app_production
.選擇一個數據庫名稱,例如
my_app_production
。- Click on “Create database,” this will take a while. 點擊“創建數據庫”,這將需要一段時間。
允許訪問數據庫 (Allowing access to the database)
In the meantime, let’s add Postgres access to your security group:
同時,讓我們向您的安全組添加Postgres訪問權限:
- Go to AWS, choose “Services -> EC2.” 轉到AWS,選擇“服務-> EC2”。
- Click on “Security Groups” on the left panel. 單擊左側面板上的“安全組”。
- Choose the security group from the previous section. 從上一節中選擇安全組。
- Go to the “Inbound” tab, and click on “Edit.” 轉到“入站”選項卡,然后單擊“編輯”。
Click on “Add Rule.” For “Type,” choose “PostgreSQL,” and for “Source” type in the ID of the security group that you are adding this rule to. It should be right above the “Inbound” tab and should look like
sg-*
.點擊“添加規則”。 對于“類型”,選擇“ PostgreSQL”,對于“源”,輸入要添加此規則的安全組的ID。 它應該在“入站”選項卡的正上方,并且應該看起來像
sg-*
。- Click “Save.” 點擊“保存”。
設置生產數據庫配置 (Setting up the production database configuration)
Now, in your Rails directory, open config/database.yml
. Change it as such:
現在,在您的Rails目錄中,打開config/database.yml
。 這樣更改:
# ...
production: <<: *default database: <%= ENV['RDS_DB_NAME'] %> username: <%= ENV['RDS_USERNAME'] %> password: <%= ENV['RDS_PASSWORD'] %> host: <%= ENV['RDS_HOSTNAME'] %> port: <%= ENV['RDS_PORT'] %>
將相關環境變量添加到Elastic Beanstalk (Adding relevant environment variables to Elastic Beanstalk)
We told Rails to get the information for the production database using the above environment variables. Now we need to make sure that our Elastic Beanstalk environment includes these variables:
我們告訴Rails使用上述環境變量來獲取生產數據庫的信息。 現在,我們需要確保我們的Elastic Beanstalk環境包含以下變量:
- Go to AWS, choose “Services -> Elastic Beanstalk,” then click on your environment. 轉到AWS,選擇“服務-> Elastic Beanstalk”,然后單擊您的環境。
- Open the “Configuration” tab, and click “Modify” under the box titled “Software.” 打開“配置”選項卡,然后在標題為“軟件”的框下單擊“修改”。
- Under “Environment properties,” add the following key-value pairs: 在“環境屬性”下,添加以下鍵值對:
RDS_DB_NAME
: Database name you picked when setting up your database.RDS_DB_NAME
:設置數據庫時選擇的數據庫名稱。RDS_USERNAME
: Username you picked when setting up your database.RDS_USERNAME
:設置數據庫時選擇的用戶名。RDS_PASSWORD
: Password you picked when setting up your database.RDS_PASSWORD
:設置數據庫時選擇的密碼。RDS_HOSTNAME
: Go to “Services -> RDS,” and you can find this information under the “Connect” section of your database instance information page. It is called “Endpoint.”RDS_HOSTNAME
:轉到“服務-> RDS”,您可以在數據庫實例信息頁面的“連接”部分下找到此信息。 它稱為“端點”。RDS_PORT
: Set this to 5432.RDS_PORT
:將此設置為5432。- Click on the “Apply” button at the bottom of the page. 單擊頁面底部的“應用”按鈕。
After this, commit your Rails app directory again, and run eb deploy
. You might want to wait a few minutes before doing this because Elastic Beanstalk does some stuff in the background after updating environment variables.
之后,再次提交您的Rails應用程序目錄,然后運行eb deploy
。 您可能需要等待幾分鐘,因為執行更新環境變量后,Elastic Beanstalk在后臺執行了一些操作。
After these steps, your Rails app “should” be running.
完成這些步驟后,“應”運行Rails應用程序。
還是行不通? (Still not working?)
If there are any issues, you can go to your EB environment on the AWS web panel, click on “Logs,” and choose “Request Logs -> Last 100 Lines” to see the logs. But before doing that, I’d recommend trying to run your Rails app using the production environment on your local machine by using the command rails s RAILS_ENV=product
ion.
如果有任何問題,您可以在AWS Web面板上轉到EB環境,單擊“日志”,然后選擇“請求日志->最后100行”以查看日志。 但是,在此之前,我建議您嘗試使用本地nd rails s RAILS_ENV=product
ion在本地計算機上的生產環境中運行Rails應用程序。
I’ll be the first to admit that I’m not the most experienced person when it comes to deployment. As I said, I always used Heroku in the past, and I probably will use it for my future projects as well. These steps worked for me after a few days of scratching my head trying to set up my Rails app on Elastic Beanstalk, so I wanted to share these in hopes of saving time for people who are in the same situation I was. So, take this all with a grain of salt, and good luck!
我將是第一個承認我在部署方面不是最有經驗的人。 就像我說的那樣,我過去一直使用Heroku,將來我也可能會使用它。 經過幾天的努力,嘗試在Elastic Beanstalk上設置我的Rails應用程序之后,這些步驟對我有用。因此,我想與大家分享這些信息,以期為處于同樣狀況的人們節省時間。 因此,帶上一粒鹽和好運來帶走這一切!
If you like this article, follow me on Twitter or sign up to my newsletter to get notified when I write new articles. I write about software and startups.
如果您喜歡這篇文章,請在Twitter上關注我或注冊我的新聞通訊,以在我撰寫新文章時得到通知。 我寫有關軟件和創業公司的文章。
If you are looking for a Rails developer, I’m currently available for remote work. Feel free to get in touch with me at hi{at}evrim.io.
如果您正在尋找Rails開發人員,那么我現在可以進行遠程工作。 歡迎訪問hi {at} evrim.io與我聯系。
Originally published at evrim.io on November 28, 2018.
最初于2018年11月28日發布在evrim.io上。
翻譯自: https://www.freecodecamp.org/news/how-to-deploy-a-rails-5-2-postgresql-app-on-aws-elastic-beanstalk-34e5cec3a984/
aws v2.2.exe