aws创建几个vpc_如何使用AWS CDK创建VPC

吴欣然
2023-12-01

aws创建几个vpc

AWS CDK is a software development framework that allows you to define your cloud infrastructure in code and provision it though Cloudformation.

一个 WS CDK是一个软件开发框架,可以让你在代码和提供它虽然Cloudformation定义你的云计算基础架构。

At moment it supports the following languages:

目前,它支持以下语言:

  • TypeScript

    打字稿
  • JavaScript

    JavaScript
  • Python

    Python
  • Java

    Java
  • C#

    C#

Through CDK you can build resources following AWS best practices with less code.

通过CDK,您可以按照AWS最佳实践以更少的代码构建资源。

This doesn’t want to be a workshop on CDK, just an introduction to show its potentiality. If you like it and you want to learn more, here you can find a workshop to help you to get started.

这并不想成为CDK的研讨会,而只是介绍它的潜力。 如果您喜欢它并且想了解更多信息,可以在这里找到一个工作坊来帮助您入门。

For this post I am using Cloud9 as my IDE.

对于本文,我将Cloud9用作我的IDE。

Follow this link to setup an environment for a single user.

单击此链接可以为单个用户设置环境。

Cloud9 allows you to get an environment ready quickly without going through all the “pain” of installing the basic requirements.

Cloud9使您可以快速准备好环境,而无需经历安装基本需求的所有“麻烦”。

Now, once my IDE is up and ready I can create my project folder and cd into it

现在,一旦我的IDE启动并准备就绪,我就可以创建我的项目文件夹并cd到其中

mkdir vpc-cdk && cd $_

You can now initialise the cdk app running the command:

现在,您可以运行以下命令来初始化cdk应用程序:

cdk init <app-template> --language python

To get the list of supported app templates and supported languages:

要获取支持的应用程序模板和支持的语言的列表,请执行以下操作:

$ cdk init --list Available templates:* └─ cdk init app --language=[csharp|fsharp|java|javascript|python|typescript]* └─ cdk init lib --language=typescript*app: Template for a CDK Applicationlib: Template for a CDK Construct Librarysample-app: Example CDK Application with some constructs
└─ cdk init sample-app --language=[csharp|fsharp|java|javascript|python|typescript]

This diagram shows you the workflow cdk goes through when you run cdk deploy

此图显示了运行cdk deploy时cdk经历的工作流程

For our purpose we are going to use app

为了我们的目的,我们将使用应用程序

$ cdk init app --language python
Applying project template app for python
Executing Creating virtualenv...
# Welcome to your CDK Python project!
This is a blank project for Python development with CDK.
The `cdk.json` file tells the CDK Toolkit how to execute your app.
This project is set up like a standard Python project. The initialization
process also creates a virtualenv within this project, stored under the .env
directory. To create the virtualenv it assumes that there is a `python3`
(or `python` for Windows) executable in your path with access to the `venv`
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.
To manually create a virtualenv on MacOS and Linux:
```
$ python3 -m venv .env
```
After the init process completes and the virtualenv is created, you can use the following
step to activate your virtualenv.
```
$ source .env/bin/activate
```
If you are a Windows platform, you would activate the virtualenv like this:
```
% .env\Scripts\activate.bat
```
Once the virtualenv is activated, you can install the required dependencies.
```
$ pip install -r requirements.txt
```
At this point you can now synthesize the CloudFormation template for this code.
```
$ cdk synth
```
To add additional dependencies, for example other CDK libraries, just add
them to your `setup.py` file and rerun the `pip install -r requirements.txt`
command.
## Useful commands
* `cdk ls` list all stacks in the app
* `cdk synth` emits the synthesized CloudFormation template
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk docs` open CDK documentation
Enjoy!

As you can see cdk has created a bunch of files and shows the README that gives some details about these files and how to use the project.

如您所见,cdk已经创建了许多文件,并显示了自述文件,其中提供了有关这些文件以及如何使用项目的一些详细信息。

Now let’s follow along and activate our virtual env, install requirements and synthesize the Cloudformation template for this code.

现在,让我们继续并激活我们的虚拟环境,安装要求并为该代码合成Cloudformation模板。

$ source .env/bin/activate$ pip install -r requirements.txt

If we want to add the code to create our VPC we need to modify the stack file created under our project folder, in our case vpc_cdk/vpc_cdk_stack.py

如果要添加代码以创建VPC,则需要修改在项目文件夹下创建的堆栈文件,在本例中为vpc_cdk / vpc_cdk_stack.py

We need to add the VPC construct.

我们需要添加VPC构造。

Here you can find the CDK construct library

在这里 您可以找到CDK构造库

Now you just need to import the aws_ec2 module from aws_cdk

现在,您只需要从aws_cdk导入aws_ec2模块

from aws_cdk import (
core,
aws_ec2 as ec2)

Now before deploy it you can run cdk diff and it will show all resources that will be created, or cdk synth to synthesize the Cloudformation template.

现在,在部署它之前,您可以运行cdk diff ,它将显示将创建的所有资源,或者显示cdk synth来合成Cloudformation模板。

As you can see with just few lines of code it will create VPC, Subnets (Public/Private), IGW, NatGW, Routing tables, everything you need.

如您所见,只需几行代码,它将创建VPC,子网(公共/专用),IGW,NatGW,路由表,以及您需要的所有内容。

If you run in this problem

如果您遇到这个问题

$ cdk diff
Traceback (most recent call last):
File “app.py”, line 5, in <module>
from vpc_cdk.vpc_cdk_stack import VpcCdkStack
File “/home/ec2-user/environment/vpc-cdk/vpc_cdk/vpc_cdk_stack.py”, line 1, in <module>
from aws_cdk import (ImportError: cannot import name 'aws_ec2'

It is probably because the module is not installed by pip. To fix it add the module into setup.py and re-run pip install -r requirements.txt

可能是因为pip未安装该模块。 要修复它,请将模块添加到setup.py中,然后重新运行pip install -r requirements.txt

You can now deploy your app

您现在可以部署您的应用程序

$ cdk deploy

If you go to Cloudformation console you should see your Stack created and when you are done, if you don’t want to incur in any cost, you can destroy it with this simple command

如果您转到Cloudformation控制台,则应该看到已创建堆栈,完成后,如果您不想承担任何费用,则可以使用此简单命令将其销毁

$ cdk destroy

CDK is very powerful tool and allows you to write Infrastructure as Code using AWS best practices, no more long Cloudformation templates and you can even write unit test.

CDK是功能非常强大的工具,它允许您使用AWS最佳实践将基础设施编写为代码,而无需再使用冗长的Cloudformation模板,甚至可以编写单元测试。

This post wasn’t a real tutorial but I hope it lighted that bit of curiosity about CDK and explore more.

这篇文章不是真正的教程,但我希望它能激发人们对CDK的好奇心并进一步探索。

For now it’s all folks, cheers!

现在,所有人都欢呼!

You can find the code in my git repository on Github.

您可以在Github上的git仓库中找到代码。

翻译自: https://medium.com/@sgrilux/how-to-create-a-vpc-with-aws-cdk-da910c1516bb

aws创建几个vpc

 类似资料: