由于与 matusko 解决方案相反,无法直接通过 Terraform 执行此操作,因此我建议使用 CloudFormation 模板。
在我看来它更优雅,因为:
- 它不需要在本地安装其他应用程序
- 它可以由 terraform 管理,因为 CF 堆栈可以由 terraform 销毁
使用模板的简单解决方案如下所示。请记住,我跳过了与provider 等不直接相关的文件和资源。示例还包含将用户加入群组。
variables.tf
variable "COGITO_USERS_MAIL" {
type = string
description = "On this mail passwords for example users will be sent. It is only method I know for receiving password after automatic user creation."
}
cf_template.json
{
"Resources" : {
"userFoo": {
"Type" : "AWS::Cognito::UserPoolUser",
"Properties" : {
"UserAttributes" : [
{ "Name": "email", "Value": "${users_mail}"}
],
"Username" : "foo",
"UserPoolId" : "${user_pool_id}"
}
},
"groupFooAdmin": {
"Type" : "AWS::Cognito::UserPoolUserToGroupAttachment",
"Properties" : {
"GroupName" : "${user_pool_group_admin}",
"Username" : "foo",
"UserPoolId" : "${user_pool_id}"
},
"DependsOn" : "userFoo"
}
}
}
cognito.tf
resource "aws_cognito_user_pool" "user_pool" {
name = "cogito-user-pool-name"
}
resource "aws_cognito_user_pool_domain" "user_pool_domain" {
domain = "somedomain"
user_pool_id = aws_cognito_user_pool.user_pool.id
}
resource "aws_cognito_user_group" "admin" {
name = "admin"
user_pool_id = aws_cognito_user_pool.user_pool.id
}
user_init.tf
data "template_file" "application_bootstrap" {
template = file("${path.module}/cf_template.json")
vars = {
user_pool_id = aws_cognito_user_pool.user_pool.id
users_mail = var.COGNITO_USERS_MAIL
user_pool_group_admin = aws_cognito_user_group.admin.name
}
}
resource "aws_cloudformation_stack" "test_users" {
name = "${var.TAG_PROJECT}-test-users"
template_body = data.template_file.application_bootstrap.rendered
}
来源
示例
简单项目基于:
- 地形,
- 认知,
- 弹性负载均衡器,
- Auto Scaling 组,
- Spring Boot 应用程序
- PostgreSQL 数据库。
在 ELB 和 Spring Boot 上进行安全检查。
这意味着 ELB 不能将未经授权的用户传递给应用程序。并且应用程序可以基于映射到 Cognito 角色的 PostgreSQL 角色做进一步的安全检查。
Terraform 项目和简单应用:
有关如何在 terraform git 存储库的 README.MD 中运行它的更多信息。