Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions sfn-manage-container-task-tf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: This pattern use Terraform not AWS SAM!


## 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: It's wrong repository.

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:
```
Expand All @@ -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

Expand Down
141 changes: 64 additions & 77 deletions sfn-manage-container-task-tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>4.0"
version = "~> 6.0"
}
}
}

provider "aws" {
region = var.region
region = var.region
}

locals {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -87,23 +87,23 @@ 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,
"protocol" : "tcp"
}
]
}
])
])
}


Expand All @@ -112,27 +112,14 @@ 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}"
}

#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"
Expand All @@ -149,52 +136,52 @@ 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",
"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" {
Expand All @@ -203,13 +190,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
})
}
}
24 changes: 12 additions & 12 deletions sfn-manage-container-task-tf/variables.tf
Original file line number Diff line number Diff line change
@@ -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 the 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 subnet"
default = "10.0.0.0/24"
}

variable "remote_cidr_blocks" {
Expand Down
12 changes: 6 additions & 6 deletions sfn-manage-container-task-tf/vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
}

Expand All @@ -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
}

Expand Down