Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments

Thing you want to do

Get the arguments registered in CloudWatch with Java running on Lambda.

Background / history

The Lambda function that starts and stops EC2 created in the previous article, CloudWatchEvents' Cron was designed to start at multiple times, such as 8:00, 9:00, 17:00, and 18:00. But I forgot which instance I tried to launch at what time, I had to check the arguments registered in CloudWatch Events one by one. It was a hassle, so I created a function to get the arguments. (If you take a memo, you have to synchronize the registered argument with the memo, so I wanted to avoid it.)

Using Java with AWS Lambda Table of Contents

Eclipse Preparation ・ Registration execution (someday) -Implementation-Stop / Start EC2 -Implementation-Check CloudWatch arguments -Implementation Tips --Get Instance Name from Reagion and Instance ID

Create a class for the time being

[Same as last time](https://qiita.com/t_ookubo/items/3b12bd985a65a73b4c59#%E3%81%A8%E3%82%8A%E3%81%82%E3%81%88%E3%81%9A % E3% 82% AF% E3% 83% A9% E3% 82% B9% E4% BD% 9C% E6% 88% 90)

Go to CloudWatch Events

Create a CloudWatchEvents object

//Create a CloudWatchEvents object
AmazonCloudWatchEvents event = AmazonCloudWatchEventsClientBuilder.defaultClient();

Get the rules for CloudWatch Events

ListRulesResult retRule = event.listRules(new ListRulesRequest());
for(Rule rule : retRule.getRules()) {
    // rule.getName()、rule.getDescription()
}

This part of the console As the method name suggests, you can get the name and description of the rule. image.png

Get targets from CloudWatch Events rules

// get target information from rule
ListTargetsByRuleRequest req = new ListTargetsByRuleRequest().withRule(rule.getName());
ListTargetsByRuleResult retTarget = event.listTargetsByRule(req);
List<Target> cloudWatchTargets = retTarget.getTargets();

for (Target target : cloudWatchTargets) {
    //  do loop each cloud watch rule's target
}

This part of the console image.png

Narrow down by function name from the targets from the rules of CloudWatch Events

target.getArn()

Now each target will be returned in the form below arn:aws:lambda:region:xxxxxx:function:functionName

So narrow down with endWith

if (target.getArn().endsWith("Function name you want to narrow down")) {
    // 
}

This part of the console (in the case of this console screen, there is only one in the first place, so it doesn't matter whether you do it or not.) image.png

Get the argument (input) from the target from the rules of CloudWatchEvents

target.getInput()

This part of the console image.png

//Since the Input of cloudWatch is described in json, convert from json to bean
SomethingBean cloudWatchInput = Jackson.fromJsonString(target.getInput(), SomethingBean.class);

After that, format the rule name, description, arguments, etc. of CloudWatchEvents that you have acquired so far into the form you want to output, and return it to output to finish.

POM added this time

<dependency>
	<groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk-cloudwatch</artifactId>
	<version>1.11.99</version>
	<scope>compile</scope>
</dependency>
<dependency>
	<groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk-events</artifactId>
	<version>1.11.719</version>
	<scope>compile</scope>
</dependency>

Recommended Posts

Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments
Using Java with AWS Lambda-Eclipse Preparation
Using Java with AWS Lambda-Implementation-Stop / Launch EC2
Using Mapper with Java (Spring)
Validate arguments using ArgumentCaptor with mockito
Try using Redis with Java (jar)
Html5 development with Java using TeaVM
Using proxy service with Java crawling
Using JupyterLab + Java with WSL on Windows 10
AWS Lambda with Java starting now Part 1
Game development with two people using java 2
I tried using OpenCV with Java + Tomcat
Game development with two people using java 1
Try managing Java libraries with AWS CodeArtifact
Game development with two people using java 3
Try using the Wii remote with Java
Using Java with AWS Lambda-Implementation Tips-Get Instance Name from Reagion and Instance ID
Interact with LINE Message API using Lambda (Java)
How to use Java framework with AWS Lambda! ??
Specify ClassPath when using jupyter + Java with WSL
Using Gradle with VS Code, build Java → run
Set a signed cookie (for CloudFront) with a custom policy using the AWS SDK for Java
Using Java 8 with Bluemix (on Liberty Runtime & DevOps Service)
Getting started with Java programs using Visual Studio Code
Submit a job to AWS Batch with Java (Eclipse)
How to deploy Java to AWS Lambda with Serverless Framework
I tried to operate SQS using AWS Java SDK
[Java] Development with multiple files using package and import
Install java with Homebrew
Sorting using java comparator
Change seats with java
Install Java with Ansible
Using Pair with OpenJDK
Comfortable download with JAVA
Scraping practice using Java ②
Switch java with direnv
[Java] Arguments and parameters
Scraping practice using Java ①
Download Java with Ansible
Let's scrape with Java! !!
Build Java with Wercker
Endian conversion with JAVA
Regularly post imaged tweets on Twitter with AWS Lambda + Java
Log aggregation and analysis (working with AWS Athena in Java)
Using multiple versions of Java with Brew on Mac + jEnv
Register your own Docker image with ECR using AWS CLI
Create a SlackBot with AWS lambda & API Gateway in Java
Problems with Dijkstra's algorithm using PriorityQueue and adjacency list (java)
Socket communication with a web browser using Java and JavaScript ②
Try Spark Submit to EMR using AWS SDK for Java
Socket communication with a web browser using Java and JavaScript ①
I tried using the CameraX library with Android Java Fragment
Upload / download / bulk delete data to S3 using Amazon S3 Client Builder with AWS SDK for Java
AWS Elastic Beanstalk # 1 with Java starting from scratch-Building a Java web application environment using the EB CLI-