0

执行 AWS cli 命令如下:

aws codepipeline get-pipeline --name pipeline_name

产生以下输出

{
    "pipeline": {
        "name": "xxxxx",,
        "roleArn": "xxxxx",,
        "artifactStore": {
            "type": "S3",
            "location": "xxxxx",
        },
        "stages": [
            {
                "name": "Source",
                "actions": [
                    {
                        "name": "Source",
                        "actionTypeId": {
                            "category": "Source",
                            "owner": "AWS",
                            "provider": "CodeCommit",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "BranchName": "main",
                            "PollForSourceChanges": "true",
                            "RepositoryName": "xxxxx",
                        },
                        "outputArtifacts": [
                            {
                                "name": "SourceOutput"
                            }
                        ],
                        "inputArtifacts": []
                    }
                ]
            },
            {
                "name": "Build",
                "actions": [
                    {
                        "name": "Build",
                        "actionTypeId": {
                            "category": "Build",
                            "owner": "AWS",
                            "provider": "CodeBuild",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "ProjectName": "xxxxx",
                        },
                        "outputArtifacts": [
                            {
                                "name": "BuildOutput"
                            }
                        ],
                        "inputArtifacts": [
                            {
                                "name": "SourceOutput"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "Deploy",
                "actions": [
                    {
                        "name": "Deploy",
                        "actionTypeId": {
                            "category": "Deploy",
                            "owner": "AWS",
                            "provider": "CodeDeployToECS",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "AppSpecTemplateArtifact": "BuildOutput",
                            "AppSpecTemplatePath": "appspec.yml",
                            "ApplicationName": "xxxxx",
                            "DeploymentGroupName": "xxxxx",
                            "Image1ArtifactName": "BuildOutput",
                            "Image1ContainerName": "IMAGE1_NAME",
                            "TaskDefinitionTemplateArtifact": "BuildOutput",
                            "TaskDefinitionTemplatePath": "taskdef.json"
                        },
                        "outputArtifacts": [],
                        "inputArtifacts": [
                            {
                                "name": "BuildOutput"
                            }
                        ]
                    }
                ]
            }
        ],
        "version": 3
    },
    "metadata": {
        "pipelineArn": "xxxxx",
        "created": "xxxxx",
        "updated": "xxxxx",
    }
}

我们可以看到 metadata 字段有 "pipelineArn": "xxxxx",. 但是这个 arn 在控制台中不可用,我也找不到任何 Terraform 数据源。

是否可以在 Terraform 中检索 codepipline ARN?

另外,为了澄清我在需要"aws_codestarnotifications_notification_rule"资源 arn 的地方需要这个。

4

1 回答 1

2

CodePipeline 的 ARN 在 AWS 控制台中可用,但有点难找。如果你去Pipelines -> Choose any pipeline -> Settings它是在General选项卡中,在Pipeline ARN(1)下。

在此处输入图像描述

至于通过 Terraform 获取 CodePipeline ARN,可以在创建 CodePipeline 资源后获取同名属性(2)。因此,如果 CodePipline 不是使用 Terraform 创建的,您可以对值进行硬编码。如果是,您可以简单地引用"aws_codestarnotifications_notification_rule"资源中的输出属性:

resource "aws_codestarnotifications_notification_rule" "this" {
  detail_type    = ""
  event_type_ids = [""]

  name     = ""
  resource = aws_codepipeline.this.arn

  target {
    address = ...
  }
}

此代码片段假定您将填写其他详细信息,并且有一个 Terraform 代码块,它创建一个名为 this 的 CodePipeline 资源,即,您必须有一个类似于以下的代码块:

resource "aws_codepipeline" "this" {
...
}

  1. https://docs.aws.amazon.com/codepipeline/latest/userguide/pipelines-view-console.html#pipelines-settings-console
  2. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codepipeline#arn
于 2022-02-11T07:19:08.987 回答