使用Terraform基于参数化名称创建多个aws_cloudformation_stack

问题描述

是否可以基于参数化名称terraform中使用一个 Process: com.gamze.pawsbook,PID: 30879 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:354) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) at java.util.concurrent.FutureTask.run(FutureTask.java:271) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) at org.json.JSONTokener.nextValue(JSONTokener.java:94) at org.json.JSONObject.<init>(JSONObject.java:159) at org.json.JSONObject.<init>(JSONObject.java:176) at com.gamze.pawsbook.MapActivitiy$ParserTask.doInBackground(MapActivitiy.java:225) at com.gamze.pawsbook.MapActivitiy$ParserTask.doInBackground(MapActivitiy.java:215) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)  at java.lang.Thread.run(Thread.java:764)  资源定义创建多个CloutFormation堆栈? 我定义了以下资源,并且希望每个aws_cloudformation_stackapp_name app_env组合都有一个堆栈:

build_name

resource "aws_s3_bucket_object" "sam_deploy_object" { bucket = var.sam_bucket key = "${var.app_env}/${var.build_name}/sam_template_${timestamp()}.yaml" source = "../.aws-sam/sam_template_output.yaml" etag = filemd5("../.aws-sam/sam_template_output.yaml") } resource "aws_cloudformation_stack" "subscriptions_sam_stack" { name = "${var.app_name}---${var.app_env}--${var.build_name}" capabilities = ["CAPABILITY_NAMED_IAM","CAPABILITY_AUTO_EXPAND"] template_url = "https://${var.sam_bucket}.s3-${data.aws_region.current.name}.amazonaws.com/${aws_s3_bucket_object.sam_deploy_object.id}" } 名称更改时运行terraform apply时,旧堆栈将被删除并创建一个新堆栈,但是我想保留旧堆栈并创建一个新堆栈

解决方法

一种方法是将变量//add bar $("#btn-Newbar").on("click",function() { $("#hbar").append("<div class='col-4' style='margin-bottom:20px;'><div class='row'><div class='col-3'><img src='' class='catHpic' style='height:80px;width:80px;'></div><div class='col-9'><input type='file' class='hpicFile'><div class='input-group mb-3' style='margin-top:10px;'><div class='input-group-prepend'><span class='input-group-text url'>URL</span></div><input type='text' class='form-control' aria-describedby='basic-addon1'></div></div></div></div>"); }); //display highlight pic real time $(document).on("change",".hpicFile",function(e) { var imgSrc = $(this).parent('div').prev().find('.catHpic') //find img if (e.target.files[0] != null) { //console.log(e.target.files[0]); $.each(this.files,function(key,file) { var reader = new FileReader(); reader.onload = function(e) { //display images real time imgSrc.attr('src',e.target.result); } reader.readAsDataURL(file); }); } });定义为列表。然后,当您创建新的内部版本时,只需将它们附加到列表中,并借助for_each创建堆栈以遍历内部版本名称。

例如,如果您具有以下条件:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-body">
    <div class="row" id="hbar">
    </div>
</div>
<div class="card-footer" style="text-align:right;">
    <button class="btn btn-secondary" id="btn-Newbar">New Bar</button>
</div>

然后,如果要为堆栈进行第二次构建,则只需扩展build_name

variable "app_name" {
  default = "test1"
}

variable "app_env" {
  default = "test2"
}


variable "build_name" {
  default = ["test3"]
}

resource "aws_cloudformation_stack" "subscriptions_sam_stack" {

  for_each     = toset(var.build_name) 
  
  name         = "${var.app_name}---${var.app_env}--${each.value}"
  capabilities = ["CAPABILITY_NAMED_IAM","CAPABILITY_AUTO_EXPAND"]
  template_url = "https://${var.sam_bucket}.s3-${data.aws_region.current.name}.amazonaws.com/${aws_s3_bucket_object.sam_deploy_object.id}"  
}