@@ -38,7 +38,7 @@ module "eventbridge" {
3838 append_rule_postfix = false
3939
4040 attach_ecs_policy = true
41- ecs_target_arns = [aws_ecs_task_definition . hello_world . arn ]
41+ ecs_target_arns = [module . ecs . services . hello-world . task_definition_arn ]
4242
4343 rules = {
4444 orders = {
@@ -114,11 +114,11 @@ module "eventbridge" {
114114 },
115115 {
116116 name = " process-email-with-ecs-task" ,
117- arn = module.ecs.ecs_cluster_arn ,
117+ arn = module.ecs.cluster_arn ,
118118 attach_role_arn = true
119119 ecs_target = {
120120 task_count = 1
121- task_definition_arn = aws_ecs_task_definition.hello_world.arn
121+ task_definition_arn = module.ecs.services.hello - world.task_definition_arn
122122 }
123123 }
124124 ]
@@ -229,6 +229,19 @@ locals {
229229 EOF
230230 }
231231}
232+ # ############################################################
233+ # Data sources to get VPC and default security group details
234+ # ############################################################
235+ data "aws_vpc" "default" {
236+ default = true
237+ }
238+
239+ data "aws_subnets" "default" {
240+ filter {
241+ name = " vpc-id"
242+ values = [data . aws_vpc . default . id ]
243+ }
244+ }
232245
233246# #################
234247# Extra resources
@@ -314,37 +327,36 @@ module "step_function" {
314327
315328module "ecs" {
316329 source = " terraform-aws-modules/ecs/aws"
317- version = " ~> 3.0"
318-
319- name = random_pet. this . id
320-
321- capacity_providers = [" FARGATE" , " FARGATE_SPOT" ]
322- }
323-
324- resource "aws_ecs_service" "hello_world" {
325- name = " hello_world-${ random_pet . this . id } "
326- cluster = module. ecs . ecs_cluster_id
327- task_definition = aws_ecs_task_definition. hello_world . arn
328-
329- desired_count = 1
330+ version = " ~> 6.0"
330331
331- deployment_maximum_percent = 100
332- deployment_minimum_healthy_percent = 0
333- }
332+ cluster_name = random_pet. this . id
334333
335- resource "aws_ecs_task_definition" "hello_world" {
336- family = " hello_world-${ random_pet . this . id } "
334+ default_capacity_provider_strategy = {
335+ FARGATE = {
336+ weight = 100
337+ base = 20
338+ }
339+ FARGATE_SPOT = {
340+ weight = 100
341+ }
342+ }
337343
338- container_definitions = << EOF
339- [
340- {
341- "name": "hello_world-${ random_pet . this . id } ",
342- "image": "hello-world",
343- "cpu": 0,
344- "memory": 128
344+ services = {
345+ hello-world = {
346+ subnet_ids = data.aws_subnets.default.ids
347+ desired_count = 1
348+ deployment_maximum_percent = 100
349+ deployment_minimum_healthy_percent = 0
350+
351+ container_definitions = {
352+ hello-world = {
353+ image = " hello-world" ,
354+ cpu = 0 ,
355+ memory = 128
356+ }
357+ }
358+ }
345359 }
346- ]
347- EOF
348360}
349361
350362# ############################################
@@ -357,7 +369,7 @@ module "lambda" {
357369
358370 function_name = " ${ random_pet . this . id } -lambda"
359371 handler = " index.lambda_handler"
360- runtime = " python3.12 "
372+ runtime = " python3.13 "
361373
362374 create_package = false
363375 local_existing_package = local. downloaded
@@ -432,84 +444,13 @@ resource "aws_cloudtrail" "trail" {
432444# s3
433445# ######
434446
435- data "aws_region" "current" {}
436-
437- data "aws_caller_identity" "current" {}
438-
439447module "bucket" {
440448 source = " terraform-aws-modules/s3-bucket/aws"
441449 version = " ~> 5.0"
442450
443- bucket = " ${ random_pet . this . id } -bucket"
444- attach_policy = true
445- policy = data. aws_iam_policy_document . bucket_policy . json
446-
447- force_destroy = true
448- }
449-
450- # https://docs.aws.amazon.com/awscloudtrail/latest/userguide/create-s3-bucket-policy-for-cloudtrail.html
451- data "aws_iam_policy_document" "bucket_policy" {
452- statement {
453- sid = " AWSCloudTrailAclCheck"
454- principals {
455- identifiers = [" cloudtrail.amazonaws.com" ]
456- type = " Service"
457- }
458- actions = [" s3:GetBucketAcl" ]
459- resources = [
460- " arn:aws:s3:::${ random_pet . this . id } -bucket"
461- ]
462- condition {
463- test = " StringEquals"
464- values = [" arn:aws:cloudtrail:${ data . aws_region . current . region } :${ data . aws_caller_identity . current . account_id } :trail/${ random_pet . this . id } -trail" ]
465- variable = " aws:SourceArn"
466- }
467- }
451+ bucket = " ${ random_pet . this . id } -bucket"
468452
469- statement {
470- sid = " AWSCloudTrailWrite"
471- principals {
472- identifiers = [" cloudtrail.amazonaws.com" ]
473- type = " Service"
474- }
475- actions = [" s3:PutObject" ]
476- resources = [
477- " arn:aws:s3:::${ random_pet . this . id } -bucket/*"
478- ]
479- condition {
480- test = " StringEquals"
481- values = [" bucket-owner-full-control" ]
482- variable = " s3:x-amz-acl"
483- }
484- condition {
485- test = " StringEquals"
486- values = [" arn:aws:cloudtrail:${ data . aws_region . current . region } :${ data . aws_caller_identity . current . account_id } :trail/${ random_pet . this . id } -trail" ]
487- variable = " aws:SourceArn"
488- }
489- }
453+ attach_cloudtrail_log_delivery_policy = true
490454
455+ force_destroy = true
491456}
492-
493- # ######
494- # # Lambda
495- # ######
496- # module "lambda" {
497- # source = "terraform-aws-modules/lambda/aws"
498- # version = "~> 8.0"
499- #
500- # function_name = "dev-cron-job"
501- # description = "Lambda Serverless Job"
502- # handler = "index.handler"
503- # runtime = "nodejs14.x"
504- # timeout = 900
505- #
506- # source_path = "../with-lambda-shceduling/lambda"
507- # }
508- #
509- # resource "aws_lambda_permission" "crons_invoke" {
510- # statement_id = "AllowExecutionFromCloudWatch"
511- # action = "lambda:InvokeFunction"
512- # function_name = module.lambda.lambda_function_name
513- # principal = "events.amazonaws.com"
514- # source_arn = module.eventbridge.eventbridge_rule_arns.orders
515- # }
0 commit comments