Codepipeline ARN 不可通过 Terraform 使用,但可使用 cli 在元数据中使用

Codepipeline ARN not available through Terraform but available in metadata using cli

正在执行如下 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",
    }
}

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

是否可以在 Terraform 中检索代码管道 ARN?

此外,为了澄清,我需要这个 "aws_codestarnotifications_notification_rule" 需要资源 arn。

AWS 控制台中提供了 CodePipeline 的 ARN,但有点难找。如果您转到 Pipelines -> Choose any pipeline -> Settings,它位于 General 选项卡中,在 Pipeline ARN (1) 下。

至于通过 Terraform 获取 CodePipeline ARN,可以通过在创建 CodePipeline 资源后获取具有相同名称的属性来实现 (2)。因此,如果 CodePipline 不是使用 Terraform 创建的,您可以 hard-code 该值。如果是,您可以简单地引用 "aws_codestarnotifications_notification_rule" 资源中的输出属性:

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

  name     = ""
  resource = aws_codepipeline.this.arn

  target {
    address = ...
  }
}

此代码片段假定您将填写其他详细信息,并且有一个 Terraform 代码块创建一个名为此的 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