Skip to content

Commit

Permalink
Fix sftp users, add custom domain tag and more improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Asmar committed Feb 15, 2024
1 parent 8d97acb commit e9b44a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
27 changes: 17 additions & 10 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ data "aws_iam_policy_document" "assume_role_policy" {
resource "aws_iam_role" "s3_access_for_sftp_users" {
for_each = var.enabled ? local.user_names_map : {}

name = module.labels.id
name = "${module.labels.id}-${each.value.user_name}"
assume_role_policy = join("", data.aws_iam_policy_document.assume_role_policy[*].json)
managed_policy_arns = [aws_iam_policy.s3_access_for_sftp_users[each.value.user_name].arn]
}

resource "aws_iam_policy" "s3_access_for_sftp_users" {
for_each = var.enabled ? local.user_names_map : {}

name = module.labels.id
name = "${module.labels.id}-${each.value.user_name}"
policy = data.aws_iam_policy_document.s3_access_for_sftp_users[each.value.user_name].json

tags = module.labels.tags
Expand All @@ -149,7 +149,7 @@ resource "aws_iam_policy" "s3_access_for_sftp_users" {
resource "aws_iam_policy" "logging" {
count = var.enabled ? 1 : 0

name = module.labels.id
name = "${module.labels.id}-logging"
policy = join("", data.aws_iam_policy_document.logging[*].json)

tags = module.labels.tags
Expand All @@ -158,7 +158,7 @@ resource "aws_iam_policy" "logging" {
resource "aws_iam_role" "logging" {
count = var.enabled ? 1 : 0

name = module.labels.id
name = "${module.labels.id}-logging"
assume_role_policy = join("", data.aws_iam_policy_document.assume_role_policy[*].json)
managed_policy_arns = [join("", aws_iam_policy.logging[*].arn)]

Expand All @@ -171,7 +171,7 @@ resource "aws_iam_role" "logging" {
##----------------------------------------------------------------------------------

resource "aws_transfer_server" "transfer_server" {
count = var.enable_sftp ? 1 : 0
count = var.enabled ? 1 : 0
identity_provider_type = var.identity_provider_type
protocols = ["SFTP"]
domain = var.domain
Expand Down Expand Up @@ -240,10 +240,10 @@ resource "aws_transfer_user" "transfer_server_user" {
##----------------------------------------------------------------------------------

resource "aws_transfer_ssh_key" "transfer_server_ssh_key" {
count = var.enabled ? length(var.sftp_users) : 0
for_each = var.enabled ? { for user in var.sftp_users : user.user_name => user } : {}
server_id = join("", aws_transfer_server.transfer_server[*].id)
user_name = aws_transfer_user.transfer_server_user[count.index].user_name
body = aws_transfer_user.transfer_server_user[count.index].public_key
user_name = aws_transfer_user.transfer_server_user[each.value.user_name].user_name
body = each.value.public_key
}


Expand All @@ -263,10 +263,17 @@ resource "aws_eip" "sftp" {
# Description : Provides a Custom Domain
##----------------------------------------------------------------------------------

resource "aws_transfer_tag" "custom_hostname" {
count = var.enabled && length(var.custom_domain) > 0 ? 1 : 0
resource_arn = aws_transfer_server.transfer_server[0].arn
key = "aws:transfer:customHostname"
value = var.custom_domain
}

resource "aws_route53_record" "custom_domain" {
count = var.enabled && length(var.domain_name) > 0 && length(var.zone_id) > 0 ? 1 : 0
count = var.enabled && length(var.custom_domain) > 0 && length(var.zone_id) > 0 ? 1 : 0

name = var.domain_name
name = var.custom_domain
zone_id = var.zone_id
type = "CNAME"
ttl = "300"
Expand Down
10 changes: 3 additions & 7 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ variable "enabled" {
#Module : SFTP
#Description : Terraform sftp module variables.
##----------------------------------------------------------------------------------
variable "enable_sftp" {
type = bool
default = true
description = "Set to false to prevent the module from creating any resources."
}

variable "identity_provider_type" {
type = string
Expand Down Expand Up @@ -125,9 +120,9 @@ variable "retention_in_days" {
default = 3
}

variable "domain_name" {
variable "custom_domain" {
type = string
description = "Domain to use when connecting to the SFTP endpoint"
description = "Custom domain to use when connecting to the SFTP endpoint"
default = ""
}

Expand All @@ -151,6 +146,7 @@ variable "workflow_details" {
})
})
description = "Workflow details for triggering the execution on file upload."
default = null
}

variable "enable_workflow" {
Expand Down

0 comments on commit e9b44a3

Please sign in to comment.