This tutorials explains creating Lambda function in eclipse using aws eclipse sdk. It also explains how to deal with certain aws components like creating IAM User, Roles, Policies, S3 bucket, Lambda functions and API Gateways. Uploading the created lambda function to S3 bucket using the aws crdentials, invoking function and starting APIs, deploying apis and testing it with Postman Rest Client.
Install and Configure Eclipse with AWS Toolkit
Install the AWS Toolkit
To install AWS Toolkit for Eclipse follow the steps below.
- Within Eclipse, click Help and then click Install New Software.
- In the Work with box, type
https://aws.amazon.com/eclipse and then press Enter.
- Choose the components of the AWS Toolkit for Eclipse that you want to install. Click Select All to install all components at once.
- Once you have made your selections, click Next (or Finish) to complete installation.
- Once installation is complete, restart the eclipse for aws toolkit to take effect. After you restart you will get a new AWS icon added in your eclipse toolbar. You can create different types of AWS projects using this icon.
Create IAM User & Get AWS Access Keys
To get your access key ID and secret access key follow the steps below.
- Open the IAM console. If you have not already logged in then sign in to the aws account. You can also create a new account if you don’t have one.
- From the navigation menu at left side, click on Users. You will get a previously created list of existing users and none if No user is created earlier.
- Now from top click on Add User and you will get a new screen. Now enter your Username and select the Programmatic access and AWS Management Console access checkbox in Access type section. Enter the custom password in Console Password section. Now click on Next: Permissions on bottom right side.
- In next window, Attach existing policies directly and select AdministratorAccess and click on Next: Review
- Now on next window just click on Create user
- Now you will get a success screen and new IAM User is created just make a note of Access key ID and Secret access key, it will be used later in this tutorial. Alternatively you can also download the csv file of credentials using Download .csv button or email it using Send email link.
After getting access credentials, we need to add those to eclipse.
Add your AWS access keys to the AWS Toolkit for Eclipse
To add your access keys follow the steps below:
- Open Eclipse’s Preferences dialog box.
- Now click AWS Toolkit in the sidebar.
- Type or paste your AWS access key ID in the Access Key ID box.
- Type or paste your AWS secret access key in the Secret Access Key box.
- Click Apply and Close to store your access key information.
Create and IAM Role
After creating an IAM User and setting credentials we need to create an IAM Role. To create an IAM Role follow the below steps:
- Go back to AWS IAM console.
- From left navigation menu click on Roles, it will show up existing roles if created earlier. To create a new role click on Create Role.
- On the new window select AWS Service and select Lambda in it and click on Next: Permissions.
- On next window select AdministratorAccess and click on Next: Review.
- Now on this window enter Role name and click on Create Role.
- You will see a success message and new role will be created and it will show up in roles list.
Create S3 Bucket
To create S3 bucket follow the steps below:
- Open the S3 Bucket AWS console. It will show the existing buckets if it is previously created. You can create a new bucket from here. Just click on Create bucket.
- On the opened window enter the Bucket Name and Choose the Region and click on Next.
Note: You need to remember the Region selected, it will be used in later part.
- Do Next and Next and finally click on Create Bucket. And once this is done, new bucket will be created and it will show up on the page.
Creating Lambda Function
To create lambda function follow the below steps:
- Just click on the AWS icon and select New AWS Lamda Java Project.
- On the opened window, enter the Project name and select Input Type as Cusotm from dropdown for now and click on Finish.
- Now customize function to output Welcome message to whatever name is given as input.
- Now right click and hover down to AWS Lambda and then click on Upload function to AWS Lambda.
- On the new Target Lambda Function window, Select the AWS Region and then Create a new Lambda function if you are uploading it for first time or else Choose an Existin Lambda Function and click on Next.
Note: You need to select the same AWS Region in which you had created your S3 bucket.
- On this window enter the Description, select the IAM Role and S3 Bucket created above and click on Finish.
- Now open your S3 Bucket and see your Lambda project code will be uploaded there as a zip file.
- Also, your lambda function will be created on AWS. Just open the AWS Lambda Console. You will see that your function is already present there.
- Now as the code is uploaded to S3 Bucket and Lambda function is already created in AWS. Now we will try to run this lambda function using eclipse.
- Right click on eclipse code pane, hover down to AWS Lambda and then click on Run function on AWS Lambda.
- Now enter the input in this window and click on Invoke.
Note: Due to some bug in eclipse, if you type your input in the box it will not work coz it automatically changes the quotes which is not compatible withh UTF-8. So just type it in some editor and just paste your input in the box.
- Now, you can see the ouput in eclipse output bar.
Now, as our service is ready, this service says Welcome to the whhatever name is given as input. We need to deploy it using AWS API Gateway and then test this service url using Postman-Rest Client.
Deploy Lambda Function to API Gateway
To deploy lambda function to API Gateway follow the steps below:
- Click on Create API to create a new API. Enter the API name, Description and Endpoint Type, then click on Create API.
- Now, from
Actions dropdown click on Create Resource.
- Give Resource Name, Resource Path and click on Create Resource.
- Now select that resource and from
Actions dropdown click on Create Method.
- Now select request method from dropdown as
GET or something else.
- Click on
tick mark. Select Integration Type as
Lambda Function. Select Lambda Region where you had created your Lambda Function. Enter the name of Lambda Function you had created earlier and click on
- Now you will get a screen like below, you can test your api now.
- Click on
Test and give the input in Request Body. Click on Test and you will get ouput in right side.
- If you are getting the screen like above, your API is working fine. You can deploy this API now.
- To deploy click on Deploy API from
- Now select Deployment stage as
New Stage. Give Stage name as
prod. Enter Stage description and Deployment description and click on Deploy.
- You will get a screen like below when deployment is successful.
- See that you have got an
Invoke URL this is the url of your deployed api. You can access your api on this url.
Testing API with Postman
To test your api with postman follow below steps:
- Open your
Postman application. Enter the your
api url in that and DON’T FORGET to append resource path after it. For this api url is
https://1173acvizd.execute-api.us-east-1.amazonaws.com/dev and resource path was
- So complete url is
Note: If you don’t append resource path you will get
"Missing Authentication Token" error.
Note: You can create many resources for a particular api and all those resources will have methods and methods will have their own Lambda Function.
In postman select request type as POST, enter API URL, then select Body and select raw. Now enter the input in box, and click on send and you will get output in below window.
Like this post
Share this post on
About the author
- Career over Relationships. Family above everything else.
- Passionate Geek with love for Computer Science, Artificial Intelligence.
- Avid interest in Economics, Sports and Adventures.
- Curious to know outside the boundary limits, coz what I know is very little and what I don’t know is huge.
Join the discussion