Last active
December 20, 2021 15:46
-
-
Save scarbeau/dfe7c0bd94bed6289c9bedfe51b5e6df to your computer and use it in GitHub Desktop.
Terraform AWS Access Key Rotation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "aws_iam_user" "monthly_credential_rotation_example" { | |
provider = aws.local | |
name = "monthly-credential-rotation-example" | |
} | |
resource "aws_iam_access_key" "one" { | |
provider = aws.local | |
count = local.modmonth == 0 ? 1 : 0 | |
user = aws_iam_user.monthly_credential_rotation_example.name | |
} | |
resource "aws_iam_access_key" "two" { | |
provider = aws.local | |
count = local.modmonth == 1 ? 1 : 0 | |
user = aws_iam_user.monthly_credential_rotation_example.name | |
} | |
data "external" "modulo_month" { | |
program = ["/bin/sh", "-c", "echo '{\"value\": \"'$(expr $(date '+%m') % 2)'\"}'"] | |
} | |
locals { | |
modmonth = tonumber(data.external.modulo_month.result.value) | |
} | |
output "accesskeyid" { | |
value = try(aws_iam_access_key.one[0].id, aws_iam_access_key.two[0].id) | |
} | |
output "secretaccesskey" { | |
value = try(aws_iam_access_key.one[0].secret, aws_iam_access_key.two[0].secret) | |
} |
ahh nevermind, I guess you can't. and im guessing you found that out already :(. Because of use of the timestamp function as described here https://www.terraform.io/language/expressions/function-calls#when-terraform-calls-functions
Yeah, terraform needs to resolve the count values in the plan so it has to be in a data source rather than a timestamp function in locals.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
looks good, instead of using an external data source you could also just use built in TF date functions
ex.