Processing s3 uploads with step functions

From mouadja02/skills · View interactive version

Processing s3 uploads with step functions is an AI agent skill from the GitHub repository mouadja02/skills. Deploy an event-driven workflow that routes S3 uploads to either Lambda or Fargate via Step Functions based on file size. Uses EventBridge to trigger a Step Functions state machine when objects are uploaded to S3. Small files are processed by Lambda, large files by a Fargate task. Includes VPC, ECR

How to use this skill

Skill file

Attribution: Sourced from aws/agent-toolkit-for-aws by Amazon Web Services, licensed Apache-2.0. Modified for this collection.

Step Functions Workflow: Route S3 Uploads to Lambda or Fargate

Overview

This skill deploys an event-driven workflow using AWS CLI. When a file is uploaded to

an S3 bucket, EventBridge triggers a Step Functions state machine. The state machine

checks the file size and routes processing to either a Lambda function (files ≤ 6 MB)

or a Fargate task (files > 6 MB).

The architecture includes:

Use this skill when:

Do not use this skill when:

Prerequisites

  1. AWS CLI v2 — Installed and configured. Verify with aws sts get-caller-identity.
  2. Python 3.12 — For the Lambda function runtime.
  3. Docker — For building and pushing the Fargate container image.

Parameters

Constraints for parameter acquisition:

Procedures

Step 0: Verify Dependencies

Constraints:

Step 1: Retrieve AWS Account ID

Constraints:

Step 2: Get the Default VPC and Networking

Constraints:

aws ec2 describe-vpcs --filters Name=isDefault,Values=true --query 'Vpcs[0].VpcId' --output text --region {region}

aws ec2 describe-subnets --filters Name=vpc-id,Values={vpc_id} --query 'Subnets[0:2].SubnetId' --output text --region {region}

aws ec2 create-security-group --group-name fargate-sg --description "Security group for Fargate tasks" --vpc-id {vpc_id} --region {region}

aws ec2 revoke-security-group-egress --group-id {sg_id} --ip-permissions IpProtocol=-1,IpRanges='[{CidrIp=0.0.0.0/0}]' --region {region}

Then add scoped rules:

aws ec2 authorize-security-group-egress --group-id {sg_id} --protocol tcp --port 443 --cidr 0.0.0.0/0 --region {region} and

aws ec2 authorize-security-group-egress --group-id {sg_id} --protocol udp --port 53 --cidr 0.0.0.0/0 --region {region}

Step 3: Create the ECR Repository

Constraints:

aws ecr create-repository --repository-name {ecr_repo_name} --region {region}

Step 4: Build and Push the Container Image

Constraints:

aws ecr get-login-password --region {region} | docker login --username AWS --password-stdin {account_id}.dkr.ecr.{region}.amazonaws.com

```

cd scripts

docker build --platform linux/amd64 -t {ecr_repo_name} .

docker tag {ecr_repo_name}:latest {account_id}.dkr.ecr.{region}.amazonaws.com/{ecr_repo_name}:latest

docker push {account_id}.dkr.ecr.{region}.amazonaws.com/{ecr_repo_name}:latest

cd ..

```

Step 5: Create IAM Roles

Follow the detailed instructions in references/iam-roles.md to create all IAM roles (Lambda, ECS task execution, ECS task, Step Functions, and EventBridge roles).

Step 6: Create the Lambda Function

Constraints:

```

aws lambda create-function \

--function-name sfn-file-processor \

--runtime python3.12 \

--handler lambda_function.lambda_handler \

--role arn:aws:iam::{account_id}:role/sfn-lambda-role \

--zip-file fileb:///tmp/lambda_function.zip \

--timeout 60 \

--architectures x86_64 \

--region {region}

```

aws lambda get-function --function-name sfn-file-processor --region {region}

Step 7: Create the CloudWatch Log Group

Constraints:

aws logs create-log-group --log-group-name /StepFunctionFargateTask --region {region}

aws logs associate-kms-key --log-group-name /StepFunctionFargateTask --kms-key-arn {kms_key_arn} --region {region}

Step 8: Create the ECS Cluster and Task Definition

Follow the detailed instructions in references/ecs-task-definition.md to create the ECS cluster and register the Fargate task definition.

Step 9: Create the S3 Bucket with EventBridge Notifications

Constraints:

aws s3api create-bucket --bucket {bucket_name} --region {region} --create-bucket-configuration LocationConstraint={region}

aws s3api put-bucket-notification-configuration --bucket {bucket_name} --notification-configuration '{"EventBridgeConfiguration": {}}' --region {region}

aws s3api put-bucket-encryption --bucket {bucket_name} --server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"aws:kms"}}]}' --region {region}

Step 10: Create the Step Functions State Machine

Constraints:

```

sed -e 's|${LambdaFunction}|arn:aws:lambda:{region}:{account_id}:function:sfn-file-processor|g' \

-e 's|${Cluster}|arn:aws:ecs:{region}:{account_id}:cluster/sfn-cluster|g' \

-e 's|${TaskDefinition}|{task_definition_arn}|g' \

-e 's|${Subnet1}|{subnet1_id}|g' \

-e 's|${Subnet2}|{subnet2_id}|g' \

-e 's|${SecurityGroup}|{sg_id}|g' \

scripts/statemachine.asl.json > /tmp/statemachine.asl.json

```

```

aws stepfunctions create-state-machine \

--name {state_machine_name} \

--definition file:///tmp/statemachine.asl.json \

--role-arn arn:aws:iam::{account_id}:role/sfn-state-machine-role \

--type STANDARD \

--region {region}

```

Step 11: Create the EventBridge Rule

Constraints:

```

aws events put-rule \

--name s3-to-stepfunctions \

--event-pattern '{

"source": ["aws.s3"],

"detail-type": ["Object Created"],

"detail": {

"bucket": {

"name": ["{bucket_name}"]

}

}

}' \

--region {region}

```

```

aws events put-targets \

--rule s3-to-stepfunctions \

--targets '[{

"Id": "StepFunctionsTarget",

"Arn": "{state_machine_arn}",

"RoleArn": "arn:aws:iam::{account_id}:role/sfn-eventbridge-role"

}]' \

--region {region}

```

Step 12: Configure Monitoring

Constraints:

aws sqs create-queue --queue-name s3-to-stepfunctions-dlq --region {region}

```

aws events put-targets \

--rule s3-to-stepfunctions \

--targets '[{

"Id": "StepFunctionsTarget",

"Arn": "{state_machine_arn}",

"RoleArn": "arn:aws:iam::{account_id}:role/sfn-eventbridge-role",

"DeadLetterConfig": {

"Arn": "arn:aws:sqs:{region}:{account_id}:s3-to-stepfunctions-dlq"

}

}]' \

--region {region}

```

aws cloudwatch put-metric-alarm --alarm-name sfn-execution-failures --metric-name ExecutionsFailed --namespace AWS/States --statistic Sum --period 300 --threshold 1 --comparison-operator GreaterThanOrEqualToThreshold --evaluation-periods 1 --dimensions Name=StateMachineArn,Value={state_machine_arn} --region {region}

Step 13: Validate

Constraints:

```

echo 'test data' > /tmp/small-file.txt

aws s3 cp /tmp/small-file.txt s3://{bucket_name}/small-file.txt --region {region}

```

aws stepfunctions list-executions --state-machine-arn {state_machine_arn} --region {region}

Troubleshooting

EventBridge rule not triggering

Step Functions execution fails at Fargate task

Lambda invocation fails

IAM PassRole errors

Fargate task stuck in PROVISIONING

Security Considerations

Version information

Additional Resources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.