terraform - I'd like to tag resources with the git-sha that created them, is this possible? - Stack Overflow

If I use the random_id, (like below), it will create the random value once and then never change itres

If I use the random_id, (like below), it will create the random value once and then never change it

resource "random_id" "example" {
  ...
}

resource "aws_*" "example" {
  tags = { 
    ...
    random_id = resource.random_id.example.hex
  }
}

I would like the same sort of behaviour, but with a non-random value - something like a git-sha. Maybe it would look something like this, or maybe it would just be a terraform built-in?

resource "hypothetical_static_value" "example" {
  value = var.git_sha
}

resource "aws_*" "example" {
  tags = { 
    ...
    created_sha = resource.hypothetical_static_value.example.value
  }
}

There are a few workarounds that probably sort-of work, but are too convoluted:

  • I think I might be able to use the random_id to get this, since it takes a prefix, and I could have a local that strips off the random value off the end and effectively then just uses the prefix.
  • I could probably add a lifecycle { ignore_changes = [tags["created_sha"] } to the resource - but that would need to be added to every resource.
  • I might be able to add a secret or an ssm parameter that would store the git-sha as a value and then use data lookup to reference that in my individual resources. But this then takes up resources and space in the cloud that are unnecessary (since the same is stored in state and then we'd have to sync with them on every plan and apply). I think that resource probably needs lifecycle rules too.
  • for anyone wanting just a timestamp instead of a git-sha, there is a time_static resource that works

Is there a good, simple way to handle this?

If I use the random_id, (like below), it will create the random value once and then never change it

resource "random_id" "example" {
  ...
}

resource "aws_*" "example" {
  tags = { 
    ...
    random_id = resource.random_id.example.hex
  }
}

I would like the same sort of behaviour, but with a non-random value - something like a git-sha. Maybe it would look something like this, or maybe it would just be a terraform built-in?

resource "hypothetical_static_value" "example" {
  value = var.git_sha
}

resource "aws_*" "example" {
  tags = { 
    ...
    created_sha = resource.hypothetical_static_value.example.value
  }
}

There are a few workarounds that probably sort-of work, but are too convoluted:

  • I think I might be able to use the random_id to get this, since it takes a prefix, and I could have a local that strips off the random value off the end and effectively then just uses the prefix.
  • I could probably add a lifecycle { ignore_changes = [tags["created_sha"] } to the resource - but that would need to be added to every resource.
  • I might be able to add a secret or an ssm parameter that would store the git-sha as a value and then use data lookup to reference that in my individual resources. But this then takes up resources and space in the cloud that are unnecessary (since the same is stored in state and then we'd have to sync with them on every plan and apply). I think that resource probably needs lifecycle rules too.
  • for anyone wanting just a timestamp instead of a git-sha, there is a time_static resource that works

Is there a good, simple way to handle this?

Share Improve this question asked Mar 3 at 15:11 davidpricedevdavidpricedev 2,2473 gold badges21 silver badges39 bronze badges 1
  • I would probably pass the current git SHA in as an input variable to Terraform, and simply use that value directly as the created_sha tag, and also add the lifecycle ignore_changes thing like you mentioned in your 2nd bullet point to every resource. That's the cleanest way I can think of to handle this. – Mark B Commented Mar 3 at 17:03
Add a comment  | 

1 Answer 1

Reset to default 0

Terraform does not have any built-in way to retrieve information about any Git repository that might contain the source code it is working with, and so any solution here will involve a provider plugin that is able to query that information.

I don't know if there's a provider already available that directly interacts with Git, but you could potentially do it with the external data source from the hashicorp/external provider, which treats an external command as a Terraform data source. To do that you will need to write an external program or script that runs Git to find out what it thinks the current commit ID is and then prints a JSON object where the commit ID is included as one of the property values.

You could then use the result of that data source to populate your tag, and use ignore_changes as you described to avoid every tag on every resource being updated each time you make a new Git commit.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745087308a4610480.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信