Do you want to shutdown your ECS instances automatically during weekends and start it out on weekdays ? Here is the solution for you.
So we would need 2 more things- CloudWatch and Lambda, besides the ECS clusters.
We will create 2 AWS CloudWatch triggers (now called EventBridge), one for starting the ECS clusters and one for stopping the ECS clusters, which will be triggered at a specified time and will call a AWS Lambda, which then will do necessary action – in this case Start/Stop the ECS clusters:
Step 1:
Lets first develop our Lambda and deploy it to AWS.
A detailed step of how to create and deploy a Lambda is also mentioned here.
Create a Java Maven project.
Put the below details in pom.xml:
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-core</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-events</artifactId> <version>2.2.7</version> </dependency> <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-ecs --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-ecs</artifactId> <version>1.11.892</version> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.2</version> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> <!-- <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>aws</shadedClassifierName> --> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
The Lambda Handler class:
public class App implements RequestHandler<Object,Objec>{ public Object handleRequest(Object input, Context context) { String resource=(String)((List)((LinkedHashMap)input).get("resources")).get(0);//Get the value of the 'resources' attribute from input event. String[] resourceArr=resource.split("/"); if(resourceArr[1].equals("Stop_ECS")) { AmazonECS client=AmazonECSClientBuilder.standard().withRegion("ap-south-1").build(); UpdateServiceRequest request=new UpdateServiceRequest().withCluster("heapsteep-cluster-3").withService("heapsteep-service-3").withDesiredCount(0); UpdateServiceResult response=client.updateService(request); System.out.println("*******ECS Clusters Stopped"); return response; }else if(resourceArr[1].equals("Start_ECS")) { AmazonECS client=AmazonECSClientBuilder.standard().withRegion("ap-south-1").build(); UpdateServiceRequest request=new UpdateServiceRequest().withCluster("heapsteep-cluster-3").withService("heapsteep-service-3").withDesiredCount(2); UpdateServiceResult response=client.updateService(request); System.out.println("*******ECS Clusters Started"); return response; } return null; } }
Do this to the project – clean package
You will see an jar file got generated in the target folder of the project.
Upload the same jar to AWS Lambda.
Add “ECSFullAccess” policy to the Lambda, since the Lambda has to do some modifications to the ECS Clusters.
Step 2:
Lets create the AWS CloudWatch triggers:
Go to AWS Console -> Cloudwatch -> Create Rule.
Enter the cron expression of the time when you want to trigger the rule.
Below is an example of cron expression:
30 12 ? * FRI * => For Friday shutdown
05 03 ? * MON * => For Monday Starting
Enter the target. In our case it would be the above Lambda function.
GitHub URL for this tutorial: https://github.com/heapsteep/Start_Stop_ECS_Lambda.git