hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.74k stars 9.1k forks source link

[Enhancement]: aws_codepipeline V2 does not support referencing pipeline level variable into stage #36409

Open SleepingTalent opened 6 months ago

SleepingTalent commented 6 months ago

Description

Hi,

I'm trying out the V2 flavour of codepipeline that was released as part of the AWS 5.41.0 provider.

I cant seem to find a way to reference my variable defined at the codepipline level into the any of the stage actions.

I assumed I could reference the pipeline variable using #{codepipeline.ROLE_NAME}

Affected Resource(s) and/or Data Source(s)

No response

Potential Terraform Configuration

resource "aws_codepipeline" "code_pipeline" {
  name     = var.pipeline_name
  pipeline_type = var.pipeline_type
  role_arn = aws_iam_role.code_pipeline_service_role.arn
  variable {
    name = "ROLE_NAME"
  }
  artifact_store {
    location = aws_s3_bucket.code_pipeline_artifacts_bucket.bucket
    type     = "S3"
  }
  stage {
    name = "Source"
    action {
      category         = "Source"
      name             = "SourceAction"
      owner            = "AWS"
      provider         = "CodeCommit"
      version          = "1"
      output_artifacts = ["SourceArtifact"]

      configuration = {
        PollForSourceChanges = false
        RepositoryName = var.source_repository
        BranchName     = var.source_repository_branch
      }
    }
  }

  stage {
    name = "CESharedServices"
    action {
      category        = "Build"
      name            = "GeneratePlan"
      owner           = "AWS"
      provider        = "CodeBuild"
      input_artifacts = ["SourceArtifact"]
      version         = "1"
      run_order       = 1
      role_arn        = module.ce_shared_services_plan.codebuild_role_arn

      configuration = {
        ProjectName = module.ce_shared_services_plan.codebuild_name
        EnvironmentVariables = jsonencode([
          {
            name  = "ROLE_NAME"
            value = "#{codepipeline.ROLE_NAME}"
            type  = "PLAINTEXT"
          }
        ])
      }
    }
    action {
      name      = "ApproveDeployment"
      category  = "Approval"
      owner     = "AWS"
      provider  = "Manual"
      version   = "1"
      run_order = 2
    }
    action {
      category        = "Build"
      name            = "Deploy"
      owner           = "AWS"
      provider        = "CodeBuild"
      input_artifacts = ["SourceArtifact"]
      version         = "1"
      run_order       = 3
      role_arn        = module.ce_shared_services_apply.codebuild_role_arn

      configuration = {
        ProjectName = module.ce_shared_services_apply.codebuild_name
        EnvironmentVariables = jsonencode([
          {
            name  = "ROLE_NAME"
            value = "#{codepipeline.ROLE_NAME}"
            type  = "PLAINTEXT"
          }
        ])
      }
    }
  }
}

References

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codepipeline

Would you like to implement a fix?

None

github-actions[bot] commented 6 months ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

sgreathouse-rgare commented 5 months ago

@SleepingTalent I got this to work. You are calling the reserved codepipeline namespace instead of the shared variables namespace.
value = "#{codepipeline.ROLE_NAME}" should be value = "#{variables.ROLE_NAME}"