|
| 1 | +# Leveraging Fargate for Scheduled Jobs |
| 2 | + |
| 3 | +This pattern contains a sample serverless framework template to deploy a scheduled AWS Fargate task using EventBridge running on an Amazon Elastic Container Service (ECS) cluster. The docker image is pushed to Amazon Elastic Container Registry (ECR) using serverless framework template without having to pre-push the image to ECR or another container library. The serverless framework plugin for fargate, `serverless-fargate` is used to deploy tasks on ECS Cluster. This task runs every 10th minute of the hour using Eventbridge Rule and a simple file containing JSON is put in S3 bucket. |
| 4 | + |
| 5 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +* [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. |
| 10 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 11 | +* [Git CLI](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed |
| 12 | +* [NodeJS](https://nodejs.org/en/download/) (LTS version) installed |
| 13 | +* [Serverless Framework CLI](https://www.serverless.com/framework/docs/getting-started) installed |
| 14 | +* [Docker Desktop](https://docs.docker.com/desktop/) installed |
| 15 | + |
| 16 | +## Deployment Instructions |
| 17 | + |
| 18 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 19 | + |
| 20 | + ``` sh |
| 21 | + git clone https://github.com/aws-samples/serverless-patterns |
| 22 | + ``` |
| 23 | + |
| 24 | +1. Change directory to the pattern directory: |
| 25 | + |
| 26 | + ``` sh |
| 27 | + cd fargate-eventbridge-serverless |
| 28 | + ``` |
| 29 | + |
| 30 | +1. Open Docker Desktop and keep it running in the background. |
| 31 | + |
| 32 | +1. Install serverless-fargate plugin in dev dependencies: |
| 33 | + ``` |
| 34 | + // using yarn |
| 35 | + yarn add serverless-fargate --dev |
| 36 | + // using npm |
| 37 | + npm install serverless-fargate --save-dev |
| 38 | + ``` |
| 39 | +1. From the command line, use Serverless Framework to deploy the AWS resources for the pattern as specified in the serverless.yml file: |
| 40 | + |
| 41 | + ``` sh |
| 42 | + serverless deploy --verbose |
| 43 | + ``` |
| 44 | + |
| 45 | + The above command will deploy resources to `ap-south-1` region by default. You can override the target region with `--region <region>` CLI option, e.g. |
| 46 | + |
| 47 | + ``` sh |
| 48 | + serverless deploy --verbose --region eu-west-1 |
| 49 | + ``` |
| 50 | + |
| 51 | +## How it works |
| 52 | + |
| 53 | +- Make sure you have all the things configured mentioned in Requirements section. |
| 54 | +- The image is constructed directly from the Dockerfile that is provided using Docker Desktop. |
| 55 | +- The image is pushed to Amazon Elastic Container Repository (ECR). |
| 56 | +- Fargate Task role is created. |
| 57 | +- Fargate Execution role is created. |
| 58 | +- The S3 bucket for output is created. |
| 59 | +- The ECS cluster is created. |
| 60 | +- The Task Definition is created. This also passes the environment variable to the scheduled task. |
| 61 | +- Networking resources are created. |
| 62 | +- Finally EventBridge Rule is created for running task every 10th minute of the hour. |
| 63 | + |
| 64 | +## Testing |
| 65 | + |
| 66 | +To test the deployment, you can verify by going to the output S3 bucket and find the output JSON file which contains a message and timestamp as below: |
| 67 | + ``` |
| 68 | + { |
| 69 | + "message": "Task executed from Fargate Cluster.", |
| 70 | + "timestamp": 1692648300534 |
| 71 | + } |
| 72 | + ``` |
| 73 | + |
| 74 | +## Cleanup |
| 75 | + |
| 76 | +1. Empty the output S3 bucket. |
| 77 | + |
| 78 | +1. Delete the stack: |
| 79 | + |
| 80 | + ```sh |
| 81 | + serverless remove --verbose |
| 82 | + ``` |
| 83 | + |
| 84 | + NOTE: You might need to add `--region <region>` option to AWS CLI command if you AWS CLI default region does not match the one, that you used for the Serverless Framework deployment. |
| 85 | + |
| 86 | +---- |
| 87 | +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 88 | + |
| 89 | +SPDX-License-Identifier: MIT-0 |
0 commit comments