Create A Lambda Microservice

Create A Lambda Microservice

In this step, we will create an AWS Lambda Function that adds all the operands provided in the input payload. We will create this Lambda manually using the console. Later, we will use a full CI/CD pipeline (with the help of AWS CodeStar) to deploy a Lambda function that will provide full arithmetic calculation for our calculator.

  1. Download file CalculatorLambda.zip to the Windows virtual machine and extract
  2. In the Eclipse IDE, click menu File
  • Click Import… Create A Lambda Microservice
  1. Click Maven
  • Select Existing Maven projects
  • Click Next Create A Lambda Microservice
  1. Click Browse…
  • Select the direction of the CalculatorLambda project we extracted in the step 1
  • Click Finish Create A Lambda Microservice
  1. In the Eclip IDE, right-click on the root element of the calculator_lambda project
  • Click Run As
  • Click JUnit Test Create A Lambda Microservice
  1. You will receive the result Create A Lambda Microservice
  2. Open Command Prompt and navigate to the directory of CalculatorLambda project we extracted in the step 1
  • Execute the command mvn package to build Create A Lambda Microservice
  1. When the target JAR is built, whick is in the folder target of the project and whose name is calculator_lambda-1.0.0 Create A Lambda Microservice
  2. Go to AWS Lambda console.
  • Click Functions.
  • Click Create functions. Create A Lambda Microservice
  1. In the Create function page
  • Select Author from scratch
  • In the Name section, type idevelop_basic_calculator
  • In the Runtime section, select Java 11 (Corretto)
  • Click Change default execution role. Create A Lambda Microservice
  1. select Change default execution role section
  • Select Use an existing role
  • Select lambda-step-role
  • Click Create function Create A Lambda Microservice
  1. In the Code soure section
  • Click Upload from
  • Click .zip or .jar file Create A Lambda Microservice
  1. Click Upload
  • Select file calculator_lambda-1.0.0.jar was built in the step 7
  • Click Save Create A Lambda Microservice
  1. select Runtime settings section, click Edit Create A Lambda Microservice
  2. In the Handler section, type idevelop.lambda.CalculatorHandler
  • Click Save Create A Lambda Microservice
  1. Test Lambda function we created.
  • Select tab Test
  • In the Name section, type CalculatorTest
  • In the Event JSON section, copy the following JSON to
{
  "operator": "add",
  "operands": [1, 2]
}
  • Click Save Create A Lambda Microservice
  1. Click Test, we will receive the following result Create A Lambda Microservice
  2. Now we know the Lambda function is processing as expected, it is time to wire this function up as a Task in our Calculator Step Function.
  1. Click Next Create A Lambda Microservice
  2. Click Next Create A Lambda Microservice
  3. Click Next Create A Lambda Microservice
  4. In the Specify state machine settings page
  • In the State machine name section, type BasicCalculatorWithAddition
  • Select Permission section, select Choose an existing role
  • In the Existing roles section, select idevelop-step-functions-execution-role Create A Lambda Microservice
  • Drag the screen down, click Create state machine.
  1. Go to AWS Lambda console.
  • Click Functions.
  • Click idevelop_basic_calculator. Create A Lambda Microservice
  1. Copy Function ARN to use in the next step Create A Lambda Microservice
  2. We will change the definition of the State machine we copied.
  • Click Edit Create A Lambda Microservice
  1. We will see the definition from the previous implementation.
  • Locate the state.process.operator.add state ( line 31 ) and change its type to Task.
  • Add a new attribute Resource to state.process.operator.add vand for its value, select the idevelop_basic_calculator Lambda function you have created previously.
  • Your new definition should look like similar to this:
"state.process.operator.add": {
  "Type": "Task",
  "Comment": "Performs an addition on the operands provided",
  "Next": "state.process.displayResult",
  "Resource" : "arn:aws:lambda:ap-southeast-1:272538243902:function:idevelop_basic_calculator"
}
  • Click Save Create A Lambda Microservice
  1. If you see an IAM Role warning, you can safely ignore and Click Save anyway Create A Lambda Microservice
  2. Click Start excution Create A Lambda Microservice
  3. In the Input section, type
{
  "operator" : "add",
  "operands" : [
      1,
      2
  ]
}
  • Click Start excution. Create A Lambda Microservice
  1. If all is well, you should see a successful result Create A Lambda Microservice
  2. Click tab Excution ouput to see the result of your calculation Create A Lambda Microservice