{
	"AWSTemplateFormatVersion": "2010-09-09",
	"Parameters": {
	    "PojoZipBucketName": {
	        "AllowedPattern": "^[0-9a-zA-Z]+([0-9a-zA-Z-]*[0-9a-zA-Z])*$",
	        "ConstraintDescription": "Bucket name can include numbers, lowercase letters, uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-).",
	        "Default": "h2o-pr",
	        "Description": "S3 bucket name that contains the lambda code as",
	        "Type": "String"
	        },
	    "PojoZipFile": {
	        "AllowedPattern": "^[.0-9a-zA-Z-/]*$",
	        "ConstraintDescription": "Object key prefix can include numbers, lowercase letters, uppercase letters, hyphens (-), and forward slash (/).",
	        "Default": "h2odrf.zip",
	        "Description": "S3 key prefix for the lambda assets. The key can include numbers, lowercase letters, uppercase letters, hyphens (-), and forward slash (/).",
	        "Type": "String"
		}
	},
	
	"Resources": {
		"LambdaExecutionRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "RoleName": "H2OLambda",
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "lambda.amazonaws.com"
                                ]
                            },
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        }
                    ]
                },
                "Path": "/",
                "Policies": [
                    {
                        "PolicyName": "root",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "logs:CreateLogGroup",
                                        "logs:CreateLogStream",
                                        "logs:PutLogEvents"
                                    ],
                                    "Resource": "*"
                                }                                
                            ]
                        }
                    }
                ]
            }
        },

		"GetH2OPredictionLambda": {
        	"Type": "AWS::Lambda::Function",
			"DependsOn": "LambdaExecutionRole",
        	"Properties": {
                "Code": {
                    "S3Bucket": {"Ref" : "PojoZipBucketName"},
                    "S3Key": {"Ref" : "PojoZipFile"}
                },
            	"Description" : "Create a new event entry into Unicorn Feedback table",
            	"FunctionName" : "GetH2OPredictionLambda",
                "Handler": "com.amazonaws.lambda.demo.LambdaFunctionHandler",
            	"Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },
                "Runtime": "java8"
	        }
	    },

        "H2OAPI": {
            "Type": "AWS::ApiGateway::RestApi",
            "DependsOn": "GetH2OPredictionLambda",
            "Properties": {
                "Name": "H2OAPI",
                "Description" : "API to provide restful interface to lambda",
                "FailOnWarnings": true,
                "Body": {
                    "swagger":"2.0",
                    "info":{
                        "version":"2018-02-16T18:05:24Z",
                        "title":"NLPWorkshop API"
                    },
                    "host":"h2oapi.execute-api.us-east-1.amazonaws.com",
                    "basePath":"/test",
                    "schemes":[
                        "https"
                    ],
                    "paths":{
                        "/predict":{
                            "post":{
                                "consumes":[
                                    "application/json"
                                ],
                                "produces":[
                                    "application/json"
                                ],
                                "parameters":[ ],
                                "responses":{
                                    "200":{
                                        "description":"200 response",
                                        "schema":{
                                            "$ref":"#/definitions/Empty"
                                        }
                                    }
                                },
                                "x-amazon-apigateway-integration":{
                                    "responses":{
                                        "default":{
                                            "statusCode":"200"
                                        }
                                    },
                                    "uri": {"Fn::Join": ["",
                                        ["arn:aws:apigateway:", {"Ref": "AWS::Region"}, ":lambda:path/2015-03-31/functions/", {"Fn::GetAtt": ["GetH2OPredictionLambda", "Arn"]}, "/invocations"]
                                    ]},               
                                    "passthroughBehavior":"WHEN_NO_MATCH",
                                    "httpMethod":"POST",
                                    "requestTemplates":{},
                                    "contentHandling":"CONVERT_TO_TEXT",
                                    "type":"aws"
                                }
                            }
                        }
                    },
                    "definitions":{
                        "Empty":{
                            "type":"object",
                            "title":"Empty Schema"
                        }
                    }
                }                
            }
        },
       "GetPredictionsLambdaPermission": {
            "Type": "AWS::Lambda::Permission",
            "DependsOn": "H2OAPI",
            "Properties": {
                "Action": "lambda:invokeFunction",
                "FunctionName": {"Fn::GetAtt": ["GetH2OPredictionLambda", "Arn"]},
                "Principal": "apigateway.amazonaws.com",
                "SourceArn": {"Fn::Join": ["", 
                    ["arn:aws:execute-api:", {"Ref": "AWS::Region"}, ":", {"Ref": "AWS::AccountId"}, ":", {"Ref": "H2OAPI"}, "/*"]
                ]}
            }
        },  

        "H2OAPIDeployment": {
            "Type": "AWS::ApiGateway::Deployment",
            "DependsOn": "H2OAPI",
            "Properties": {
                "RestApiId": {"Ref": "H2OAPI"},
                "StageName": "prod1",
                "StageDescription": {
                    "LoggingLevel": "INFO",
                    "MetricsEnabled": "true"
                }
            }
        } 
	},

	"Outputs": {
       "APIURL": {
            "Description" : "URL to make predictions",
            "Value": {
                "Fn::Join": ["", ["https://", {"Ref": "H2OAPI"}, ".execute-api.", {"Ref": "AWS::Region"}, ".amazonaws.com/prod1/predict"]]
            }
		}
	}
}