

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 AWS CloudFormation 自定义资源和 Amazon SNS 创建 Infoblox 对象
<a name="create-infoblox-objects-using-aws-cloudformation-custom-resources-and-amazon-sns"></a>

*Tim Sutton，Amazon Web Services*

## Summary
<a name="create-infoblox-objects-using-aws-cloudformation-custom-resources-and-amazon-sns-summary"></a>

**注意**： AWS Cloud9 不再向新客户开放。的现有客户 AWS Cloud9 可以继续照常使用该服务。[了解详情](https://aws.amazon.com/blogs/devops/how-to-migrate-from-aws-cloud9-to-aws-ide-toolkits-or-aws-cloudshell/)

Infoblox 域名系统（DNS）、动态主机配置协议（DHCP）和 IP 地址管理（[Infoblox DDI](https://www.infoblox.com/products/ddi/)）使您能够集中并高效控制复杂的混合环境。借助 Infoblox DDI，除了使用相同的设备管理本地和 Amazon Web Services （AWS） 云上的 DNS 外，您还可以在一个权威 IP 地址管理器（IPAM）数据库中发现和记录所有网络资产。

此模式描述了如何使用 AWS CloudFormation 自定义资源通过调用 Infoblox WAPI 来创建 Infoblox 对象（例如 DNS 记录或 IPAM 对象）。有关 Infoblox WAPI 的详细信息，请参阅 Infoblox 文档中的 [WAPI 文档](https://www.infoblox.com/wp-content/uploads/infoblox-deployment-infoblox-rest-api.pdf)。

通过使用此模式的方法，除了删除创建记录和预置网络的手动流程外，您还可以获得 AWS 和本地环境的 DNS 记录和 IPAM 配置的统一视图。您可以将此模式的方法用于以下用例：
+ 在创建亚马逊弹性计算云 (Amazon EC2) 实例后添加 A 记录 
+ 创建应用程序负载均衡器后添加 CNAME 记录
+ 创建虚拟私有云（VPC）后添加网络对象
+ 提供下一个网络范围并使用该范围创建子网

您还可以扩展此模式并使用其他 Infoblox 设备功能，例如添加不同的 DNS 记录类型或配置 Infoblox vDiscovery。 

该模式使用的 hub-and-spoke设计是，中心需要连接到 AWS 云上或本地的 Infoblox 设备，并使用 AWS Lambda 调用 Infoblox API。分支位于 AWS Organizations 中同一组织中的相同或不同账户中，并使用 AWS CloudFormation 自定义资源调用 Lambda 函数。

## 先决条件和限制
<a name="create-infoblox-objects-using-aws-cloudformation-custom-resources-and-amazon-sns-prereqs"></a>

**先决条件**
+ 现有的 Infoblox 设备或网格，安装在 AWS Cloud和/或本地，并配置了可以管理 IPAM 和 DNS 操作的管理员用户。有关此内容的详细信息，请参阅 Infoblox 文档中的[关于管理员账户](https://docs.infoblox.com/display/nios86/About+Admin+Accounts)。 
+ 要在 Infoblox 设备上添加记录的现有 DNS 权威区域。有关此内容的更多信息，请参阅 [Infoblox 文档中的配置权威区域](https://docs.infoblox.com/display/nios86/Configuring+Authoritative+Zones)。 
+ AWS Organizations 中的两个活动 Amazon Web Services account。一个账户是中心账户，另一个账户是分支账户。
+ 中心账户和分支账户必须位于同一个 AWS 区域。 
+ 中心账户的 VPC 必须连接到 Infoblox 设备；例如，通过使用 AWS Transit Gateway 或 VPC 对等连接。
+ [AWS 无服务器应用程序模型 (AWS SAM) Model，在本地安装并使用 AWS](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) Cloud9 或 AWS 进行配置。 CloudShell
+ `Infoblox-Hub.zip` 和 `ClientTest.yaml` 文件（附加），下载到包含 AWS SAM 的本地环境。

**限制**
+ AWS CloudFormation 自定义资源的服务令牌必须来自创建堆栈的同一区域。我们建议您在每个区域中使用中心账户，而不是在一个区域中创建 Amazon Simple Notification Service（Amazon SNS）主题，然后在另一个区域调用 Lambda 函数。

**产品版本**
+ Infoblox WAPI 版本 2.7

## 架构
<a name="create-infoblox-objects-using-aws-cloudformation-custom-resources-and-amazon-sns-architecture"></a>

下图演示了此模式的工作流程。 

![\[使用 AWS CloudFormation 自定义资源和 Amazon SNS 创建 Infoblox 对象。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/8d609d3f-6f5e-4084-849f-ca191db8055e/images/3594a064-e103-4211-84b7-da67c41ebb15.png)


该图显示了此模式解决方案的以下组件：

1. AWS CloudFormation 自定义资源允许您在创建、更新或删除堆栈时 AWS CloudFormation 运行的模板中编写自定义配置逻辑。当您创建堆栈时，AWS CloudFormation 会向由 EC2 实例上运行的应用程序监控的 SNS 主题发送`create`请求。

1. 来自 AWS CloudFormation 自定义资源的 Amazon SNS 通知通过特定的 AWS 密钥管理服务 (AWS KMS) 密钥进行加密，并且仅限组织中的账户访问 Organizations。SNS 主题启动调用 Infoblox WAPI API 的 Lambda 资源。

1. Amazon SNS 调用以下 Lambda 函数，这些函数将 Infoblox WAPI 网址、用户名和密码 AWS Secrets Manager 亚马逊资源名称 () 作为环境变量：ARNs 
   + `dnsapi.lambda_handler`— 从 AWS CloudFormation 自定义资源中接收`DNSName``DNSType`、和`DNSValue`值，并使用这些值创建 DNS A 记录和别名记录。
   + `ipaddr.lambda_handler`— 从 AWS CloudFormation 自定义资源接收`VPCCIDR``Type``SubnetPrefix`、、和`Network Name`值，并使用这些值将网络数据添加到 Infoblox IPAM 数据库中，或者为自定义资源提供下一个可用于创建新子网的可用网络。
   + `describeprefixes.lambda_handler` – 使用 `"com.amazonaws."+Region+".s3"` 筛选条件调用 `describe_managed_prefix_lists` AWS API 以检索所需 `prefix ID`。
**重要**  
这些 Lambda 函数是用 Python 编写的，彼此相似，但调用方式不同。 APIs

1. 您可以将 Infoblox 网格部署为物理、虚拟或基于云的网络设备。 它可以在本地部署，也可以使用一系列虚拟机管理程序作为虚拟设备部署，包括 VMware ESXi微软 Hyper-V、Linux KVM 和 Xen。您还可以使用亚马逊机器映像（AMI）在 Amazon Web Services Cloud 上部署 Infoblox 网格。

1. 该图显示了 Infoblox 网格的混合解决方案，该解决方案为 Amazon Web Services Cloud 和本地资源提供 DNS 和 IPAM。

**技术堆栈**
+ AWS CloudFormation
+ IAM
+ AWS KMS
+ AWS Lambda
+ AWS SAM
+ AWS Secrets Manager
+ Amazon SNS
+ Amazon VPC 

## 工具
<a name="create-infoblox-objects-using-aws-cloudformation-custom-resources-and-amazon-sns-tools"></a>
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可帮助您设置 AWS 资源，快速一致地配置这些资源，并在 AWS 账户和区域的整个生命周期中对其进行管理。
+ [AWS Identity and Access Management (AWS IAM) ](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)通过控制验证和授权使用您 AWS 资源的用户，帮助您安全地管理对您 AWS 资源的访问。
+ [AWS Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 可帮助您创建和控制加密密钥，以帮助保护您的数据。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，而无需预置或管理服务器。它仅在需要时运行您的代码，并且能自动扩缩，因此您只需为使用的计算时间付费。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一项账户管理服务，使您可将多个 AWS 账户整合到您所创建的组织中并进行集中管理。
+ [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) 帮助您将代码中的硬编码凭证（包括密码）替换为对 Secrets Manager 的 API 调用，以便以编程方式检索密钥。
+ [AWS Serverless Application Model（AWS SAM）](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html)是一个开源框架，帮助您在 Amazon Web Services Cloud 中构建无服务器应用程序。
+ [Amazon Simple Notiﬁcation Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 可帮助您协调和管理发布者与客户端（包括 Web 服务器和电子邮件地址）之间的消息交换。
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 可帮助您将 AWS 资源启动到您定义的虚拟网络中。此虚拟网络类似于您在自己的数据中心内运行的传统网络，具有使用 AWS 可扩展基础设施的优势。

**代码**

您可以使用`ClientTest.yaml`示例 AWS CloudFormation 模板（附后）来测试 Infoblox 中心。您可以自定义 AWS CloudFormation 模板以包含下表中的自定义资源。


|  | 
| --- |
| 使用 Infoblox 分支自定义资源创建 A 记录 | 返回值： `infobloxref ` – Infoblox 参考资料示例资源：

```
ARECORDCustomResource:

  Type: "Custom::InfobloxAPI"

  Properties:

    ServiceToken: !Sub  arn:aws:sns:${AWS::Region}:${HubAccountID}:RunInfobloxDNSFunction

    DNSName: 'arecordtest.company.com'

    DNSType: 'ARecord' 

    DNSValue: '10.0.0.1'
``` | 
| --- |--- |
| 使用 Infoblox 分支自定义资源创建 CNAME 记录 | **返回值**： `infobloxref ` – Infoblox 参考资料**示例资源**：<pre>CNAMECustomResource:<br /><br />  Type: "Custom::InfobloxAPI"<br /><br />  Properties:<br /><br />    ServiceToken: !Sub arn:aws:sns:${AWS::Region}:${HubAccountID}:RunInfoblox    <br /><br />    DNSFunction<br /><br />    DNSName: 'cnametest.company.com'<br /><br />    DNSType: 'cname' <br /><br />    DNSValue: 'aws.amazon.com'</pre> | 
| 使用 Infoblox 分支自定义资源创建网络对象 | **返回值：**`infobloxref ` – Infoblox 参考资料`network` – 网络范围（与 `VPCCIDR` 相同）**示例资源：**<pre>VPCCustomResource:<br /><br />  Type: 'Custom::InfobloxAPI'<br /><br />  Properties:<br /><br />    ServiceToken: !Sub  arn:aws:sns:${AWS::Region}:${HubAccountID}:RunInfobloxNextSubnetFunction<br /><br />    VPCCIDR: !Ref VpcCIDR<br /><br />    Type: VPC<br /><br />    NetworkName: My-VPC</pre> | 
| 使用 Infoblox 分支自定义资源检索下一个可用子网 | **返回值：**`infobloxref` – Infoblox 参考资料`network ` – 子网的网络范围**示例资源：**<pre>Subnet1CustomResource:<br /><br />  Type: 'Custom::InfobloxAPI'<br /><br />  DependsOn: VPCCustomResource<br /><br />  Properties:<br /><br />    ServiceToken: !Sub  arn:aws:sns:${AWS::Region}:${HubAccountID}:RunInfobloxNextSubnetFunction<br /><br />    VPCCIDR: !Ref VpcCIDR<br /><br />    Type: Subnet<br /><br />    SubnetPrefix: !Ref SubnetPrefix<br /><br />NetworkName: My-Subnet</pre> | 

## 操作说明
<a name="create-infoblox-objects-using-aws-cloudformation-custom-resources-and-amazon-sns-epics"></a>

### 创建和配置中心账户的 VPC
<a name="create-and-configure-the-hub-accountrsquor-s-vpc"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建与 Infoblox 设备连接的 VPC。 | 登录您的中心账户的 AWS 管理控制台，并按照 AWS Quick Start 中的 [Amazon Web Services Cloud 上的 Amazon VPC 快速入门参考部署](https://aws-quickstart.github.io/quickstart-aws-vpc/)中的步骤创建 VPC。VPC 必须具有与 Infoblox 设备的 HTTPS 连接，我们建议您使用私有子网进行此连接。 | 网络管理员、系统管理员 | 
| （可选）为私有子网创建 VPC 端点。 | VPC 端点为您的私有子网提供与公共服务的连接。需要以下端点：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/create-infoblox-objects-using-aws-cloudformation-custom-resources-and-amazon-sns.html)有关为私有子网创建端点的更多信息，请参阅 [Amazon VPC 文档中的 VPC 端点](https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints.html)。 | 网络管理员、系统管理员 | 

### 部署 Infoblox 中心
<a name="deploy-the-infoblox-hub"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 构建 AWS SAM 模板。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/create-infoblox-objects-using-aws-cloudformation-custom-resources-and-amazon-sns.html) | 开发人员、系统管理员 | 
| 部署 AWS SAM 模板。 | 该`sam deploy`命令获取所需的参数并将其保存到`samconfig.toml`文件中，将 AWS CloudFormation 模板和 Lambda 函数存储在 S3 存储桶中，然后将 AWS CloudFormation 模板部署到您的中心账户。 以下示例代码演示如何部署 AWS SAM 模板：<pre>$ sam deploy --guided<br /><br />Configuring SAM deploy<br />======================<br />        Looking for config file [samconfig.toml] :  Found<br />        Reading default arguments  :  Success<br />        Setting default arguments for 'sam deploy'<br />        =========================================<br />        Stack Name [Infoblox-Hub]:<br />        AWS Region [eu-west-1]:<br />        Parameter InfobloxUsername:<br />        Parameter InfobloxPassword:<br />        Parameter InfobloxIPAddress [xxx.xxx.xx.xxx]:<br />        Parameter AWSOrganisationID [o-xxxxxxxxx]:<br />        Parameter VPCID [vpc-xxxxxxxxx]:<br />        Parameter VPCCIDR [xxx.xxx.xxx.xxx/16]:<br />        Parameter VPCSubnetID1 [subnet-xxx]:<br />        Parameter VPCSubnetID2 [subnet-xxx]:<br />        Parameter VPCSubnetID3 [subnet-xxx]:<br />        Parameter VPCSubnetID4 []: <br />        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy<br />        Confirm changes before deploy [Y/n]: y<br />        #SAM needs permission to be able to create roles to connect to the resources in your template<br />Allow SAM CLI IAM role creation [Y/n]: n<br />Capabilities [['CAPABILITY_NAMED_IAM']]:<br />        Save arguments to configuration file [Y/n]: y<br />        SAM configuration file [samconfig.toml]:<br />        SAM configuration environment [default]: </pre>每次都必须使用 `--guided` 选项，因为 Infoblox 登录凭证未存储在 `samconfig.toml` 文件中。 | 开发人员、系统管理员 | 

## 相关资源
<a name="create-infoblox-objects-using-aws-cloudformation-custom-resources-and-amazon-sns-resources"></a>
+ [开始 WAPIs 使用 Postman](https://blogs.infoblox.com/community/getting-started-with-wapis-using-postman/)（Infoblox 博客）
+ [使用 BYOL 模型为 AWS 预置 vNIOS](https://docs.infoblox.com/display/NAIG/Provisioning+vNIOS+for+AWS+Using+the+BYOL+Model)（Infoblox 文档）
+ [quickstart-aws-vpc](https://github.com/aws-quickstart/quickstart-aws-vpc)（GitHub 存储库）
+ [describe\$1managed\$1prefix\$1lists](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_managed_prefix_lists)（适用于 Python 的 AWS SDK 文档）

## 附件
<a name="attachments-8d609d3f-6f5e-4084-849f-ca191db8055e"></a>

要访问与此文档相关联的其他内容，请解压以下文件：[attachment.zip](samples/p-attach/8d609d3f-6f5e-4084-849f-ca191db8055e/attachments/attachment.zip)