Help us understand the problem. What is going on with this article?

[terraform] [小ネタ] 配列の各要素の末尾に文字列を追加した配列を返す

More than 1 year has passed since last update.

Rubyならこんなに簡単に書ける。

list_variables = [ 'var1', 'var2', 'var3' ]
new_list = list_variables.map { |var| var + '_suffix' }
# p new_list
# [ 'var1_suffix', 'var2_suffix', 'var3_suffix' ]

terraformでやろうと頑張ってみて、こんな感じでできた。

variable list_variables {
  type = "list"
  default = [ "var1", "var2", "var3" ]
}

new_list = ["${split( ",", "${join("_suffix,", var.list_variables)}_suffix" )}"]

上の処理シーケンスをJavaで実装するとこんな感じ。

final List<String> listVariables = Arrays.asList("var1", "var2", "var3");
final String combinedVariables = listVariables
  .stream()
  .collect(Collectors.joining("_suffix,"))
  + "_suffix";
final String[] newVariables = combinedVariables.split(",");

実際にありそうな使い方としては、S3バケット名のリストがあるけど、その配下のオブジェクトに対して権限を付与したいときなど。

data aws_iam_policy_document "bucket_policy" {
  statement {
    actions = ["s3:GetObject", "s3:PutObject"]
    resources = ["${split( ",", "${join("/*,", var.bucket_names)}/*" )}"]
  }
}

もっといいやり方あったら教えてください。

sonodar
AWS, Java, JavaScript が圧倒的に得意。次いでC#, Ruby, PHP, Go, Pythonの順で得意。
m3dev
インターネット、最新IT技術を活用し日本・世界の医療を改善することを目指します
https://m3.recruitment.jp/engineer/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした