Skip to content

Commit ce4504c

Browse files
committed
Add IAM role and policy for API task; update S3 bucket output URL and container images
1 parent a683872 commit ce4504c

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

terraform/apps/playground/app_stack/main.tf

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,45 @@ module "app_s3" {
7777
cloudfront_price_class = var.app_s3_cloudfront_price_class
7878
}
7979

80+
data "aws_iam_policy_document" "api_task_assume" {
81+
statement {
82+
actions = ["sts:AssumeRole"]
83+
84+
principals {
85+
type = "Service"
86+
identifiers = ["ecs-tasks.amazonaws.com"]
87+
}
88+
}
89+
}
90+
91+
data "aws_iam_policy_document" "api_task_s3" {
92+
statement {
93+
sid = "AllowBucketReadWrite"
94+
actions = [
95+
"s3:PutObject",
96+
"s3:DeleteObject",
97+
"s3:GetObject",
98+
"s3:ListBucket"
99+
]
100+
resources = [
101+
"arn:aws:s3:::${var.app_s3_bucket_name}",
102+
"arn:aws:s3:::${var.app_s3_bucket_name}/*"
103+
]
104+
}
105+
}
106+
107+
resource "aws_iam_role" "api_task" {
108+
name = "${var.environment}-api-task"
109+
assume_role_policy = data.aws_iam_policy_document.api_task_assume.json
110+
tags = local.common_tags
111+
}
112+
113+
resource "aws_iam_role_policy" "api_task_s3" {
114+
name = "${var.environment}-api-task-s3"
115+
role = aws_iam_role.api_task.id
116+
policy = data.aws_iam_policy_document.api_task_s3.json
117+
}
118+
80119
module "rds" {
81120
source = "../../../modules/rds_postgres"
82121

@@ -133,6 +172,8 @@ module "api_service" {
133172

134173
health_check_path = "/health/live"
135174

175+
task_role_arn = aws_iam_role.api_task.arn
176+
136177
environment_variables = {
137178
ASPNETCORE_ENVIRONMENT = local.aspnetcore_environment
138179
DatabaseOptions__ConnectionString = local.db_connection_string
@@ -141,7 +182,6 @@ module "api_service" {
141182
CorsOptions__AllowedOrigins__0 = "http://${module.alb.dns_name}"
142183
Storage__Provider = "s3"
143184
Storage__S3__Bucket = var.app_s3_bucket_name
144-
Storage__S3__PublicRead = false
145185
Storage__S3__PublicBaseUrl = module.app_s3.cloudfront_domain_name != "" ? "https://${module.app_s3.cloudfront_domain_name}" : ""
146186
}
147187

terraform/apps/playground/envs/dev/us-east-1/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ output "s3_bucket_name" {
6969
}
7070

7171
output "s3_cloudfront_domain" {
72-
value = module.app.s3_cloudfront_domain
72+
value = "https://${module.app.s3_cloudfront_domain}"
7373
}

terraform/apps/playground/envs/dev/us-east-1/terraform.tfvars

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ db_name = "fshdb"
3333
db_username = "fshadmin"
3434
db_password = "password123!" # Note: In production, use a more secure method for managing secrets.
3535

36-
api_container_image = "ghcr.io/fullstackhero/fsh-playground-api:1c555545cee10cb9703f5ecbbb928e45e5ba8990"
36+
api_container_image = "ghcr.io/fullstackhero/fsh-playground-api:a6838728a6314c4a635d732e90f8c51c5f890732"
3737
api_container_port = 8080
3838
api_cpu = "256"
3939
api_memory = "512"
4040
api_desired_count = 1
4141

42-
blazor_container_image = "ghcr.io/fullstackhero/fsh-playground-blazor:1c555545cee10cb9703f5ecbbb928e45e5ba8990"
42+
blazor_container_image = "ghcr.io/fullstackhero/fsh-playground-blazor:a6838728a6314c4a635d732e90f8c51c5f890732"
4343
blazor_container_port = 8080
4444
blazor_cpu = "256"
4545
blazor_memory = "512"

terraform/modules/ecs_service/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ resource "aws_ecs_task_definition" "this" {
100100
network_mode = "awsvpc"
101101
requires_compatibilities = ["FARGATE"]
102102
execution_role_arn = aws_iam_role.task_execution.arn
103+
task_role_arn = var.task_role_arn
103104

104105
container_definitions = jsonencode([
105106
{

terraform/modules/ecs_service/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ variable "environment_variables" {
9494
default = {}
9595
}
9696

97+
variable "task_role_arn" {
98+
type = string
99+
description = "Optional task role ARN to attach to the task definition."
100+
default = null
101+
}
102+
97103
variable "tags" {
98104
type = map(string)
99105
description = "Tags to apply to resources."

0 commit comments

Comments
 (0)