Deploy Java Lambda - AWS

This post walks through steps required to create and deploy java lambda functions using eclipse. In the next post, we will have a look at release management and versioning of lambdas.
  • We will be using Eclipse for this example. AWS tool kit plugin is not mandatory to create java lambda functions, but it is handy to have it installed. This post assumes that AWS plugin is installed in eclipse. Click File -> New -> Other -> aws and then select ‘AWS Lambda Java Project’.

  • Enter name of project ‘LambdaExamples’ and click Finish.
  • Lambda Project has been created and lambda functions can now be created in this project. Again click File -> New -> Other -> aws and then select ‘AWS Lambda Function’. Enter source folder, package name and name of lambda function. Also select type of event which this lambda will receive. Lambdas are usually triggered as part of some event. For example, a new file uploaded to S3, or a new message arrived in SNS Topic. AWS passes information regarding these events as input parameter to lambda function. For this example, we can select custom event.
  • Add a simple print statement printing ‘Hello from Lambda’ in function. We can see this message logged later to make sure lambda is being invoked fine.
  • Before deploying lambda function to aws, we need to create IAM role. This is the role that lambda can assume when executing. Permissions of lambda will depend on policies attached to this role. For now, create a role using IAM Management console and attach AWSLambdaBasicExecutionRole Policy to attach role. This policy allows lambda to publish logs to aws cloud watch. If this policy is not attached to role, then lambda obviously cannot publish any logs to cloud watch.
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "logs:CreateLogGroup",
                    "logs:CreateLogStream",
                    "logs:PutLogEvents"
                ],
                "Resource": "*"
            }
        ]
    }
    
  • Now that lambda and IAM role have been created, next step is to deploy lambda to aws. Lambda can be deployed using AWS Toolkit. However, in some cases, it is not feasible to use toolkit to deploy lambdas. A simple scenario is when developer does not have permission to deploy lambdas to account or when jar files need to be part of some other release management process. We will also have a look at deploying lambdas manually.
  • Deploying using AWS Toolkit Plugin:
    • Go to windows -> preference -> aws tool kit to open plugin configurations. AWS plugin can read profiles from aws credentials file and can use those credentials to deploy lambda to aws account. Create AWS credentials in IAM console, grant access to deploy lambda, save credentials and profile name in a file and enter path of credentials file in aws plugin. Then select profile in dropdown box. Make sure that credentials against this profile have permission to deploy lambda.
    • Right click on function name, (in our example LambdaExample.java), and select Amazon Web Services -> Upload Function to AWS. Select region, enter new function name and click next.
    • Enter description of lambda, select IAM role created earlier, and select bucket where packaged file will be saved. Log into AWS Management console to verify that lambda has been deployed successfully.
  • Deploying Manually:
    • Package the lambda function in jar or zip file. We need to create Maven run configuration such that goal is ‘package’. Right click on lambda project, select run as, then select ‘Maven build…’ and enter ‘package’ as goal. Click Run and jar file should be created in target directory.
    • Log into AWS Management console and go to lambda screen. Create new lambda function, upload jar file and select IAM role created earlier. Select Run time as Java8, and select Handler as ‘com.amazonaws.lambda.examples.LambdaExample’. Handler is fully qualified name of class of which handleRequest method will be called whenever lambda is invoked.
  • Test Lambda Deployment
    • AWS allows to create Test events. On AWS lambda console, select function name and then select Configure Test Events.
    • While creating lambda function, we selected Custom Event which means our lambda function is getting Object as input parameter. Enter name of Test, and then select create. Click Test, and you should see status code 200 and should also see “Hello from Lambda” in logs.

Comments

Popular posts from this blog

Practice Questions - AWS Solutions Architect - Associate Certification

Continuous Integration using AWS CodePipeline (GitHub to Elastic BeanStalk)

AWS Parameter Store