在 Amazon EC2 上部署 Web 應用程式
入門指南
單元 2:自動化組態
在本單元中,您將學習使用使用者資料設定 EC2 執行個體並在 EC2 執行個體上安裝軟體套件
簡介
在 Amazon EC2 中啟動執行個體時,您可以選擇將使用者資料傳遞至該執行個體,此執行個體可以用來執行一般自動的設定任務,甚至在執行個體啟動之後執行指令碼。您將使用此功能,透過指定在首次啟動時安裝和設定的軟體來簡化應用程式部署,並將範例應用程式複製到執行個體。本單元將介紹如何建立使用者資料並將其新增至在上一單元中建立的 EC2 執行個體。
您將學到的內容
- 如何將使用者資料新增至 EC2 執行個體
- 建立使用者資料以在作業系統上安裝和設定所有必需的軟體套件
- 透過將 Web 應用程式複製到執行個體來部署它
完成時間
5 分鐘
單元先決條件
- 有管理員等級存取權限的 AWS 帳戶**
- 建議的瀏覽器:最新版的 Chrome 或 Firefox
[**]過去 24 小時內建立的帳戶可能尚未有權存取本教學課程所需的服務。
實作
將使用者資料新增至您的 EC2 執行個體
SampleApp 資料夾中託管的範例 Web 應用程式是您將部署的 Python 應用程式。它需要 Nginx 和 uWSGI 才能運行。要安裝這些元件,需要遵循許多步驟。首先,您需要安裝所有作業系統套件,設定 nginx 和 uwsgi,確保它們正在運行,然後將範例應用程式複製到執行個體。SampleApp/configure_amz_linux_sample_app.sh 中提供了設定所有這些設定步驟的指令碼檔案 - 如果您想了解有關如何設定執行個體的更多資訊,請檢視其中的步驟。
要部署 Web 應用程式,您需要將程式碼新增至 CDK,以將組態檔案和指令碼以及範例應用程式複製到 S3。您將設定組態指令碼。為此,請在 ec2-cdk-stack.ts 中先前程式碼下方新增以下程式碼:
// Use an asset to allow uploading files to S3, and then download it to the EC2 instance as part of the user data
// --- Sample App ---
// Upload the sample app to S3
const sampleAppAsset = new s3assets.Asset(this, "SampleAppAsset", {
path: path.join(__dirname, "../../SampleApp"),
});
// Allow EC2 instance to read the file
sampleAppAsset.grantRead(role);
// Download the file from S3, and store the full location and filename as a variable
const sampleAppFilePath = ec2Instance.userData.addS3DownloadCommand({
bucket: sampleAppAsset.bucket,
bucketKey: sampleAppAsset.s3ObjectKey,
});
// --- Sample App ---
// --- Configuration Script ---
// Upload the configuration file to S3
const configScriptAsset = new s3assets.Asset(this, "ConfigScriptAsset", {
path: path.join(__dirname, "../../SampleApp/configure_amz_linux_sample_app.sh"),
});
// Allow EC2 instance to read the file
configScriptAsset.grantRead(ec2Instance.role);
// Download the file from S3, and store the full location and filename as a variable
const configScriptFilePath = ec2Instance.userData.addS3DownloadCommand({
bucket: configScriptAsset.bucket,
bucketKey: configScriptAsset.s3ObjectKey,
});
// Add a line to the user data to executy the downloaded file
ec2Instance.userData.addExecuteFileCommand({
filePath: configScriptFilePath,
arguments: sampleAppFilePath,
});
// --- Configuration Script ---
所有步驟都將新增至執行個體的使用者資料指令碼中,並在它首次啟動時執行。在部署所有內容之前,還要執行一個步驟:將輸出新增至 CDK 堆疊,以便更輕鬆地透過 SSH 連線到執行個體。在上面的基礎設施中,您建立了一個 SSH 金鑰,該金鑰儲存在 AWS Secret Manager 中。要將其下載到您的工作站,您需要擷取它。您還需要執行個體的公開 IP,以及要執行的 SSH 命令。
將以下程式碼新增至堆疊底部:
// Create outputs for connecting
// Output the public IP address of the EC2 instance
new cdk.CfnOutput(this, "IP Address", {
value: ec2Instance.instancePublicIp,
});
// Command to download the SSH key
new cdk.CfnOutput(this, "Download Key Command", {
value:
"aws secretsmanager get-secret-value --secret-id ec2-ssh-key/cdk-keypair/private --query SecretString --output text > cdk-key.pem && chmod 400 cdk-key.pem",
});
// Command to access the EC2 instance using SSH
new cdk.CfnOutput(this, "ssh command", {
value:
"ssh -i cdk-key.pem -o IdentitiesOnly=yes ec2-user@" +
ec2Instance.instancePublicIp,
});
這三個輸出將顯示以下內容:
- 如何下載 SSH 金鑰以存取執行個體
- 執行個體的公開 IP
- 存取執行個體的 SSH 命令。
您現在準備好部署堆疊。