Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@sonodar

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

More than 3 years have 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)}/*" )}"]
  }
}

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

3
Help us understand the problem. What is going on with this article?
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
sonodar
AWS, Java, JavaScript が圧倒的に得意。次いでC#, Ruby, PHP, Go, Pythonの順で得意。
m3dev
インターネット、最新IT技術を活用し日本・世界の医療を改善することを目指します

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?