Browse the Repo

file-type-icon.circleci
file-type-icon.github
file-type-icon.patcher
file-type-icon_ci
file-type-icon_docs
file-type-iconcodegen
file-type-iconexamples
file-type-iconmodules
file-type-icon_deprecated
file-type-iconauto-update
file-type-iconaws-auth
file-type-iconaws-config-bucket
file-type-iconaws-config-multi-region
file-type-iconaws-config-rules
file-type-iconaws-config
file-type-iconaws-organizations
file-type-iconcloudtrail-bucket
file-type-iconcloudtrail
file-type-iconcross-account-iam-roles
file-type-iconcustom-iam-entity
file-type-iconebs-encryption-multi-region
file-type-iconebs-encryption
file-type-iconfail2ban
file-type-icongithub-actions-iam-role
file-type-iconguardduty-multi-region
file-type-iconguardduty
file-type-iconiam-access-analyzer-multi-region
file-type-iconiam-groups
file-type-iconiam-policies
file-type-iconiam-user-password-policy
file-type-iconiam-users
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf
file-type-iconip-lockdown
file-type-iconkms-cmk-replica
file-type-iconkms-grant-multi-region
file-type-iconkms-master-key-multi-region
file-type-iconkms-master-key
file-type-iconntp
file-type-iconos-hardening
file-type-iconprivate-s3-bucket
file-type-iconsaml-iam-roles
file-type-iconsecrets-manager-resource-policies
file-type-iconssh-grunt-selinux-policy
file-type-iconssh-grunt
file-type-iconssh-iam
file-type-iconssm-healthchecks-iam-permissions
file-type-icontls-cert-private
file-type-icontest
file-type-icon.editorconfig
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc
file-type-iconterraform-cloud-enterprise-private-module-...

Browse the Repo

file-type-icon.circleci
file-type-icon.github
file-type-icon.patcher
file-type-icon_ci
file-type-icon_docs
file-type-iconcodegen
file-type-iconexamples
file-type-iconmodules
file-type-icon_deprecated
file-type-iconauto-update
file-type-iconaws-auth
file-type-iconaws-config-bucket
file-type-iconaws-config-multi-region
file-type-iconaws-config-rules
file-type-iconaws-config
file-type-iconaws-organizations
file-type-iconcloudtrail-bucket
file-type-iconcloudtrail
file-type-iconcross-account-iam-roles
file-type-iconcustom-iam-entity
file-type-iconebs-encryption-multi-region
file-type-iconebs-encryption
file-type-iconfail2ban
file-type-icongithub-actions-iam-role
file-type-iconguardduty-multi-region
file-type-iconguardduty
file-type-iconiam-access-analyzer-multi-region
file-type-iconiam-groups
file-type-iconiam-policies
file-type-iconiam-user-password-policy
file-type-iconiam-users
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf
file-type-iconip-lockdown
file-type-iconkms-cmk-replica
file-type-iconkms-grant-multi-region
file-type-iconkms-master-key-multi-region
file-type-iconkms-master-key
file-type-iconntp
file-type-iconos-hardening
file-type-iconprivate-s3-bucket
file-type-iconsaml-iam-roles
file-type-iconsecrets-manager-resource-policies
file-type-iconssh-grunt-selinux-policy
file-type-iconssh-grunt
file-type-iconssh-iam
file-type-iconssm-healthchecks-iam-permissions
file-type-icontls-cert-private
file-type-icontest
file-type-icon.editorconfig
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc
file-type-iconterraform-cloud-enterprise-private-module-...
IAM Users

IAM Users

Create and manage IAM users, IAM groups, IAM roles, and IAM policies as code.

Code Preview

Preview the Code

mobile file icon

README.md

down

IAM Users

This is a Terraform module you can use to create and manage IAM users as code.

How do you use this module?

This module allows you to pass in a map of users to create, where the keys in the map are the user names, and the values are properties for that IAM user (all optional). For documentation on the supported properties see variables.tf.

Check out the iam-users example for working sample code.

How do you generate passwords and access keys with this module?

This module can optionally create a password for AWS Web Console access and/or access keys for programmatic access for each IAM user if you set create_login_profile and/or create_access_keys to true for that IAM user, respectively.

To avoid having these secrets stored in plain text in Terraform state, this module will only generate the password or access keys if you specify the pgp_key param for that user. This param can contain either the base-64 encoded PGP public key for that user or the user's Keybase username in the format keybase:<USERNAME>.

We recommend using Keybase, as it makes it easier to manage PGP keys. Have each user at your company:

  1. Install the Keybase app.
  2. Claim a Keybase username.
  3. Use the Keybase app to create a PGP key and add it to their profile.
  4. Send you their username.

Once you have their user name, set pgp_key = "keybase:<USERNAME>" and create_login_profile and/or create_access_keys to true for that user, and this module will generate the password and/or access keys, and export them in the output variables user_passwords and user_access_keys. The output will look something like this:

user_access_keys = {
  "alice" = {
    "access_key_id" = "AKIARIUU2OIYE2APGOYK"
    "secret_access_key" = "wcBMA7E6Kn/t1YPfAQgAjLvUWUES/GeLHr/=="
  }
}
user_passwords = {
  "bob" = "wcBMA7E6Kn/t1YPfAQgAdByWFftehuD3uw="
}

You can see that Alice's secret_access_key and Bob's password are encrypted, so you can safely mail those credentials to each user.

How do I use PGP keys directly?

The module also supports using PGP keys directly, without keybase. To use a raw PGP key, export the public key in binary format and base64 encode it:

# Set KEYID to the ID of the PGP key you would like to use for encrypting
gpg --export $KEYID | base64

You can then set the pgp_key field to the resulting string.

How do you decrypt the generated passwords and access keys?

To decrypt a user's password or access keys, that user can decrypt them on the command-line as follows:

echo "<SECRET>" | base64 --decode | keybase pgp decrypt

Note that this only works if the user has the private key for their PGP key on their local computer (which they will if they used the Keybase app to create the PGP key in the first place).

If you used a raw PGP key, you can use the following command instead:

# Set KEYID to the ID of the PGP key you used for encrypting
echo "<SECRET>" | base64 --decode | gpg -d $KEYID

How do you delete users?

Under the hood, this module uses the aws_iam_user resource, which only allows you to delete an IAM user by doing the following multi-step process:

  1. Set the force_destroy parameter to true on that user. Example:

    module "iam_users" {
      source = "git::git@github.com:gruntwork-io/terraform-aws-security.git//modules/iam-users?ref=v0.53.2"
    
      users = {
        # Set force_destroy to true on the user you want to delete
        "example-user-to-delete" = {
          force_destroy = true
        }
        
        "some-other-user"  = {}
        "yet-another-user" = {}
      }
    }    
    
  2. Run apply.

  3. Remove the user you wish to delete from the users input variable. Following on the previous example:

    module "iam_users" {
      source = "git::git@github.com:gruntwork-io/terraform-aws-security.git//modules/iam-users?ref=v0.53.2"
    
      users = {
        # Note that example-user-to-delete has been removed
        
        "some-other-user"  = {}
        "yet-another-user" = {}
      }
    }    
    
  4. Run apply again.

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?