Service Discovery using Amazon Cloud Map

   Go back to the Task List

  « 4. Create DynamoDB Table    6. Create Lambda Function to read data »

5. Create Lambda Function to write data

In this task, you create a Lambda function which is used to write data to 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 writefunction 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
    import random
    
    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.put_item( Item={ 'id': str(random.randint(1,100)), 'todo': event } )
           
        return {
            'statusCode': 200,
            'body': json.dumps(response)
        }
    

    `

  4. The code above shows how to use Cloud Map to query / discover the resources used in the application and then use it. 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 query parameters. Since the Lambda function is trying to discover the data table, it is asking for the name=datatable resource. You configured the discovery of the table in the previous step. You fetched the tablename attribute from the service discovery query response. You then created dynamodb client and used put_item method to put the data in the table. As data, along with primary key id, you are also inserting data for a column todo. The todo data is passed using the event parameter of the Lambda function. The table primary key is generated using python random function (I know it is not full proof for the uniqueness but was simple to use especially for the query).

  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 dojowriteinstance for the Service instance ID field. Add two custom attributes - 1) key = name, value = writeservice 2) key = function, value = writefunction. 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 the Lambda function which reads data from the table.