Skip to main content

现代 CloudFormation Python 工具,具有真正目标编程风格的类型提示、参数提示、文档提示 + 自动验证。

项目描述

https://github.com/MacHu-GWU/cottonformation-project/workflows/CI/badge.svg https://codecov.io/gh/MacHu-GWU/cottonformation-project/branch/main/graph/badge.svg https://readthedocs.org/projects/cottonformation/badge/ https://img.shields.io/pypi/v/cottonformation.svg https://img.shields.io/pypi/l/cottonformation.svg https://img.shields.io/pypi/pyversions/cottonformation.svg https://badges.gitter.im/cottonformation/community.svg https://img.shields.io/badge/STAR_Me_on_GitHub!--None.svg?style=social
https://img.shields.io/badge/Link-Document-blue.svg https://img.shields.io/badge/Link-API-blue.svg https://img.shields.io/badge/Link-Source_Code-blue.svg https://img.shields.io/badge/Link-Install-blue.svg https://img.shields.io/badge/Link-GitHub-blue.svg https://img.shields.io/badge/Link-Submit_Issue-blue.svg https://img.shields.io/badge/Link-Request_Feature-blue.svg https://img.shields.io/badge/Link-Download-blue.svg

欢迎来到棉花形成文档

完整的文档在这里

Cottonformation是一个 Python 工具,提供最佳的开发体验和最高的生产力。由现代类型提示、高级自动完成、参数提示、即时官方 AWS 文档查找提供支持这是它在PyCharm中的样子(开箱即用的 PyCharm 支持,但您可以通过在 VSCode、Sublime、Atom 中安装扩展程序或插件来配置它)。

  1. 类型提示和属性提示:告诉您哪些属性可用,类型是什么,并自动为您完成代码。rp前缀代表Required Propertyp prefix 代表Optional Property,您可以使用 UP 和 DOWN 键导航属性。您还可以查看可用的属性和类型 (Cmd + P),请参阅查看参数信息

https://user-images.githubusercontent.com/6800411/123467578-a691af00-d5be-11eb-8869-83c0db3253b5.gif
  1. 返回值提示:告诉您可用的返回值,并让您无需查找 AWS 文档即可立即访问它。您无需记住任何 API。老实说,在https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html中查找确切的资源属性名称,然后GetAtt(resource_object, attribute_name)是真的很丑!

https://user-images.githubusercontent.com/6800411/123468458-ca092980-d5bf-11eb-906b-793c148dc3c0.gif
  1. 参数提示:告诉你所有可用的参数及其类型(Cmd + P)。它适用于资源类型和复杂的属性类型。

https://user-images.githubusercontent.com/6800411/123477456-19eded80-d5cc-11eb-84a6-7c864e39a142.gif
  1. 跳转到资源文档:立即在 Web 浏览器中打开有关此 AWS 资源的 AWS 文档。单击类,按 F1 并单击链接。

https://user-images.githubusercontent.com/6800411/123477362-f0cd5d00-d5cb-11eb-9d09-32cfb1d96710.gif
  1. 跳转到属性文档方法 1:立即在 Web 浏览器中打开有关此资源属性的 AWS 文档。单击类,按 F1(查看文档字符串),找到属性并单击链接。

https://user-images.githubusercontent.com/6800411/123478114-ff684400-d5cc-11eb-8a62-f168d3ae4ed3.gif
  1. 跳转到属性文档方法2 :使用resource_object.property_name语法引用资源属性,单击属性,按F1(查看文档字符串),然后单击链接。

https://user-images.githubusercontent.com/6800411/123478125-042cf800-d5cd-11eb-8fc9-80cb5507845b.gif
  1. 跳转到返回值文档:使用resource_object.attribute_name语法引用资源属性,单击属性,按 F1(查看文档字符串),然后单击链接。

https://user-images.githubusercontent.com/6800411/123478144-0bec9c80-d5cd-11eb-8260-a9de04690177.gif

作为代码脚本的示例基础架构可能如下所示。您可以立即从 Python 部署它

# -*- coding: utf-8 -*-

# First, import cottonformation, I prefer to use ctf for a short name
import cottonformation as cf

# import the aws service module you need
from cottonformation.res import iam, awslambda

# create a ``Template`` object to represent your cloudformation template
tpl = cf.Template()

# create a ``Parameter`` object, and add it to template.
param_env_name = cf.Parameter(
    "EnvName",
    Type=cf.Parameter.TypeEnum.String,
)
tpl.add(param_env_name)

# create a ``Resource`` object for aws iam role
iam_role_for_lambda = iam.Role(
    "IamRoleForLambdaExecution",
    # you don't need to remember the exact name or syntax for
    # trusted entity / assume role policy, cottonformation has a helper for this
    rp_AssumeRolePolicyDocument=cf.helpers.iam.AssumeRolePolicyBuilder(
        cf.helpers.iam.ServicePrincipal.awslambda()
    ).build(),
    p_RoleName=cf.Sub("${EnvName}-iam-role-for-lambda", dict(EnvName=param_env_name.ref())),
    p_Description="Minimal iam role for lambda execution",
    # you don't need to remember the exact ARN for aws managed policy.
    # cottonformation has a helper for this
    p_ManagedPolicyArns=[
        cf.helpers.iam.AwsManagedPolicy.AWSLambdaBasicExecutionRole,
    ]
)
tpl.add(iam_role_for_lambda)


# create a ``Resource`` object for aws lambda function
lbd_source_code = """
def handler(event, context):
    return "hello cottonformation"
""".strip()

lbd_func = awslambda.Function(
    "LbdFuncHelloWorld",
    # rp_ stands for Required Property, it will gives you parameter-hint
    # for all valid required properties.
    rp_Code=awslambda.FunctionCode(
        p_ZipFile=lbd_source_code,
    ),
    # normally we need to explicitly call GetAtt(resource, attribute)
    # and you need to remember the exact attribute name
    # but cottonformation allow you to instantly reference the attribute
    # powered by auto-complete. the prefix rv_ stands for Return Value
    rp_Role=iam_role_for_lambda.rv_Arn,
    # p_ stands for Property, it will gives you parameter-hint
    # for all valid properties
    p_MemorySize=256,
    p_Timeout=3,
    # some constant value helper here too
    p_Runtime=cf.helpers.awslambda.LambdaRuntime.python37,
    p_Handler="index.handler",
    ra_DependsOn=iam_role_for_lambda,
)
tpl.add(lbd_func)

out_lambda_role_arn = cf.Output(
    "LbdRoleArn",
    Description="aws lambda basic execution iam role for reuse",
    Value=iam_role_for_lambda.rv_Arn
)
tpl.add(out_lambda_role_arn)


if __name__ == "__main__":
    # my private aws account session and bucket for testing
    from cottonformation.tests.boto_ses import boto_ses, bucket

    # define the Parameter.EnvName value
    env_name = "ctf-1-quick-start-1-basic"

    # create an environment for deployment, it is generally a boto3 session
    # and a s3 bucket to upload cloudformation template
    env = cf.Env(boto_ses=boto_ses)
    env.deploy(
        template=tpl,
        stack_name=env_name,
        stack_parameters=dict(
            EnvName=env_name,
        ),
        bucket_name=bucket,
        include_iam=True,
    )

获得帮助

  1. 学习最佳实践的最简单方法是通过实例学习您可以通过示例从棉花形成开始。然后您可以在棉花成型最佳实践中学习在生产环境中证明的最佳实践

  2. 第二种方法是提交一个 GitHub 问题。所以其他人也可能会看到讨论和解决方案。另外在 gitter 上有一个cottonformation 社区 可以直接问作者

https://badges.gitter.im/cottonformation/community.svg

概述

为什么这个项目?

目标

有很多云基础设施作为代码工具可用AWS CloudFormationTerraformtroposphereaws cdkpulumi。他们都以不同的方式好。Cottonformation并没有试图击败或取代他们中的任何一个,而是专注于以特殊的方式成为最好的。

  1. 最有利于发展。

  2. 最用户友好,无需记忆,没有困难的学习曲线。

  3. 代码少,重量轻,易于定制和扩展。

历史

第一代基础设施即代码 ( IAC ) 可能是 2011 年首次发布的 AWS CloudFormation 和 2014 年首次发布的 Terraform。第一代 IAC 主要是领域特定语言 ( DSL )。它们不如 Java、C#、Python、Ruby、Go 等通用编程语言强大。由于DSL的特性,数据难以操作,逻辑流程难以定制,代码复用性差,难以定制和扩展。

规则破坏者对流层于 2013 年发布。它是一个 Python 项目,允许您使用面向目标的编程模型在 Python 中编写 CloudFormation 模板。但由于最初的代码设计,它本身无法支持现代开发人员功能,如“自动完成”和“类型提示”。因此,至少 50% 的开发时间用于查找手册、阅读文档。与此同时,AWS Cloudformation 正在快速发展,支持更多的 AWS 资源。由于对流层依赖于维护者手动添加实现,它通常落后于最新功能。

我开始维护一个并行库troposphere_mate以支持“自动完成”和“类型提示”以及更高级的功能。但是,它不能快速进化,因为它是基于对流层的,我完全无法控制它。我曾经想过使用最新的编程模型重新设计一个新项目来替换我组织中的对流层。但是有 162 个 AWS 服务、768 个 AWS 资源、2,499 个 AWS 财产和 43,200 行声明代码需要处理。作为个人开发人员,不可能使其保持最新状态。

幸运的是,AWS 将 AWS CloudFormation 资源规范发布为机器可读的 json 文件,并且类型提示和静态检查技术在 Python 社区中已经成熟,我相信现在是重新发明现代化 CloudFormation 工具的好时机。我想出了一种方法来自动生成 162 个 AWS 服务、768 个 AWS 资源、2,499 个 AWS 属性和 43,200 行带有类型提示/自动完成/aws 文档跳转功能的声明代码。现在,我们可以使用 AWS CloudFormation 轻松地让cottonformation 保持最新!

AWS CDK 或 Pulumi 呢?

同样,cottomformation不想成为改进的 AWS CDK 或 Pulumi。它想在作为 IAC 工具的有限但重要的功能上做到最好。

AWS CDK

Python 不是 AWS CDK 中的一等成员。AWS CDK 的本质是一个 TypeScript 库,AWS 找到了一种从 Java / Ruby / Python / C# / Go 等其他编程语言调用 TypeScript / JavaScript API 的方法。当您使用 TypeScript 以外的编程语言运行 AWS CDK 时,代码实际上已被转换为低级 api,并由后端 TypeScript 代码处理。这会导致两个问题:

  1. 编辑严重延误。由于“类型提示”和“代码完成”是基于静态代码分析技术和 Python 导入引擎。但是当您导入 AWS 资源声明类时,AWS CDK 会在内部导入底层编译的 Python - TypeScript *.jsii代码。这就是 2019 年 16GB 内存 Macbook Pro 速度慢的原因。

  2. 难以定制和扩展。由于 python 代码是调用 TypeScript API 的底层,因此您无法在 python 代码中注入自定义逻辑,因为 TypeScript API 无法识别它。

  3. 您必须将 Node.JS 环境配置为 AWS CDK 的 Node.JS 版本。在 python 社区中,我们期望一个简单的pip install something然后import something。在远程或 CI 环境中运行它时,您需要额外的配置步骤。

普鲁米

Pulumi 更像是 terraform。与 troposphere 和 AWS CDK 不同,它不会将脚本转换为 CloudFormation,而是使用它自己的执行引擎来部署资源。虽然它很容易学习并且很有价值,但是你需要学习很多新的概念和组件。

安装

Cottonformation是在 PyPI 上发布的,所以你只需要:

$ pip install cottonformation

要升级到最新版本:

$ pip install --upgrade cottonformation

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

Cottonformation-0.0.8.tar.gz (710.9 kB 查看哈希

已上传 source

内置分布

Cottonformation-0.0.8-py2.py3-none-any.whl (830.4 kB 查看哈希

已上传 py2 py3