Skip to content

Commit 64e186f

Browse files
authored
Merge pull request aws-samples#1588 from distinction-dev/notanmay-feature-fargate-eventbridge-serverless
New Serverless Pattern: fargate-eventbridge-serverless
2 parents 862513f + 15c854f commit 64e186f

6 files changed

Lines changed: 312 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM amd64/node:18.17-alpine
2+
3+
COPY . .
4+
5+
RUN yarn install
6+
7+
CMD [ "handlers/handler.js" ]
8+
9+
ENTRYPOINT [ "node" ]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"title": "Leveraging Fargate for Scheduled Jobs",
3+
"description": "Create a scheduled AWS Fargate Task running on AWS ECS Cluster and EventBridge Rule using Serverless Framework",
4+
"language": "Node.js",
5+
"architectureURL": "",
6+
"videoId": "",
7+
"level": "100",
8+
"framework": "Serverless Framework",
9+
"introBox": {
10+
"headline": "How it works",
11+
"text": [
12+
"This pattern deploys an AWS Fargate Task on AWS ECS Cluster which runs on EventBridge Rule.",
13+
"",
14+
"The docker image for the task based on Node.js is built first, then the image is pushed to AWS ECR. AWS ECS pulls the image from ECR and runs the image on Fargate. A serverless plugin called 'serverless-fargate' is used to create the template. The job is scheduled to run on every 10th minute of the hour.",
15+
"",
16+
"A simple JSON file containing message string and timestamp, is pushed to S3 bucket. All the necessary resources and permissions are configured using serverless framework."
17+
]
18+
},
19+
"gitHub": {
20+
"template": {
21+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/fargate-eventbridge-serverless",
22+
"templateURL": "serverless-patterns/fargate-eventbridge-serverless",
23+
"projectFolder": "fargate-eventbridge-serverless",
24+
"templateFile": "serverless.yml"
25+
}
26+
},
27+
"resources": {
28+
"headline": "Additional resources",
29+
"bullets": [
30+
{
31+
"text": "Creating Amazon ECS resources with AWS CloudFormation",
32+
"link": "https://docs.aws.amazon.com/AmazonECS/latest/userguide/creating-resources-with-cloudformation.html"
33+
},
34+
{
35+
"text": "Serverless Fargate Plugin",
36+
"link": "https://www.serverless.com/plugins/serverless-fargate"
37+
}
38+
]
39+
},
40+
"deploy": {
41+
"text": [
42+
"<code>serverless deploy --verbose</code>"
43+
]
44+
},
45+
"testing": {
46+
"text": [
47+
"See the Github repo for detailed testing instructions."
48+
]
49+
},
50+
"cleanup": {
51+
"text": [
52+
"<code>serverless remove --verbose</code>."
53+
]
54+
},
55+
"authors": [
56+
{
57+
"headline": "Presented by Tejas Dhamecha, Software Engineer at Distinction-Dev, India.",
58+
"name": "Tejas Dhamecha",
59+
"bio": "Tejas Dhamecha is a Software Engineer working at Distinction-Dev, India who has a passion for building serverless solutions.",
60+
"linkedin":"https://www.linkedin.com/in/tejas-dhamecha/",
61+
"imageURL": "https://0.gravatar.com/avatar/98a4c425988ef7ee758a28ba98b947b7860c3182e898340b7b3c12bc441effb7?size=256"
62+
}
63+
]
64+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { PutObjectCommand, S3Client } = require("@aws-sdk/client-s3");
2+
const s3 = new S3Client({});
3+
4+
const handler = async () => {
5+
const timestamp = Date.now()
6+
const message = JSON.stringify({
7+
message: 'Task executed from Fargate Cluster!',
8+
timestamp
9+
})
10+
const putCommand = new PutObjectCommand({
11+
Bucket: process.env.DESTINATION_BUCKET,
12+
Key: timestamp + '.json',
13+
ContentType: 'application/json',
14+
Body: message,
15+
});
16+
const result = await s3.send(putCommand);
17+
console.log('result: ', result)
18+
};
19+
20+
handler()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "fargate-eventbridge-serverless",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"devDependencies": {
7+
"serverless-fargate": "^0.0.12"
8+
},
9+
"dependencies": {
10+
"@aws-sdk/client-s3": "^3.395.0"
11+
}
12+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
service: fargate-eventbridge-serverless
2+
frameworkVersion: '3'
3+
provider:
4+
name: aws
5+
stage: ${opt:stage, 'dev'}
6+
region: ${opt:region, 'ap-south-1'}
7+
iamRoleStatements:
8+
- Effect: Allow
9+
Action:
10+
- s3:CreateBucket
11+
Resource: arn:aws:s3:::${self:custom.destinationBucket}/*
12+
ecr:
13+
images:
14+
hello-task:
15+
path: ./
16+
file: Dockerfile
17+
18+
custom:
19+
destinationBucket: ${self:service}-${self:provider.stage}-destination-bucket
20+
21+
fargate:
22+
clusterName: fargate-eventbridge-serverless
23+
containerInsights: true
24+
memory: '0.5GB'
25+
cpu: 256
26+
architecture: X86_64
27+
vpc:
28+
assignPublicIp: true
29+
securityGroupIds:
30+
- {Ref: SecurityGroup}
31+
subnetIds:
32+
- {Ref: Subnet}
33+
iamRoleStatements:
34+
- Effect: Allow
35+
Action:
36+
- s3:PutObject
37+
Resource: arn:aws:s3:::${self:custom.destinationBucket}/*
38+
39+
tasks:
40+
hello-task:
41+
name: hello-task
42+
image: hello-task
43+
vpc:
44+
assignPublicIp: true
45+
securityGroupIds:
46+
- {Ref: SecurityGroup}
47+
subnetIds:
48+
- {Ref: Subnet}
49+
memory: '0.5GB'
50+
cpu: 256
51+
schedule: cron(0/10 * * * ? *)
52+
environment:
53+
DESTINATION_BUCKET: ${self:custom.destinationBucket}
54+
55+
resources:
56+
Resources:
57+
DestinationBucket:
58+
Type: AWS::S3::Bucket
59+
Properties:
60+
BucketName: ${self:custom.destinationBucket}
61+
# creates a VPC with a public subnets within the VPC
62+
VPC:
63+
Type: AWS::EC2::VPC
64+
Properties:
65+
CidrBlock: 12.2.1.0/24
66+
EnableDnsHostnames: true
67+
EnableDnsSupport: true
68+
Subnet:
69+
Type: AWS::EC2::Subnet
70+
Properties:
71+
VpcId: { Ref: VPC }
72+
CidrBlock: 12.2.1.0/25
73+
MapPublicIpOnLaunch: true
74+
AvailabilityZone: !Select
75+
- 0
76+
- Fn::GetAZs: {Ref: 'AWS::Region'}
77+
SecurityGroup:
78+
Type: AWS::EC2::SecurityGroup
79+
Properties:
80+
GroupDescription: SG created via serverless framework
81+
VpcId: { Ref: VPC }
82+
SecurityGroupIngress:
83+
- IpProtocol: tcp
84+
FromPort: 80
85+
ToPort: 80
86+
CidrIp: 0.0.0.0/0
87+
- IpProtocol: tcp
88+
FromPort: 443
89+
ToPort: 443
90+
CidrIp: 0.0.0.0/0
91+
# internet gateway that connects the vpc to the internet
92+
InternetGateway:
93+
Type: AWS::EC2::InternetGateway
94+
AttachGateway:
95+
Type: AWS::EC2::VPCGatewayAttachment
96+
Properties:
97+
VpcId: { Ref: VPC }
98+
InternetGatewayId: { Ref: InternetGateway }
99+
PublicSubnetRouteTable:
100+
Type: AWS::EC2::RouteTable
101+
Properties:
102+
VpcId: { Ref: VPC }
103+
# creates public routes for the subnet
104+
PublicSubnetRoute:
105+
Type: AWS::EC2::Route
106+
DependsOn: AttachGateway
107+
Properties:
108+
RouteTableId: { Ref: PublicSubnetRouteTable }
109+
DestinationCidrBlock: 0.0.0.0/0
110+
GatewayId: { Ref: InternetGateway }
111+
PublicSubnetRouteTableAssociationOne:
112+
Type: AWS::EC2::SubnetRouteTableAssociation
113+
Properties:
114+
RouteTableId: { Ref: PublicSubnetRouteTable }
115+
SubnetId: { Ref: Subnet }
116+
117+
plugins:
118+
- serverless-fargate

0 commit comments

Comments
 (0)