Skip to content

Commit

Permalink
add an ECS task role to send email using SES and make SMTP vars optional
Browse files Browse the repository at this point in the history
  • Loading branch information
briskt committed Oct 11, 2023
1 parent 20d2c4a commit f41476e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docker-compose/email/local.env.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
MAILER_USEFILES=
MAILER_HOST=
MAILER_USERNAME=
Expand Down
7 changes: 4 additions & 3 deletions terraform/031-email-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This module is used to create an ECS service running email-service.
- Create task definition and ECS service for email-service API
- Create task definition and ECS service for email-service cron
- Create Cloudflare DNS record
- Create ECS task role to send email via SES

## Required Inputs

Expand All @@ -23,9 +24,6 @@ This module is used to create an ECS service running email-service.
- `idp_name` - Short name of IdP for use in logs and email alerts
- `internal_alb_dns_name` - DNS name for the IdP-in-a-Box's internal Application Load Balancer
- `internal_alb_listener_arn` - ARN for the IdP-in-a-Box's internal ALB's listener
- `mailer_host` - SMTP hostname
- `mailer_password` - SMTP password
- `mailer_username` - SMTP username
- `mysql_host` - Address for RDS instance
- `mysql_pass` - MySQL password for email-service
- `mysql_user` - MySQL username for email-service
Expand All @@ -44,6 +42,9 @@ This module is used to create an ECS service running email-service.
- `email_queue_batch_size` - How many queued emails to process per run
- `enable_cron` - Set to false to disable the cron instance
- `from_name` - Name to use when sending emails
- `mailer_host` - SMTP hostname
- `mailer_password` - SMTP password
- `mailer_username` - SMTP username
- `mailer_usefiles` - Whether or not YiiMailer should write to files instead of sending emails
- `memory_api` - Memory (RAM) resources to allot to each API instance
- `memory_cron` - Memory (RAM) resources to allot to the cron instance
Expand Down
48 changes: 48 additions & 0 deletions terraform/031-email-service/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,52 @@ resource "random_id" "access_token_idsync" {
byte_length = 16
}

/*
* Create role for access to SES
*/
resource "aws_iam_role" "ses" {
name = "ses-${var.idp_name}-${var.app_name}-${var.app_env}-${var.aws_region}"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "ECSAssumeRoleSES"
Effect = "Allow"
Principal = {
Service = [
"ses.amazonaws.com",
"ecs-tasks.amazonaws.com",
]
}
Action = "sts:AssumeRole"
}
]
})
}

resource "aws_iam_role_policy" "ses" {
name = "ses"
role = aws_iam_role.ses.id
policy = jsonencode(
{
Version = "2012-10-17"
Statement = [
{
Sid = "SendEmail"
Effect = "Allow"
Action = "ses:SendEmail"
Resource = "*"
Condition = {
StringEquals = {
"ses:FromAddress" = var.from_email
}
}
}
]
})
}

/*
* Create ECS services
*/
Expand Down Expand Up @@ -92,6 +138,7 @@ module "ecsservice_api" {
container_def_json = local.task_def_api
desired_count = var.desired_count_api
tg_arn = aws_alb_target_group.email.arn
task_role_arn = aws_iam_role.ses.arn
lb_container_name = "api"
lb_container_port = "80"
}
Expand Down Expand Up @@ -130,6 +177,7 @@ module "ecsservice_cron" {
service_name = "${var.idp_name}-${var.app_name}-cron"
service_env = var.app_env
container_def_json = local.task_def_cron
task_role_arn = aws_iam_role.ses.arn
desired_count = var.enable_cron ? 1 : 0
}

Expand Down
17 changes: 12 additions & 5 deletions terraform/031-email-service/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,27 @@ variable "internal_alb_listener_arn" {
}

variable "mailer_host" {
type = string
description = "SMTP hostname - if omitted, SES will be used"
type = string
default = ""
}

variable "mailer_password" {
type = string
description = "password, used with mailer_username for authentication to SMTP server"
type = string
default = ""
}

variable "mailer_usefiles" {
type = string
default = "false"
description = "Controls whether YiiMailer should write to files instead of sending emails"
type = string
default = "false"
}

variable "mailer_username" {
type = string
description = "username, used with mailer_password for authentication to SMTP server"
type = string
default = ""
}

variable "memory_api" {
Expand Down

0 comments on commit f41476e

Please sign in to comment.