From 91b93ebefd795479422878eded8a9a925ea78580 Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Sun, 9 Aug 2026 17:11:56 +0900 Subject: [PATCH 1/5] sfn-manage-container-task-tf: Update AWS Provider to v6 --- sfn-manage-container-task-tf/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sfn-manage-container-task-tf/main.tf b/sfn-manage-container-task-tf/main.tf index 2ccc2152ab..daa1858385 100644 --- a/sfn-manage-container-task-tf/main.tf +++ b/sfn-manage-container-task-tf/main.tf @@ -2,7 +2,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = "~>4.0" + version = "~> 6.0" } } } @@ -212,4 +212,4 @@ resource "aws_sfn_state_machine" "sfn_container_task" { subnet_a = module.vpc.public_subnet_1_id, subnet_b = module.vpc.public_subnet_2_id }) -} \ No newline at end of file +} From 45cfbf1ccde51c91111b9df068068d39a5ade5a5 Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Sun, 9 Aug 2026 17:13:17 +0900 Subject: [PATCH 2/5] sfn-manage-container-task-tf: terraform fmt --- sfn-manage-container-task-tf/main.tf | 120 +++++++++++----------- sfn-manage-container-task-tf/variables.tf | 24 ++--- sfn-manage-container-task-tf/vpc/main.tf | 12 +-- 3 files changed, 78 insertions(+), 78 deletions(-) diff --git a/sfn-manage-container-task-tf/main.tf b/sfn-manage-container-task-tf/main.tf index daa1858385..7549590823 100644 --- a/sfn-manage-container-task-tf/main.tf +++ b/sfn-manage-container-task-tf/main.tf @@ -8,7 +8,7 @@ terraform { } provider "aws" { - region = var.region + region = var.region } locals { @@ -21,9 +21,9 @@ data "aws_partition" "partition" {} resource "random_string" "random" { - length = 16 - special = false - upper = false + length = 16 + special = false + upper = false } # VPC @@ -32,10 +32,10 @@ resource "random_string" "random" { module "vpc" { source = "./vpc" - vpc_name = "example-vpc" - cidr_block = "10.0.0.0/16" + vpc_name = "example-vpc" + cidr_block = "10.0.0.0/16" public_subnet_cidr_blocks = ["10.0.1.0/24", "10.0.2.0/24"] - availability_zones = ["us-east-1a", "us-east-1b"] + availability_zones = ["us-east-1a", "us-east-1b"] } # Security Group @@ -87,15 +87,15 @@ resource "aws_ecs_task_definition" "fargate_task" { memory = 512 execution_role_arn = aws_iam_role.ecs_task_execution_role.arn requires_compatibilities = ["FARGATE"] - container_definitions = jsonencode( + container_definitions = jsonencode( [ { "cpu" : 256, "image" : "nginx", "memory" : 512, "name" : "my_container", - "command": ["echo", "Task succeeded!"], - "essential": true, + "command" : ["echo", "Task succeeded!"], + "essential" : true, "portMappings" : [ { "containerPort" : 80, @@ -103,7 +103,7 @@ resource "aws_ecs_task_definition" "fargate_task" { } ] } - ]) + ]) } @@ -152,49 +152,49 @@ resource "aws_iam_policy" "sfn_container_task_policy" { name = "sfn-conteiner-task-policy-${random_string.random.result}" policy = jsonencode({ - "Version": "2012-10-17", - "Statement": [ - { - "Action": [ - "sns:Publish" - ], - "Resource": [ - aws_sns_topic.sns_topic.arn - ], - "Effect": "Allow" - }, - { - "Action": [ - "ecs:RunTask" - ], - "Resource": [ - aws_ecs_task_definition.fargate_task.arn - ], - "Effect": "Allow" - }, - { - "Action": [ - "ecs:StopTask", - "ecs:DescribeTasks" - ], - "Resource": "*", - "Effect": "Allow" - }, - { - "Action": [ - "events:PutTargets", - "events:PutRule", - "events:DescribeRule", - "iam:PassRole" - ], - "Resource": [ - "*" - ], - "Effect": "Allow" - } + "Version" : "2012-10-17", + "Statement" : [ + { + "Action" : [ + "sns:Publish" + ], + "Resource" : [ + aws_sns_topic.sns_topic.arn + ], + "Effect" : "Allow" + }, + { + "Action" : [ + "ecs:RunTask" + ], + "Resource" : [ + aws_ecs_task_definition.fargate_task.arn + ], + "Effect" : "Allow" + }, + { + "Action" : [ + "ecs:StopTask", + "ecs:DescribeTasks" + ], + "Resource" : "*", + "Effect" : "Allow" + }, + { + "Action" : [ + "events:PutTargets", + "events:PutRule", + "events:DescribeRule", + "iam:PassRole" + ], + "Resource" : [ + "*" + ], + "Effect" : "Allow" + } ] -} - ) + } + ) } resource "aws_iam_role_policy_attachment" "state_machine_custom_policy_attachment" { @@ -203,13 +203,13 @@ resource "aws_iam_role_policy_attachment" "state_machine_custom_policy_attachmen } resource "aws_sfn_state_machine" "sfn_container_task" { - name = "state-machine-container-task-${random_string.random.result}" - role_arn = aws_iam_role.sfn_container_task_role.arn + name = "state-machine-container-task-${random_string.random.result}" + role_arn = aws_iam_role.sfn_container_task_role.arn definition = templatefile("${path.module}/statemachine/statemachine.asl.json", { - sns_topic = aws_sns_topic.sns_topic.arn, - ecs_cluster = aws_ecs_cluster.fargate_cluster.arn, - task_definition = aws_ecs_task_definition.fargate_task.arn, - subnet_a = module.vpc.public_subnet_1_id, - subnet_b = module.vpc.public_subnet_2_id + sns_topic = aws_sns_topic.sns_topic.arn, + ecs_cluster = aws_ecs_cluster.fargate_cluster.arn, + task_definition = aws_ecs_task_definition.fargate_task.arn, + subnet_a = module.vpc.public_subnet_1_id, + subnet_b = module.vpc.public_subnet_2_id }) } diff --git a/sfn-manage-container-task-tf/variables.tf b/sfn-manage-container-task-tf/variables.tf index f8dce5c15a..45d8938bc5 100644 --- a/sfn-manage-container-task-tf/variables.tf +++ b/sfn-manage-container-task-tf/variables.tf @@ -1,25 +1,25 @@ variable "region" { - type=string - description = "AWS Region where deploying resources" - default = "us-east-1" + type = string + description = "AWS Region where deploying resources" + default = "us-east-1" } variable "aws_profile_name" { - type=string - description = "AWS CLI credentials profile name" - default="default" + type = string + description = "AWS CLI credentials profile name" + default = "default" } variable "vpc_cidr" { - type=string - description = "CIDR block for Batch VPC" - default = "10.0.0.0/16" + type = string + description = "CIDR block for Batch VPC" + default = "10.0.0.0/16" } variable "subnet_cidr" { - type=string - description = "CIDR block for the Batch subnet" - default = "10.0.0.0/24" + type = string + description = "CIDR block for the Batch subnet" + default = "10.0.0.0/24" } variable "remote_cidr_blocks" { diff --git a/sfn-manage-container-task-tf/vpc/main.tf b/sfn-manage-container-task-tf/vpc/main.tf index ea4121617c..065af1d39f 100644 --- a/sfn-manage-container-task-tf/vpc/main.tf +++ b/sfn-manage-container-task-tf/vpc/main.tf @@ -22,12 +22,12 @@ resource "aws_internet_gateway" "main" { resource "aws_subnet" "public" { count = 2 - vpc_id = aws_vpc.main.id - cidr_block = var.public_subnet_cidr_blocks[count.index] + vpc_id = aws_vpc.main.id + cidr_block = var.public_subnet_cidr_blocks[count.index] availability_zone = var.availability_zones[count.index] tags = { - Name = "${var.vpc_name}-public-${count.index+1}" + Name = "${var.vpc_name}-public-${count.index + 1}" } } @@ -42,16 +42,16 @@ resource "aws_route_table" "public" { # Create route for public subnets to internet gateway resource "aws_route" "public_igw" { - route_table_id = aws_route_table.public.id + route_table_id = aws_route_table.public.id destination_cidr_block = "0.0.0.0/0" - gateway_id = aws_internet_gateway.main.id + gateway_id = aws_internet_gateway.main.id } # Associate public subnets with public route table resource "aws_route_table_association" "public" { count = 2 - subnet_id = aws_subnet.public[count.index].id + subnet_id = aws_subnet.public[count.index].id route_table_id = aws_route_table.public.id } From 730f6997bc3baffe551646b82f17df5d8fdad0bf Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Sun, 9 Aug 2026 17:26:41 +0900 Subject: [PATCH 3/5] sfn-manage-container-task-tf: Remove leftover AWS Batch references --- sfn-manage-container-task-tf/README.md | 4 ++-- sfn-manage-container-task-tf/main.tf | 4 ++-- sfn-manage-container-task-tf/variables.tf | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sfn-manage-container-task-tf/README.md b/sfn-manage-container-task-tf/README.md index 2b3b61cf22..18f22ec7fe 100644 --- a/sfn-manage-container-task-tf/README.md +++ b/sfn-manage-container-task-tf/README.md @@ -38,9 +38,9 @@ Important: this application uses various AWS services and there are costs associ ## How it works -This sample project demonstrates how to submit an AWS Batch job, and then send an Amazon SNS notification based on whether that job succeeds or fails. Deploying this sample project creates an AWS Step Functions state machine, an AWS Batch job, and an Amazon SNS topic. +This sample project demonstrates how to run an AWS Fargate task, and then send an Amazon SNS notification based on whether that task succeeds or fails. -In this project, Step Functions uses a state machine to call the AWS Batch job synchronously. It then waits for the job to succeed or fail, and it sends an Amazon SNS topic with a message about whether the job succeeded or failed. +The state machine runs the Fargate task synchronously and waits for it to complete. The task simply runs an `echo` command in a container and exits. Based on the result, the state machine publishes a message to the Amazon SNS topic to notify whether the task succeeded or failed. ## Image diff --git a/sfn-manage-container-task-tf/main.tf b/sfn-manage-container-task-tf/main.tf index 7549590823..f13cf11a9a 100644 --- a/sfn-manage-container-task-tf/main.tf +++ b/sfn-manage-container-task-tf/main.tf @@ -132,7 +132,7 @@ resource "aws_sns_topic" "sns_topic" { #SFN resource "aws_iam_role" "sfn_container_task_role" { - name = "start-batch-job-${random_string.random.result}" + name = "sfn-container-task-role-${random_string.random.result}" assume_role_policy = jsonencode({ Version = "2012-10-17" @@ -149,7 +149,7 @@ resource "aws_iam_role" "sfn_container_task_role" { } resource "aws_iam_policy" "sfn_container_task_policy" { - name = "sfn-conteiner-task-policy-${random_string.random.result}" + name = "sfn-container-task-policy-${random_string.random.result}" policy = jsonencode({ "Version" : "2012-10-17", diff --git a/sfn-manage-container-task-tf/variables.tf b/sfn-manage-container-task-tf/variables.tf index 45d8938bc5..ea22ec713e 100644 --- a/sfn-manage-container-task-tf/variables.tf +++ b/sfn-manage-container-task-tf/variables.tf @@ -12,13 +12,13 @@ variable "aws_profile_name" { variable "vpc_cidr" { type = string - description = "CIDR block for Batch VPC" + description = "CIDR block for the VPC" default = "10.0.0.0/16" } variable "subnet_cidr" { type = string - description = "CIDR block for the Batch subnet" + description = "CIDR block for the subnet" default = "10.0.0.0/24" } From f0cdfbd444bc0aded95c6845ae1df68f02a7d7c6 Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Sun, 9 Aug 2026 17:28:12 +0900 Subject: [PATCH 4/5] sfn-manage-container-task-tf: Remove unnecessary ECS service --- sfn-manage-container-task-tf/main.tf | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/sfn-manage-container-task-tf/main.tf b/sfn-manage-container-task-tf/main.tf index f13cf11a9a..5795d77b6c 100644 --- a/sfn-manage-container-task-tf/main.tf +++ b/sfn-manage-container-task-tf/main.tf @@ -112,19 +112,6 @@ resource "aws_ecs_cluster" "fargate_cluster" { name = "FargateTaskNotification-${random_string.random.result}" } -resource "aws_ecs_service" "example" { - name = "example-service" - cluster = aws_ecs_cluster.fargate_cluster.id - task_definition = aws_ecs_task_definition.fargate_task.arn - launch_type = "FARGATE" - desired_count = 1 - - network_configuration { - security_groups = [aws_security_group.fargate_sg.id] - subnets = [module.vpc.public_subnet_1_id, module.vpc.public_subnet_2_id] - } -} - # Define SNS topic resource "aws_sns_topic" "sns_topic" { name = "FargateTaskNotification-${random_string.random.result}" From 2c41c4e38340355ac4a2b98fca4b59fab802da0e Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Sun, 9 Aug 2026 17:32:14 +0900 Subject: [PATCH 5/5] sfn-manage-container-task-tf: Fix README --- sfn-manage-container-task-tf/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sfn-manage-container-task-tf/README.md b/sfn-manage-container-task-tf/README.md index 18f22ec7fe..91a572a0f7 100644 --- a/sfn-manage-container-task-tf/README.md +++ b/sfn-manage-container-task-tf/README.md @@ -5,7 +5,7 @@ This sample project demonstrates how to run an AWS Fargate task, and then send a In this project, Step Functions uses a state machine to call the Fargate task synchronously. It then waits for the task to succeed or fail, and it sends an Amazon SNS topic with a message about whether the job succeeded or failed. * A Fargate task * An Amazon SNS topic -* AWS Step Function +* AWS Step Functions * Related AWS Identity and Access Management (IAM) roles Learn more about this workflow at Step Functions workflows collection: https://docs.aws.amazon.com/step-functions/latest/dg/sample-project-container-task-notification.html @@ -17,17 +17,17 @@ Important: this application uses various AWS services and there are costs associ * [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. * [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) -* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed +* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) with version 1.x installed (this pattern has been tested with version 1.15) ## Deployment Instructions 1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: ``` - git clone https://github.com/aws-samples/step-functions-workflows-collection + git clone https://github.com/aws-samples/serverless-patterns ``` 1. Change directory to the pattern directory: ``` - cd sfn-manage-container-task-tf + cd serverless-patterns/sfn-manage-container-task-tf ``` 1. From the command line, use Terraform to deploy the AWS resources for the workflow as specified in the ```main.tf``` file: ```