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.mzph.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;借此分享。 我们的项目中应该有很…