# Directoryless User Administration in AWS IAM With Terraform and CI/CD

Page content

I just completed some work on a little project with some unique requirements. It's a project that uses Terraform to provision infrastructure within AWS. That's not too terribly hard. We're trying to make the platform, infrastructure and code as reusable as possible while maintaining customer-specific privacy and security requirements.

The requirements and curve balls were unique enough to make this project a little challenging:

• Create and manage IAM users inside an AWS account.
• Provision IAM roles inside subaccounts within the organization (or inside the main account if your use case is not as complex as this).
• Provision sts-assume-role permissions on those roles based on group membership from an identity provider.

Sounds simple, right? Well, let's add in the curve balls:

• You cannot set sts-assume-role policies based on IAM group membership (this is an AWS limitation). You can do this with SAML and/or some kind of federated access, but in this case that was not available to us. We had to provide some way to do this without an IdP and only manage users inside IAM. If you're using pure IAM, you can only provision users to assume roles on a user-by-user basis. Ick.
• Do not hard-code the usernames or group membership inside the Terraform.
• Make it work with a CI/CD deployment - this means you can't use a local workstation tfvars file to define the users.
• Treat the usernames and group memberships as sensitive information - which means they must be encrypted.

Setting up CI/CD to work with your Terraform deployment is outside the scope of this article. I'm only focusing on the little bits of code that I used to make this work. Let's just assume that code that is pushed to your master branch is deployed to production within AWS.

How did we pull this off? AWS’ EC2 Parameter Store to the rescue. Parameter Store allows you to just store key/value pairs. You can store a string, StringList, or SecureString. A SecureString requires the use of a KMS key. So you'll have to create a KMS key manually or through your Terraform code.

After the KMS key is created, set up your parameters. In my use case, I set up four parameters. The first parameter is a SecureString. It's just a comma-delimited list of usernames you wish to create. Terraform can automagically decrypt the parameter store object through code, provided the user executing the code has access to use the key to decrypt the parameter store object. You can use the web console or AWS CLI to create this parameter store object and its value. You don't want to create the parameter store object in your Terraform code, since one of the requirements was to NOT hard-code the user names or use tfvars.

The Terraform code to read the value looks like this:

data "aws_ssm_parameter" "iam_user_list" {
name = "iam-user-list"
}


All this does is set up a method inside your Terraform to look at the parameter store object and read the value by calling: ${data.aws_ssm_parameter.iam_user_list.value} elsewhere in your code. Terraform will go out to AWS, find the parameter you supplied in the “name” property and read it into memory. Now it's available for use in other places. Remember though, we supplied the values as a comma-delimited list. This is important because that's where things get tricky. First we have to create the users identified in that parameter store object. The best way to accomplish this is to user a local to split the comma-delimited value into a usable list, then loop through that list and create the users. locals { user_list = ["${split(",", data.aws_ssm_parameter.iam_user_list.value)}"]
}

resource "aws_iam_user" "iam_users" {
count = "${length(local.user_list)}" name = "${local.user_list[count.index]}"
}


Now if you run your Terraform code, you'll end up with all new IAM users created by the usernames from the list you provided in the Terraform code. Better yet, if you add/remove information from that string, Terraform will automatically adjust the next time you manually run the code or CI/CD executes it. Congratulations, you have basic user management! It may be even more useful to write a Lambda script that runs this routine every so often, but we didn't do it that way for this particular use case.

This doesn't set up the users with access keys, passwords or MFA's. Sorry, that's harder to do. For now I just handle that in the web console or CLI.

Next, let's handle the really tricky part. Again, there's no way to set up a group and use group membership to decide who should get an assume role permission. But that's ok. We can handle this in a similar fashion. Build parameters that are similar to the iam_user_list parameter. Put a comma-delimited list of the users that should belong to the “group” in this parameter. Make sure the IAM users actually exist before you go further, because Terraform will get mad at you if you try to set up sts-assume-role policies for users that do not exist.

Just like before, set up a data object that reads your new parameter.

data "aws_ssm_parameter" "admin_iam_role_list" {
}


This will expose the contents of that parameter to your Terraform template as: ${data.aws_ssm_parameter.admin_iam_role_list}. Apply the same locals trick as above and iterate through your list to build out a list of users and the ARNs that should be set in the assume-role permissions. locals { admin_iam_role_list = ["${split(",", data.aws_ssm_parameter.admin_iam_role_list.value)}"]
}

name = "${var.admin_role_name}" assume_role_policy = <<EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS":${jsonencode(concat(formatlist("arn:aws:iam::%s:user/%s", var.aws_account_id, local.tenant_viewonly_iam_role_list)))}