Service Discovery using Amazon Cloud Map

   Go back to the Task List

  « 5. Create Lambda Function to write data    7: Setup Development Environment »

6. Create Lambda Function to read data

In this task, you create a Lambda function which is used to read data from the data service. The Lambda function is also registered with the Cloud Map for the discovery purpose.

  1. Goto AWS Lambda console, click on the Create function button.

    Cloud Map

  2. On the next screen, select option Author from scratch. Type in readfunction as the function name. Select Python 3.8 as the runtime. For the permissions, select Use an existing role option and select dojolambdarole as the role. Click on the Create function button.

    Cloud Map

  3. The function is created in no time. Goto the Function code section and update the code as following.

    Cloud Map

    import json
    import boto3
    
    def lambda_handler(event, context):
        serviceclient = boto3.client('servicediscovery')
    
        response = serviceclient.discover_instances(NamespaceName='dojoappnamespace', ServiceName='dataservices',  QueryParameters={ 'name': 'datatable' })
           
        tablename = response["Instances"][0]["Attributes"]["tablename"]
           
        dynamodbclient = boto3.resource('dynamodb')
           
        table = dynamodbclient.Table(tablename)
           
        response = table.get_item(Key={'id': event})
    
        return {
            'statusCode': 200,
            'body': json.dumps(response)
        }
    

    `

  4. In the code above, you created a service discovery client. You used discover_instances method to search the service instance using namespace, service name and service instance name custom attribute as the parameters. You fetched the tablename attribute from the service discovery query response. You then created DynamoDB client and use get_item method to fetch the data from the table using the primary key id. The primary key id is passed using the Lambda event parameter.

  5. Click on the Save button to save the lambda function.

    Cloud Map

  6. The next step is to register the lambda function in the Cloud Map. Goto Amazon Cloud Map console, open dojoappnamespace namespace and then open appservices service. Then click on the Register service instance button.

    Cloud Map

  7. On the next screen, select Identifying information for another resource option for the instance type. Type in dojoreadinstance for the Service instance ID field. Add two custom attributes - 1) key = name, value = readservice 2) key = function, value = readfunction. Click on the **Register service instance button.

    Cloud Map

  8. The Lambda function is registered as the service instance in no time. In the configuration above, key = name is used to search and discover the Lambda function and key = function is used to fetch the actual Lambda function name to invoke it.

  9. In the next task, you create Amazon Cloud9 development environment which works as the frontend to call the application services.