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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.