Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a AWS Cognito user with Terraform

I'd like to use Terraform to create AWS Cognito User Pool with one test user. Creating a user pool is quite straightforward:

resource "aws_cognito_user_pool" "users" {
  name = "${var.cognito_user_pool_name}"
  admin_create_user_config {
    allow_admin_create_user_only = true
    unused_account_validity_days = 7
  }
}

However, I cannot find a resource that creates AWS Cognito user. It is doable with AWS Cli

aws cognito-idp admin-create-user --user-pool-id <value> --username <value>

Any idea on how to do it with Terraform?

like image 899
Daniel Avatar asked Mar 10 '19 12:03

Daniel


2 Answers

It should be noted that the aws_cognito_user resource is now supported in the AWS Terraform provider, as documented here: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cognito_user

Version 4.3.0 at time of writing.

like image 104
Ciaran Evans Avatar answered Nov 12 '22 17:11

Ciaran Evans


In order to automate things, it can be done in terraform using a null_resource and local_exec provisioner to execute your aws cli command

e.g.

resource "aws_cognito_user_pool" "pool" {
  name = "mypool"
}

resource "null_resource" "cognito_user" {

  triggers = {
    user_pool_id = aws_cognito_user_pool.pool.id
  }

  provisioner "local-exec" {
    command = "aws cognito-idp admin-create-user --user-pool-id ${aws_cognito_user_pool.pool.id} --username myuser"
  }
}
like image 44
matusko Avatar answered Nov 12 '22 16:11

matusko