Skip to content

Instantly share code, notes, and snippets.

@Zate
Created May 12, 2020 22:16
Show Gist options
  • Save Zate/317a991ac243faf9f03ea9c543f40be0 to your computer and use it in GitHub Desktop.
Save Zate/317a991ac243faf9f03ea9c543f40be0 to your computer and use it in GitHub Desktop.
terraform Service Specific Credential with IAM user
locals {
ssc = jsondecode(data.local_file.ssc-output.content)
}
resource "aws_iam_user" "repo" {
name = "${var.origin}-${var.env}-codecommit-user"
path = "/${var.app}/${var.env}/"
provisioner "local-exec" {
command = "aws iam create-service-specific-credential --user-name ${self.name} --service-name codecommit.amazonaws.com > output.log"
}
tags = merge(
var.default_tags,
{
"Name" = "${var.origin}-${var.env}-codecommit-user"
},
)
}
data "template_file" "log_name" {
template = "${path.module}/output.log"
}
data "local_file" "ssc-output" {
filename = "${data.template_file.log_name.rendered}"
depends_on = [aws_iam_user.repo]
}
resource "null_resource" "ssc-d" {
triggers = {
name = aws_iam_user.repo.name
ssc_id = local.ssc.ServiceSpecificCredential.ServiceSpecificCredentialId
}
provisioner "local-exec" {
when = destroy
command = "aws iam delete-service-specific-credential --user-name ${self.triggers.name} --service-specific-credential-id ${self.triggers.ssc_id} > output-destroy.log"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment