Update Unicorn Management Service

Update Unicorn Management Service

  1. First, we grant IAM permission to Lambda.
  • Update the SAM template code/lab-1/template.yaml:
- SNSPublishMessagePolicy:
    TopicName: !GetAtt RideCompletionTopic.TopicName

Working with SNS

  1. Next, we must grant the Amazon SNS topic ARN to Lambda.
  • Continue updating the SAM template code/lab-1/template.yaml:
TOPIC_ARN: !Ref RideCompletionTopic

Working with SNS

  1. Then, we will update the Lambda function to call Amazon SNS.
  • Open the file code/lab-1/unicorn-management-service/app.py:
sns = boto3.client('sns', config=config)

Working with SNS

  1. Next, we will put an item into DynamoDB:
    response = sns.publish(
        TopicArn=TOPIC_ARN,
        Message=json.dumps(request),
        MessageAttributes = {
            'fare': {
                'DataType': 'Number',
                'StringValue': str(request['fare'])
            },
            'distance': {
                'DataType': 'Number',
                'StringValue': str(request['distance'])
            }
        }
    )

Working with SNS

  1. Update the template using the following command:
cd ~/wild-rydes-async-messaging/code/lab-1
sam build --use-container

Working with SNS

  1. After updating, we deploy using the following command:
sam deploy

Working with SNS