【问题标题】:How to export an existing CodePipeline to CloudFormation template如何将现有 CodePipeline 导出到 CloudFormation 模板
【发布时间】:2021-07-26 18:06:33
【问题描述】:

这更多是我缺乏理解,但我似乎无法调试它。 我创建了一个运行 terraform apply 的代码管道(它在内部为我创建了 aws 基础设施)。代码管道似乎正在工作。

我需要为另一个帐户实现相同的代码管道,我该怎么做。

我尝试使用以下命令获取 json 脚本。 aws codepipeline get-pipeline --name

我将 json 脚本转换为 yaml 脚本。

当我尝试在另一个帐户上运行 yaml 脚本时,出现以下错误

Template format error: At least one Resources member must be defined.

问题: 1.) 我可以将代码管道导出到 cloudformation 模板的最佳方式 2.) 我用的方法不行,怎么解决?

{
    "pipeline": {
        "name": "my-code-pipeline",
        "roleArn": "arn:aws:iam::aws-account-id:role/service-role/AWSCodePipelineServiceRole-aws-region-my-code-pipeline",
        "artifactStore": {
            "type": "S3",
            "location": "codepipeline-aws-region-45856771421"
        },
        "stages": [
            {
                "name": "Source",
                "actions": [
                    {
                        "name": "Source",
                        "actionTypeId": {
                            "category": "Source",
                            "owner": "ThirdParty",
                            "provider": "GitHub",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "Branch": "master",
                            "OAuthToken": "****",
                            "Owner": "github-account-name",
                            "PollForSourceChanges": "false",
                            "Repo": "repo-name"
                        },
                        "outputArtifacts": [
                            {
                                "name": "SourceArtifact"
                            }
                        ],
                        "inputArtifacts": [],
                        "region": "aws-region",
                        "namespace": "SourceVariables"
                    }
                ]
            },
            {
                "name": "codebuild-for-terraform-init-and-plan",
                "actions": [
                    {
                        "name": "codebuild-for-terraform-init",
                        "actionTypeId": {
                            "category": "Build",
                            "owner": "AWS",
                            "provider": "CodeBuild",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "ProjectName": "my-code-pipeline-build-stage"
                        },
                        "outputArtifacts": [],
                        "inputArtifacts": [
                            {
                                "name": "SourceArtifact"
                            }
                        ],
                        "region": "aws-region"
                    }
                ]
            },
            {
                "name": "manual-approve",
                "actions": [
                    {
                        "name": "approval",
                        "actionTypeId": {
                            "category": "Approval",
                            "owner": "AWS",
                            "provider": "Manual",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "NotificationArn": "arn:aws:sns:aws-region:aws-account-id:Email-Service"
                        },
                        "outputArtifacts": [],
                        "inputArtifacts": [],
                        "region": "aws-region"
                    }
                ]
            },
            {
                "name": "codebuild-for-terraform-apply",
                "actions": [
                    {
                        "name": "codebuild-for-terraform-apply",
                        "actionTypeId": {
                            "category": "Build",
                            "owner": "AWS",
                            "provider": "CodeBuild",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "ProjectName": "codebuild-project-for-apply"
                        },
                        "outputArtifacts": [],
                        "inputArtifacts": [
                            {
                                "name": "SourceArtifact"
                            }
                        ],
                        "region": "aws-region"
                    }
                ]
            }
        ],
        "version": 11
    },
    "metadata": {
        "pipelineArn": "arn:aws:codepipeline:aws-region:aws-account-id:my-code-pipeline",
        "created": "2020-09-17T13:12:50.085000+05:30",
        "updated": "2020-09-21T15:46:19.613000+05:30"
    }
}

给定的代码是我用来创建 cloudformation 模板的 yaml 模板。

【问题讨论】:

  • 模板的其余部分在哪里?
  • @Marcin,尝试下面的命令 aws codepipeline get-pipeline --name 给了我 json 格式的模板,转换为 yaml,这就是我得到的。
  • @Marcin,您是否可以推荐一些步骤来获取示例代码管道的 cloudformation 模板。 (我希望在多个帐户以及多个区域中使用相同的管道,最好的选择似乎已经创建了一个 cloudformation 模板。
  • 您必须手动创建此类模板或查看第三方工具,例如前2.
  • @Marcin ,我查看了第三方工具,例如former2,但它给出了同样的问题。手动操作是唯一的选择。

标签: amazon-web-services terraform amazon-cloudformation aws-codepipeline aws-cloudformation-custom-resource


【解决方案1】:

aws codepipeline get-pipeline --name CLI 命令返回有关管道结构和管道元数据的信息,但它与 CloudFormation 模板(或其资源部分)的格式不同。

没有内置支持导出现有 AWS 资源以创建 CloudFormation 模板,但您有几个选项。

  1. 使用former2(由 AWS 精英 Ian Mckay 构建和维护)从您选择的资源中生成 CloudFormation 模板。
  2. 从您使用的aws codepipeline get-pipeline --name 命令获取 JSON 输出并手动制作 CloudFormation 模板。管道将是the full template 中资源列表中的一项资源。它包含的信息非常接近,但需要进行一些调整以符合 CodePipeline 的 CloudFormation 资源规范,您可以找到 here。您还需要使用aws <service name> describe 对需要引入模板的其他资源执行相同操作。

如果您选择选项 2(即使您不这样做),我建议您在代码编辑器中使用 cfn-lint 以帮助遵守规范。

【讨论】:

  • 这很有帮助。我继续使用选项2。唯一的问题是它需要我已经创建了资源。因此,当我想在 aws 帐户中运行模板时,它需要我已经拥有某些可用资源。
猜你喜欢
  • 1970-01-01
  • 2019-11-09
  • 2017-06-04
  • 2018-11-27
  • 2021-08-30
  • 2017-11-14
  • 2023-03-19
  • 2016-10-29
  • 2021-07-23
相关资源
最近更新 更多