Terraform模块强制定义资源中定义的所有块,是否有任何方法可以从资源中进行动态导入



我正在尝试使用TF中的资源创建aws代码管道。这是我在m,y TF中的参考资料部分。

resource "aws_codepipeline" "codepipeline" {
name     = var.name                                   
role_arn = var.role_arn                               

artifact_store {
location = var.location                             
type     = var.type
}

stage {
name = var.stage1_name                              

action {
name             = var.action1_name     
category         = var.source_category
owner            = var.source_owner    
provider         = var.source_provider            
version          = var.source_version             
output_artifacts = var.source_output_artifacts    

configuration = {
ConnectionArn        = var.connection_arn           
FullRepositoryId     = var.full_repository_id         
BranchName           = var.branch_name   
OutputArtifactFormat = var.output_artifact_format             
}
}
}

stage {
name = var.stage2_name                               

action {
name             =  var.action2_name               
category         =  var.build_category             
owner            =  var.build_owner                
provider         = var.build_provider              
input_artifacts  = var.input_artifacts             
output_artifacts = var.build_output_artifacts      
version          = var.build_version     

configuration = {
ProjectName = var.project_name
EnvironmentVariables = var.environment_variables /*jsonencode(
[
{
name  = var.environment_name
type  = var.environment_type
value = var.environment_value
}
]
)  */     
}
}
}
}

在我的TF模块部分中,通过调用上面给出的资源来创建代码管道。我的模块代码是

module "codepipeline_notification" {
source = "../../modules/codepipeline"
name                    = var.codepipeline_lambda_notification_name                                   
role_arn                = aws_iam_role.cp_lambda_deploy_role.arn #var.codepipeline_lambda_notification_role_arn                               
location                = module.s3_codepipeline_artifact.s3_bucket_account_id   #var.codepipeline_lambda_notification_location                             
type                    = var.codepipeline_lambda_notification_type

stage1_name             = var.codepipeline_lambda_notification_stage1_name    
action1_name            = var.codepipeline_lambda_notification_action1_name            
source_category         = var.codepipeline_lambda_notification_source_category
source_owner            = var.codepipeline_lambda_notification_source_owner   
source_provider         = var.codepipeline_lambda_notification_source_provider            
source_version          = var.codepipeline_lambda_notification_source_version            
source_output_artifacts = var.codepipeline_lambda_notification_source_output_artifacts
full_repository_id      = var.codepipeline_lambda_notification_full_repository_id         
branch_name             = var.codepipeline_lambda_notification_branch_name               
output_artifact_format  = var.codepipeline_lambda_notification_output_artifact_format
environment_variables   = jsonencode(
[
{
name  = var.codepipeline_lambda_notification_environment_name
type  = var.codepipeline_lambda_notification_environment_type
value = var.codepipeline_lambda_notification_environment_value
}
]
)         
build_output_artifacts        = var.codepipeline_lambda_notification_build_output_artifacts   
connection_arn          = module.codestarconnections.arn
stage2_name             = var.codepipeline_lambda_notification_stage2_name                               
action2_name            = var.codepipeline_lambda_notification_action2_name     
build_category         = var.codepipeline_lambda_notification_build_category   
build_owner            = var.codepipeline_lambda_notification_build_owner      
build_provider         = var.codepipeline_lambda_notification_build_provider              
build_version          = var.codepipeline_lambda_notification_build_version               
input_artifacts         = var.codepipeline_lambda_notification_input_artifacts   
project_name             = module.codebuild_notification.name
}

使用这种方法,我试图创建4个管道,其中一个管道只有2个阶段,另两个管道有3个阶段。如果我在资源中定义了3个阶段,那么Terraform会强制模块在所有管道中创建3个阶段(我需要两个阶段(。地形中是否有任何方法可以根据条件在资源中定义并在模块中使用资源

不确定你是否得到了问题的答案,但是的,有办法。它被称为动态管道。我有一个存储库,可以引导您了解动态管道的使用情况。简而言之,您将资源视为动态资源,使用每条语句并将配置作为映射传入。

模块如下所示:

resource "aws_codepipeline" "codepipeline" {
for_each = var.code_pipeline
name     = "${local.name_prefix}-${var.AppName}"
role_arn = each.value["code_pipeline_role_arn"]
tags = {
Pipeline_Key = each.key
}
artifact_store {
type     = lookup(each.value, "artifact_store", null) == null ? "" : lookup(each.value.artifact_store, "type", "S3")
location = lookup(each.value, "artifact_store", null) == null ? null : lookup(each.value.artifact_store, "artifact_bucket", null)
}
dynamic "stage" {
for_each = lookup(each.value, "stages", {})
iterator = stage
content {
name = lookup(stage.value, "name")
dynamic "action" {
for_each = lookup(stage.value, "actions", {}) //[stage.key]
iterator = action
content {
name             = action.value["name"]
category         = action.value["category"]
owner            = action.value["owner"]
provider         = action.value["provider"]
version          = action.value["version"]
run_order        = action.value["run_order"]
input_artifacts  = lookup(action.value, "input_artifacts", null)
output_artifacts = lookup(action.value, "output_artifacts", null)
configuration    = action.value["configuration"]
namespace        = lookup(action.value, "namespace", null)
}
}
}
}
}

执行模块

module "code_pipeline" {
source = "../module-aws-codepipeline" #using module locally
#source       = "your-github-repository/aws-codepipeline" #using github repository
AppName       = "My_new_pipeline"
code_pipeline = local.code_pipeline
}

带有管道变量的示例locals.tf

locals {
/*
DECLARE enviornment variables.  Note each Action does not require environment variables
*/
action_second_stage_variables = [
{
name  = "PIPELINE_EXECUTION_ID"
type  = "PLAINTEXT"
value = "#{codepipeline.PipelineExecutionId}"
},
{
name  = "NamespaceVariable"
type  = "PLAINTEXT"
value = "some_value"
},
]
action_third_stage_variables = [
{
name  = "PL_VARIABLE_1"
type  = "PLAINTEXT"
value = "VALUE1"
},
{
name  = "PL_VARIABLE 2"
type  = "PLAINTEXT"
value = "VALUE2"
},
{
name  = "PL_VARIABLE_3"
type  = "PLAINTEXT"
value = "VAUE3"
},
{
name  = "PL_VARIABLE_4"
type  = "PLAINTEXT"
value = "#{BLD.NamespaceVariable}"
},
]
/*
BUILD YOUR STAGES
*/
code_pipeline = {
codepipeline-configs = {
code_pipeline_role_arn = "arn:aws:iam::aws_account_name:role/role_name"
artifact_store = {
type            = "S3"
artifact_bucket = "your-aws-bucket-name"
}
stages = {
stage_1 = {
name = "Download"
actions = {
action_1 = {
run_order        = 1
category         = "Source"
name             = "First_Stage"
owner            = "AWS"
provider         = "CodeCommit"
version          = "1"
output_artifacts = ["download_ouput"]
configuration = {
RepositoryName       = "Codecommit_target_repo"
BranchName           = "main"
PollForSourceChanges = true
OutputArtifactFormat = "CODE_ZIP"
}
}
}
}
stage_2 = {
name = "Build"
actions = {
action_1 = {
run_order        = 2
category         = "Build"
name             = "Second_Stage"
owner            = "AWS"
provider         = "CodeBuild"
version          = "1"
namespace        = "BLD"
input_artifacts  = ["Download_ouput"]
output_artifacts = ["build_outputs"]
configuration = {
ProjectName          = "codebuild_project_name_for_second_stage"
EnvironmentVariables = jsonencode(local.action_second_stage_variables)
}
}
}
}
stage_3 = {
name = "Validation"
actions = {
action_1 = {
run_order        = 1
name             = "Third_Stage"
category         = "Build"
owner            = "AWS"
provider         = "CodeBuild"
version          = "1"
input_artifacts  = ["build_outputs"]
output_artifacts = ["validation_outputs"]
configuration = {
ProjectName          = "codebuild_project_name_for_third_stage"
EnvironmentVariables = jsonencode(local.action_third_stage_variables)
}
}
}
}
}
}
}
}

该模块的全部使用可以在这个GitHub存储库中找到。在您的情况下,您可以传入多个资源,以便在一个具有唯一和自定义阶段和操作的模块中创建各种管道。我希望这能有所帮助。

相关内容

  • 没有找到相关文章

最新更新