amazon-web-servicesterraformamazon-snsaws-codepipelineaws-codestar

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


Executing AWS cli command as below:

aws codepipeline get-pipeline --name pipeline_name

produces following output

{
    "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",
    }
}

We can see that metadata field has "pipelineArn": "xxxxx", . But this arn is not available in console nor have I been able to find any Terraform data source for this.

Is it possible to retrieve codepipline ARN in Terraform?

Also, to clarify I need this for "aws_codestarnotifications_notification_rule" where resource arn is required.


Solution

  • The ARN for the CodePipeline is available in the AWS console, but it is a bit hard to find. If you go to Pipelines -> Choose any pipeline -> Settings it is in the General tab, under Pipeline ARN (1).

    enter image description here

    As for the getting the CodePipeline ARN through Terraform, that is possible by getting the attribute with the same name after the CodePipeline resource is created (2). So in case the CodePipline was not created with Terraform, you can hard-code the value. If it was, you could simply reference the output attribute in the "aws_codestarnotifications_notification_rule" resource:

    resource "aws_codestarnotifications_notification_rule" "this" {
      detail_type    = ""
      event_type_ids = [""]
    
      name     = ""
      resource = aws_codepipeline.this.arn
    
      target {
        address = ...
      }
    }
    

    This code snippet assumes that you will fill out other details and that there is a Terraform code block which creates a CodePipeline resource with name this, i.e., you would have to have a code block similar to following:

    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