由于 Terraform 无法直接实现这一点,与 matusko 解决方案相反,我建议使用 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
}
来源
示例
基于以下技术的简单项目:
- Terraform,
- Cognito,
- Elastic Load Balancer,
- Auto Scaling Group,
- Spring Boot 应用程序
- PostgreSQL 数据库。
在 ELB 和 Spring Boot 上进行安全检查。
这意味着 ELB 无法将未经授权的用户传递给应用程序。 并且应用程序可以根据映射到 Cognito 角色的 PostgreSQL 角色执行进一步的安全检查。
Terraform 项目和简单应用程序:
有关如何在 terraform git 存储库的 README.MD 中运行它的更多信息。