GithubHelp home page GithubHelp logo

tassosl / cfn-response-java Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 3.0 17 KB

A utility for using Cloudformation with Java8-based lambdas as custom resources

License: Apache License 2.0

Java 100.00%

cfn-response-java's Introduction

Build Status Maven Central License

cfn-response-java

A utility for using Cloudformation with Java8-based AWS lambdas as custom resources

You may find it is convenient to run Java 8 AWS Lambdas as a step in your Cloudformation infrastructure provisioning. For example it may be useful to run a Java-based tool such as Liquibase to fully provision a schema in a SQL database. Why limit yourself to Python or Node.JS when Java8 is a supported platform on AWS Lambda? This project was inspired by another Github project for use with Python lambdas: https://github.com/jorgebastida/cfn-response

This project provides the same support for Java that AWS includes for Javascript. If you are using Javascript to write AWS Lambdas, you can use the code available from:

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html

Usage

To use, include the following Maven dependency in your project

    <dependency>
      <groupId>com.sunrun</groupId>
      <artifactId>cfn-response</artifactId>
      <version>1.0.2</version>
    </dependency>

Next, write a handler for your Lambda as usual, but before returning, call CfnResponseSender.send():

private final CfnResponseSender sender = new CfnResponseSender();

public MyCustomResponse myHandler(final CfnRequest<MyCustomRequest> request, final Context context) {
    // Do some stuff...
    final MyCustomResponse response = new MyCustomResponse();
    response.setFoo("bar");
    
    final boolean result = sender.send(request, Status.SUCCESS, context, null, response, null);
    LOGGER.info("outcome sent to Cloudformation successfully: " + result);
    
    return response;
}

Debugging

To make debugging easier and provide more helpful outputs even in production, make sure your Java 8 Lambda also has a runtime for the slf4j logging interface. For example you might include the following in your POM in order to get log messages sent to CloudWatch:

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.6</version>
    </dependency>

If running your lambda within a VPC, make sure you add a VPC endpoint as even public subnets do not seem to have access to the S3 endpoint necessary to communicate with Cloudformation. You probably want an unrestrictive policy as this policy will be the policy used for all traffic to S3 in the affected subnets.

{
  "Resources":{
    "VpcEndpoint":{
      "Type":"AWS::EC2::VPCEndpoint",
      "Properties":{
        "PolicyDocument":{
          "Statement":[
            {
              "Action":"*",
              "Effect":"Allow",
              "Resource":"*",
              "Principal":"*"
            }
          ]
        },
        "RouteTableIds":{
          "Ref":"YourRouteTableIdsWhereLambdaWillExecute"
        },
        "ServiceName":{
          "Fn::Join":[
            "",
            [
              "com.amazonaws.",
              {
                "Ref":"AWS::Region"
              },
              ".s3"
            ]
          ]
        },
        "VpcId":{
          "Ref":"YourVpcId"
        }
      }
    }
  }
}

Contributing

Please make sure all tests are passing & add unit test coverage if necessary.

References

cfn-response-java's People

Watchers

James Cloos avatar Tassos Livogiannis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.