aws中部署防火墻_如何在AWS中設置自動部署

aws中部署防火墻

by Harry Sauers

哈里·紹爾斯(Harry Sauers)

如何在AWS中設置自動部署 (How to set up automated deployment in AWS)

設置和配置服務器 (Provisioning and Configuring Servers)

介紹 (Introduction)

In this tutorial, you’ll learn how to use Amazon’s AWS SDK to deploy your Python application to a real-world server.

在本教程中,您將學習如何使用Amazon的AWS開發工具包將Python應用程序部署到實際服務器上。

Before we begin, you should have a working knowledge of Python, Git, and general cloud infrastructure. I recommend Codecademy if you want to learn these fundamentals.

在開始之前,您應該具有Python,Git和常規云基礎架構的工作知識。 如果您想學習這些基礎知識,我建議您使用Codecademy 。

Some of the Terminal/Bash commands I use are for an Ubuntu system. If they don’t work, check for your system’s equivalent.

我使用的一些Terminal / Bash命令用于Ubuntu系統。 如果它們不起作用,請檢查系統是否等效。

入門 (Getting Started)

  • Spin up your favorite Python IDE and create a new project.

    啟動您最喜歡的Python IDE并創建一個新項目。

  • Create your main project file and name it whatever you want — I chose “app.py” for simplicity.

    創建您的主項目文件并隨便命名—我為簡單起見選擇了“ app.py”。
  • Add print("Hello Python!") to the file and run it to ensure your environment is set up correctly.

    添加print("Hello Python!") 到文件并運行它,以確保正確設置環境。

  • Next, we need to install Amazon’s SDK. Though AWS does provide a standard HTTP API, the software development kit is much more robust. The SDK handles tedious and lower-level operations for you.r

    接下來,我們需要安裝Amazon的SDK。 盡管AWS確實提供了標準的HTTP API,但是該軟件開發套件更加強大。 SDK為您處理乏味的底層操作。
  • Open a terminal and type sudo pip3 install boto3 and enter your sudo password, if needed.

    打開終端,然后輸入sudo pip3 install boto3并輸入您的sudo密碼(如果需要)。

  • Add import boto3 to the top of your Python file.

    import boto3添加到Python文件的頂部。

  • This allows us to use Amazon’s SDK in our Python application.

    這使我們能夠在Python應用程序中使用Amazon的SDK。

AWS憑證 (AWS Credentials)

Before we can actually use anything on AWS, we need credentials for our AWS account. If you don’t have one, you can sign up here.

在我們可以在AWS上實際使用任何東西之前,我們需要我們的AWS賬戶憑證。 如果您沒有,可以在這里注冊。

  • Go to your Identity and Access Management panel and click “Add user” under the “Users” tab.

    轉到“ 身份和訪問管理”面板 ,然后在“用戶”選項卡下單擊“添加用戶”。

  • Enter a username and tick the box beside “programmatic access.”

    輸入用戶名,然后選中“程序訪問”旁邊的框。
  • Click “Next: Permissions” and create a new group, if needed.

    如果需要,請單擊“下一步:權限”并創建一個新組。
  • For the purposes of this tutorial, I’ll create a new group with the “AdministratorAccess” policy. This gives us permission to manage everything in our AWS console programmatically.

    就本教程而言,我將使用“ AdministratorAccess”策略創建一個新組。 這使我們可以通過編程方式管理AWS控制臺中的所有內容。
  • Click “Next: Tags” and add any relevant information. This is optional.

    單擊“下一步:標簽”,然后添加所有相關信息。 這是可選的。
  • Click “Review,” then “Create User.”

    點擊“查看”,然后點擊“創建用戶”。
  • Download your security credentials (the CSV file) and copy it into your project’s root directory. If you’re using source control, be careful.

    下載您的安全憑證(CSV文件),并將其復制到項目的根目錄中。 如果您使用的是源代碼管理,請當心。

閱讀證書 (Reading the Credentials)

  • Create a new file “creds.py” with the following code:

    使用以下代碼創建一個新文件“ creds.py”:
import csv
class Creds:
# credentials
username = “”
access_key_id = “”
secret_key = “”
def __init__(self, creds_file):
with open(creds_file) as file:
reader = csv.reader(file, delimiter=”,”)
header = next(reader)
creds_line = next(reader)
self.username = creds_line[0]
self.access_key_id = creds_line[2]
self.secret_key = creds_line[3]
  • Add from creds import Creds to the top of your main Python file.

    from creds import Creds添加from creds import Creds到主Python文件的頂部。

  • Initialize your Creds object in it: creds = Creds(“credentials.csv”)

    在其中初始化您的Creds對象: creds = Creds(“credentials.csv”)

Great! Now we can use these to access Amazon Web Services.

大! 現在,我們可以使用它們來訪問Amazon Web Services。

調配EC2服務器 (Provisioning an EC2 Server)

Add the following code after your creds variable:

在您的creds變量之后添加以下代碼:

REGION = “us-east-2”
client = boto3.client(
‘ec2’,
aws_access_key_id=creds.access_key_id,
aws_secret_access_key=creds.secret_key,
region_name=REGION
)

Now, let’s provision a new instance of Ubuntu Server 18.04. This is eligible for Amazon’s free tier as well!

現在,讓我們提供一個Ubuntu Server 18.04的新實例。 這也適用于亞馬遜的免費套餐!

At the top of your file, add from botocore.exceptions import ClientError so your program knows how to handle errors.

在文件頂部, 從botocore.exceptions添加import ClientError,以便您的程序知道如何處理錯誤。

Head over to your AWS dashboard and go to EC2->Network & Security-> Key pairs and click “Create key pair.”

轉到您的AWS儀表板,然后轉到EC2->網絡和安全->密鑰對,然后單擊“創建密鑰對”。

Enter a name and hit “Create.” I used “robot” for mine. Though you should avoid hardcoding strings like this, we’ll overlook this, for now, to get it up and running.

輸入名稱,然后點擊“創建”。 我使用“機器人”作為我的機器人。 盡管您應該避免像這樣對字符串進行硬編碼,但現在我們將忽略它以使其啟動并運行。

To run commands on the server and open it to the Web, we have to create a security group and IAM role on AWS. Go to your dashboard.

要在服務器上運行命令并將其打開到Web,我們必須在AWS上創建安全組和IAM角色。 轉到儀表板。

創建一個安全組: (Creating a security group:)

  • Navigate to Network & Security -> Security Groups.

    導航到網絡和安全->安全組。
  • Create a security group, and open ports 22, 80, 443, and 5000. This will allow general access to it from the Web. Allow all IPs to access them.

    創建一個安全組,并打開端口22、80、443和5000。這將允許從Web對其進行常規訪問。 允許所有IP訪問它們。
  • Copy down the group ID of the security group you just created, and paste it into a global variable called SECURITY_GROUP.

    抄下剛剛創建的安全組的組ID,然后將其粘貼到名為SECURITY_GROUP的全局變量中

創建IAM角色: (Creating an IAM role:)

  • Go to your AWS dashboard and navigate to the IAM service.

    轉到您的AWS儀表板并導航到IAM服務。
  • Click on the “Roles” tab.

    點擊“角色”標簽。
  • Click “Create role” and select “EC2.” For the purposes of this tutorial, you’ll want to select “Administrator Access,” but in a real-world setting, this may not be appropriate.

    點擊“創建角色”,然后選擇“ EC2”。 就本教程而言,您將要選擇“ Administrator Access”,但在實際設置中,這可能不合適。
  • Click through the rest of the steps to create a role.

    單擊其余步驟以創建角色。
  • Copy down the name of the IAM role and paste it into a global variable called IAM_PROFILE.

    抄下 IAM角色的名稱,并將其粘貼到名為IAM_PROFILE的全局變量中

  • Add this code to provision a minimal Ubuntu server from Amazon:

    添加以下代碼以從亞馬遜配置最小的Ubuntu服務器:
def provision_server():
# Ubuntu Server 18.04 ID from the AWS panel
image_id = "ami-0f65671a86f061fcd"
# Second smallest instance, free tier eligible.
instance_type = "t2.micro"
# Make this a command-line argument in the future.
keypair_name = "robot"
response = {}
try:
response = ec2.run_instances(ImageId=image_id,
InstanceType=instance_type,
KeyName=keypair_name,
SecurityGroupIds=[SECURITY_GROUP],
IamInstanceProfile={'Name': IAM_PROFILE},
MinCount=1,
MaxCount=1)
print(response['Instances'][0])
print("Provisioning instance…")
# wait for server to be provisioned before returning anything
time.sleep(60)
return str(response['Instances'][0]['InstanceId'])
except ClientError as e:
print(e)

Congratulations! You’re ready to provision your first EC2 server on Amazon. Learn how to configure its network and security settings and deploy a real web app to it in Part 2 when you’re ready to move on.

恭喜你! 您已經準備在Amazon上配置您的第一臺EC2服務器。 當您準備好繼續前進時,將在第2部分中了解如何配置其網絡和安全設置以及如何向其部署真實的Web應用程序。

部署您的應用 (Deploying Your Application)

You made it! Let’s learn how to manage EC2 instances and deploy an application from Github to one.

你做到了! 讓我們學習如何管理EC2實例以及如何從Github部署一個應用程序。

Amazon’Amazon’s SDK supports executing commands on the instance. This is very helpful. It allows us to manage the instance without having to worry about setting up a secure shell and the like.

Amazon的Amazon SDK支持在實例上執行命令。 這非常有幫助。 它使我們能夠管理實例,而不必擔心設置安全的shell等。

  • First, we need to get a list of the instances in your private cloud:

    首先,我們需要獲取私有云中實例的列表:
def get_instance_ids():
instance_id_list = []
instances = ec2.describe_instances()
instances = instances[‘Reservations’][0][‘Instances’]
for instance in instances:
instance_id_list.append(instance[‘InstanceId’])
return instance_id_list
  • Add this code to be able to execute commands on your server’s terminal:

    添加以下代碼以能夠在服務器的終端上執行命令:
def send_command_aws(commands=[“echo hello”], instance=”i-06cca6072e593a0ac”):
ssm_client = boto3.client(‘ssm’,
aws_access_key_id=creds.access_key_id,
aws_secret_access_key=creds.secret_key,
region_name=REGION)
response = ssm_client.send_command(
InstanceIds=[instance],
DocumentName=”AWS-RunShellScript”,
Parameters={‘commands’: commands}, )
command_id = response[‘Command’][‘CommandId’]
time.sleep(5)
output = ssm_client.get_command_invocation(
CommandId=command_id,
InstanceId=instance,
)
print(output)
  • Finally, we need to generate commands to install dependencies and deploy a Flask webapp from Github on the live server:

    最后,我們需要生成命令來安裝依賴項并在實時服務器上從Github部署Flask Web應用程序:
def generate_git_commands(git_url=GIT_URL, start_command=”sudo python3 hellopython/app.py”, pip3_packages=[], additional_commands=[]):
commands = []
if “.git” in git_url:
git_url = git_url[:-4]
repo_name = git_url[git_url.rfind(‘/’):]
# install dependencies
commands.append(“sudo apt-get update”)
commands.append(“sudo apt-get install -y git”)
commands.append(“sudo apt-get install -y python3”)
commands.append(“sudo apt-get install -y python3-pip”)
commands.append(“sudo rm -R hellopython”)
commands.append(“pip3 — version”)
commands.append(“sudo git clone “ + git_url)
# commands.append(“cd “ + repo_name)
# install python dependencies
for dependency in pip3_packages:
commands.append(“sudo pip3 install “ + dependency)
# run any additional custom commands
for command in additional_commands:
commands.append(command)
# start program execution
commands.append(start_command)
return commands
  • Add these constants to the top of your program:

    將這些常量添加到程序的頂部:
GIT_URL = "https://github.com/hsauers5/hellopython"REGION = "us-east-2"SECURITY_GROUP = "sg-0c7a3bfa35c85f8ce"IAM_PROFILE = "Python-Tutorial"
  • Now, add this line to the bottom of your program:

    現在,將此行添加到程序的底部:
send_command_aws(commands=generate_git_commands(GIT_URL, pip3_packages=["flask"]), instance=provision_server())
  • Run your code! python3 app.py

    運行您的代碼! python3 app.py

  • Head over to your EC2 panel, and copy the machine’s public DNS. Add “:5000” to it and navigate to it in your browser.

    轉到您的EC2面板,然后復制計算機的公共DNS。 在其中添加“:5000”,然后在瀏覽器中導航到它。

Congratulations! You just completed your first automated deployment using Amazon’s Boto3 SDK.

恭喜你! 您剛剛使用Amazon的Boto3 SDK完成了第一次自動部署。

You can view or download the complete repository here: https://github.com/hsauers5/AWS-Deployment

您可以在此處查看或下載完整的存儲庫: https : //github.com/hsauers5/AWS-Deployment

翻譯自: https://www.freecodecamp.org/news/automated-deployment-in-aws-5aadc2e708a9/

aws中部署防火墻

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391907.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391907.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391907.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Runtime的應用

來自&#xff1a;http://www.imlifengfeng.com/blog/?p397 1、快速歸檔 (id)initWithCoder:(NSCoder *)aDecoder { if (self [super init]) { unsigned int outCount; Ivar * ivars class_copyIvarList([self class], &outCount); for (int i 0; i < outCount; i ) …

使用 VisualVM 進行性能分析及調優

https://www.ibm.com/developerworks/cn/java/j-lo-visualvm/轉載于:https://www.cnblogs.com/adolfmc/p/7238893.html

spring—事務控制

編程式事務控制相關對象 PlatformTransactionManager PlatformTransactionManager 接口是 spring 的事務管理器&#xff0c;它里面提供了我們常用的操作事務的方法。注意&#xff1a; PlatformTransactionManager 是接口類型&#xff0c;不同的 Dao 層技術則有不同的實現類 …

為什么印度盛產碼農_印度農產品價格的時間序列分析

為什么印度盛產碼農Agriculture is at the center of Indian economy and any major change in the sector leads to a multiplier effect on the entire economy. With around 17% contribution to the Gross Domestic Product (GDP), it provides employment to more than 50…

SAP NetWeaver

SAP的新一代企業級服務架構——NetWeaver    SAP NetWeaver是下一代基于服務的平臺&#xff0c;它將作為未來所有SAP應用程序的基礎。NetWeaver包含了一個門戶框架&#xff0c;商業智能和報表&#xff0c;商業流程管理&#xff08;BPM&#xff09;&#xff0c;自主數據管理&a…

NotifyMyFrontEnd 函數背后的數據緩沖區(一)

async.c的 static void NotifyMyFrontEnd(const char *channel, const char *payload, int32 srcPid) 函數中的主要邏輯是這樣的&#xff1a;復制代碼if (whereToSendOutput DestRemote) { StringInfoData buf; pq_beginmessage(&buf, A); //cursor 為 A pq…

最后期限 軟件工程_如何在軟件開發的最后期限內實現和平

最后期限 軟件工程D E A D L I N E…最后期限… As a developer, this is one of your biggest nightmares or should I say your enemy? Name it whatever you want.作為開發人員&#xff0c;這是您最大的噩夢之一&#xff0c;還是我應該說您的敵人&#xff1f; 隨便命名。 …

SQL Server的復合索引學習【轉載】

概要什么是單一索引,什么又是復合索引呢? 何時新建復合索引&#xff0c;復合索引又需要注意些什么呢&#xff1f;本篇文章主要是對網上一些討論的總結。一.概念單一索引是指索引列為一列的情況,即新建索引的語句只實施在一列上。用戶可以在多個列上建立索引&#xff0c;這種索…

leetcode 1423. 可獲得的最大點數(滑動窗口)

幾張卡牌 排成一行&#xff0c;每張卡牌都有一個對應的點數。點數由整數數組 cardPoints 給出。 每次行動&#xff0c;你可以從行的開頭或者末尾拿一張卡牌&#xff0c;最終你必須正好拿 k 張卡牌。 你的點數就是你拿到手中的所有卡牌的點數之和。 給你一個整數數組 cardPoi…

pandas處理excel文件和csv文件

一、csv文件 csv以純文本形式存儲表格數據 pd.read_csv(文件名)&#xff0c;可添加參數enginepython,encodinggbk 一般來說&#xff0c;windows系統的默認編碼為gbk&#xff0c;可在cmd窗口通過chcp查看活動頁代碼&#xff0c;936即代表gb2312。 例如我的電腦默認編碼時gb2312&…

tukey檢測_回到數據分析的未來:Tukey真空度的整潔實現

tukey檢測One of John Tukey’s landmark papers, “The Future of Data Analysis”, contains a set of analytical techniques that have gone largely unnoticed, as if they’re hiding in plain sight.John Tukey的標志性論文之一&#xff0c;“ 數據分析的未來 ”&#x…

spring— Spring與Web環境集成

ApplicationContext應用上下文獲取方式 應用上下文對象是通過new ClasspathXmlApplicationContext(spring配置文件) 方式獲取的&#xff0c;但是每次從容器中獲 得Bean時都要編寫new ClasspathXmlApplicationContext(spring配置文件) &#xff0c;這樣的弊端是配置文件加載多次…

Elasticsearch集群知識筆記

Elasticsearch集群知識筆記 Elasticsearch內部提供了一個rest接口用于查看集群內部的健康狀況&#xff1a; curl -XGET http://localhost:9200/_cluster/healthresponse結果&#xff1a; {"cluster_name": "format-es","status": "green&qu…

Item 14 In public classes, use accessor methods, not public fields

在public類中使用訪問方法&#xff0c;而非公有域 這標題看起來真晦澀。。解釋一下就是&#xff0c;如果類變成public的了--->那就使用getter和setter&#xff0c;不要用public成員。 要注意它的前提&#xff0c;如果是private的class&#xff08;內部類..&#xff09;或者p…

子集和與一個整數相等算法_背包問題的一個變體:如何解決Java中的分區相等子集和問題...

子集和與一個整數相等算法by Fabian Terh由Fabian Terh Previously, I wrote about solving the Knapsack Problem (KP) with dynamic programming. You can read about it here.之前&#xff0c;我寫過有關使用動態編程解決背包問題(KP)的文章。 你可以在這里閱讀 。 Today …

matplotlib圖表介紹

Matplotlib 是一個python 的繪圖庫&#xff0c;主要用于生成2D圖表。 常用到的是matplotlib中的pyplot&#xff0c;導入方式import matplotlib.pyplot as plt 一、顯示圖表的模式 1.plt.show() 該方式每次都需要手動show()才能顯示圖表&#xff0c;由于pycharm不支持魔法函數&a…

到2025年將保持不變的熱門流行技術

重點 (Top highlight)I spent a good amount of time interviewing SMEs, data scientists, business analysts, leads & their customers, programmers, data enthusiasts and experts from various domains across the globe to identify & put together a list that…

spring—SpringMVC的請求和響應

SpringMVC的數據響應-數據響應方式 頁面跳轉 直接返回字符串 RequestMapping(value {"/qq"},method {RequestMethod.GET},params {"name"})public String method(){System.out.println("controller");return "success";}<bea…

Maven+eclipse快速入門

1.eclipse下載 在無外網情況下&#xff0c;無法通過eclipse自帶的help-install new software輸入url來獲取maven插件&#xff0c;因此可以用集成了maven插件的免安裝eclipse(百度一下有很多)。 2.jdk下載以及環境變量配置 JDK是向前兼容的&#xff0c;可在Eclipse上選擇編譯器版…

源碼閱讀中的收獲

最近在做短視頻相關的模塊&#xff0c;于是在看 GPUImage 的源碼。其實有一定了解的伙伴一定知道 GPUImage 是通過 addTarget 鏈條的形式添加每一個環節。在對于這樣的設計贊嘆之余&#xff0c;想到了實際開發場景下可以用到的場景&#xff0c;借此分享。 我們的項目中應該有很…