Skip to content

Instantly share code, notes, and snippets.

@arnathan2k
Last active April 19, 2018 02:27
Show Gist options
  • Save arnathan2k/d137d82f47c71d75cdae4ff533d2c27a to your computer and use it in GitHub Desktop.
Save arnathan2k/d137d82f47c71d75cdae4ff533d2c27a to your computer and use it in GitHub Desktop.
Auto Scaling Life Cycle Event processing with Lambda
//RunCommandlambda.java Source Code
package net.anandus.captureloglamda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.List;
public class RunCommandLambda implements RequestHandler<Request, Response> {
/**
*
* @param request
* @param context
* @return
*/
@Override
public Response handleRequest(Request request, Context context) {
/* just getting the values do your thing by getting these values */
context.getLogger().log("Id: "+request.getId());
context.getLogger().log("Account: "+request.getAccount());
context.getLogger().log("Region: "+request.getRegion());
context.getLogger().log("Source: "+request.getSource());
context.getLogger().log("Version: "+request.getVersion());
List list = request.getResources();
for(int i=0;i < list.size();i++){
context.getLogger().log(" Resources: "+(String)list.get(i));
}
context.getLogger().log("Instance id is ... "+request.getDetail().get("EC2InstanceId"));
String message = "";
// return what ever you want
return new Response();
}
}
// POJO for Request
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package net.anandus.captureloglamda;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
*
* @author Zadious2
*/
public class Request {
private String version;
private String id;
private String detailType;
private String source;
private String account;
private Date time;
private String region;
private List<String> resources;
private HashMap detail;
public HashMap getDetail() {
return detail;
}
public void setDetail(HashMap details) {
this.detail = details;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDetailType() {
return detailType;
}
public void setDetailType(String detailType) {
this.detailType = detailType;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public List<String> getResources() {
return resources;
}
public void setResources(List<String> resources) {
this.resources = resources;
}
}
// POJO for response
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package net.anandus.captureloglamda;
/**
*
* @author Zadious2
*/
public class Response {
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Response() {
}
}
/* Sample Cloud Watch Event to process
Just cut and paste this into your Lambda Test
*/
{
"version": "0",
"id": "468fe059-f4b7-445f-bb22-2a271b94974d",
"detail-type": "EC2 Instance-terminate Lifecycle Action",
"source": "aws.autoscaling",
"account": "123456789012",
"time": "2015-12-22T18:43:48Z",
"region": "us-east-1",
"resources": [
"arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:59fcbb81-bd02-485d-80ce-563ef5b237bf:autoScalingGroupName/sampleASG"
],
"detail": {
"LifecycleActionToken": "630aa23f-48eb-45e7-aba6-799ea6093a0f",
"AutoScalingGroupName": "sampleASG",
"LifecycleHookName": "SampleLifecycleHook-6789",
"EC2InstanceId": "i-12345678",
"LifecycleTransition": "autoscaling:EC2_INSTANCE_TERMINATING"
}
}
// POM File - get the dependencies from search.maven.com
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>be.quodlibet</groupId>
<artifactId>Captureloglambda</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ssm</artifactId>
<version>1.11.308</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-events</artifactId>
<version>1.11.309</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<name>CaptureLogLambda</name>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment