This application is simple example of creating an AWS lambda functions using aws java core library.
How to build -
mvn clean package
There are two ways to deploy this library -
-
Manual deployment
- Login to AWS Console with credentials which has appropriate permissions.
- Search for Lambda and go to lambda home page.
- Create a new function of Java type.
- Go to code and upload the jar file "awslambda-java-httpendpoint.jar" from target directory.
- Edit our entry point and specify the Handler method with full class name. in this example "com.sourabh.aws.lambda.handleRequest".
- In top panel click on test and create a test event which will trigger this lambda.
- Click test now. We can see logs on the UI as well as in cloudwatch.
-
Serverless framework
- Install npm server less framework by
npm install serverless -g
- Now go to project root directory and run serverless deploy.
- The serverless framework will read serverless.yml from root folder and create a cloud formation template.
- It will upload the template and jar to one newly created s3 bucket.
- Sample cloud formation template generated i this case has been attached in resource folder.
- Next, It will create all resources based on this template.
Notes -
- To remove all resources created run command -
serverless remove
. I t will remove function, files, S3 bucket and everything created for tis project. - Manual uploading of artifact has size limit of 10 MB, so if you have bigger artifact upload to s3 first and then use from there.
- Install npm server less framework by
Sample Output from serverless deploy
serverless deploy
Serverless: Deprecation warning: Starting with next major version, API Gateway naming will be changed from "{stage}-{service}" to "{service}-{stage}".
Set "provider.apiGateway.shouldStartNameWithService" to "true" to adapt to the new behavior now.
More Info: https://www.serverless.com/framework/docs/deprecations/#AWS_API_GATEWAY_NAME_STARTING_WITH_SERVICE
Serverless: Packaging service...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
........
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service awslambda-java-httpendpoint.jar file to S3 (2.17 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............................
Serverless: Stack update finished...
Service Information
service: awslambda-java-httpendpoint
stage: dev
region: us-east-1
stack: awslambda-java-httpendpoint-dev
resources: 11
api keys:
None
endpoints:
GET - https://w6ybctx1cl.execute-api.us-east-1.amazonaws.com/dev/ping
functions:
currentTime: awslambda-java-httpendpoint-dev-currentTime
layers:
None
Sample output from serverless remove
serverless remove
Serverless: Deprecation warning: Starting with next major version, API Gateway naming will be changed from "{stage}-{service}" to "{service}-{stage}".
Set "provider.apiGateway.shouldStartNameWithService" to "true" to adapt to the new behavior now.
More Info: https://www.serverless.com/framework/docs/deprecations/#AWS_API_GATEWAY_NAME_STARTING_WITH_SERVICE
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
..............
Serverless: Stack removal finished...
Serverless: Stack removal finished...