Skip to content

Latest commit

 

History

History

solicitations

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Overview

This Sample Solution provides all required resources to deploy to the AWS cloud a fully functional SP-API application that implements the Solicitations use case end-to-end. Use this application to test the proposed solution, do changes and/or integrate it to your own product.

Solicitations API

The Selling Partner API for Solicitations (Solicitations API) helps you build applications that let sellers send consistent, high-quality solicitations to buyers. Sellers can request both a product review and seller feedback by sending a single template-based email.

If you haven't already, we recommend you to navigate the following resources:

Solution

This Sample Solution implements a solicitations workflow that reacts to incoming ORDER_CHANGE notifications and tries to submit a product review and seller feedback solicitation to the buyer at the appropriate time.

The solution consists of the following components:

  • A Step Functions state machine with a fully functional Solicitations workflow
  • Lambda functions that support each of the steps of the state machine
  • An SQS queue to receive notifications for new or updated Amazon orders
  • A Secrets Manager secret to securely store SP-API app credentials

Workflow

The application waits for incoming SP-API ORDER_CHANGE notifications. These events are processed by the SPAPISolicitationScheduler Lambda function, which creates an EventBridge schedule for each shipped order. This schedule is triggered 5 days after the order's earliest delivery date, at which point a Step Functions state machine is started.

The state machine validates the available actions for the order by leveraging the SPAPIGetSolicitationActions Lambda function, which calls the getSolicitationActionsForOrder SP-API operation. If no actions are available, it waits for one day before retrying. If the productReviewAndSellerFeedback action is available, it uses the SPAPISubmitSolicitation Lambda function to call the createProductReviewAndSellerFeedbackSolicitation SP-API operation to send a request to the buyer.

Pre-requisites

The pre-requisites for deploying the Sample Solution App to the AWS cloud are:

  • Registering as a developer for SP-API, and registering an SP-API application
  • An IAM user with permissions to create a new user, a policy, and attach it to the user
    • If you don't have one, you can create it following the steps under Usage - 2. Configure Sample Solution App's IAM user
  • The AWS CLI
    • If not present, it will be installed as part of the deployment script
  • NodeJS 14.15.0 or later
    • Required by AWS CDK stack for the sample solution deployment.
    • If not present, it will be installed as part of the deployment script.
  • The Python app requires the following packages: boto3, requests, and setuptools. If not present, they will be installed as part of the deployment script
  • GitBash
    • in case you use Windows in order to run the deployment script.

Usage

1. Update config file

To allow the Sample Solution App to connect to SP-API, the config file has to be updated to match the set-up of your SP-API application.

  1. Open app.config file and replace all occurrences of <dev_value> following the instructions below:
  2. Update ClientId and ClientSecret attribute values with Client Id and Client Secret of the SP-API application respectively
  3. Update RefreshToken attribute value with the refresh token of the selling partner you will be using for testing

Note: While updating the config file, don't leave blank spaces before and after =, and don't use quotation marks

Sample config file:

ClientId=amzn1.application-oa2-client.abc123def456xyz789
ClientSecret=amzn1.oa2-cs.v1.abc123def456xyz789
RefreshToken=Atzr|Abc123def456xyz789

2. Configure Sample Solution App's IAM user

I. Create IAM policy

In order to execute the deployment script, an IAM user with the appropriate permissions is needed. To create a new IAM policy with the required permissions, follow the steps below.

  1. Open the AWS console
  2. Navigate to IAM Policies console
  3. Click Create policy
  4. Next to Policy editor, select JSON and replace the default policy with the JSON below. Make sure to replace <aws_account_id_number> your AWS account id number
{
 	"Version": "2012-10-17",
 	"Statement": [
 		{
 			"Sid": "SPAPISampleAppIAMPolicy",
 			"Effect": "Allow",
 			"Action": [
 				"iam:CreateUser",
 				"iam:DeleteUser",
 				"iam:CreatePolicy",
 				"iam:DeletePolicy",
 				"iam:AttachUserPolicy",
 				"iam:DetachUserPolicy",
 				"iam:CreateAccessKey",
 				"iam:DeleteAccessKey",
				"iam:GetRole",
				"iam:CreateRole",
				"iam:TagRole",
				"iam:AttachRolePolicy",
				"iam:PutRolePolicy",
				"iam:DeleteRole",
				"iam:DeleteRolePolicy",
				"iam:DetachRolePolicy",
				"iam:PassRole"
 			],
 			"Resource": [
 				"arn:aws:iam::<aws_account_id_number>:user/*",
				"arn:aws:iam::<aws_account_id_number>:policy/*",
				"arn:aws:iam::<aws_account_id_number>:role/*"
 			]
 		},
 		{
			"Sid": "SPAPISampleAppCloudFormationPolicy",
			"Effect": "Allow",
			"Action": [
				"cloudformation:*",
				"ecr:*",
				"ssm:*"
			],
			"Resource": [
				"arn:aws:cloudformation:us-east-1:<aws_account_id_number>:stack/CDKToolkit/*",
				"arn:aws:ecr:us-east-1:<aws_account_id_number>:repository/cdk*",
				"arn:aws:ssm:us-east-1:<aws_account_id_number>:parameter/cdk-bootstrap/*",
				"arn:aws:cloudformation:us-east-1:<aws_account_id_number>:stack/sp-api-app*"
			]
		},
		{
			"Sid": "SPAPISampleAppCloudFormationS3Policy",
			"Effect": "Allow",
			"Action": [
				"s3:*"
			],
			"Resource": [
				"arn:aws:s3:::cdk*",
				"arn:aws:s3:::sp-api-app-bucket*"
			]
		}
 	]
 }
  1. Click Next
  2. Select a name for your policy. Take note of this value as you will need it in the next section.
  3. Review the changes and click Create policy

II. Create IAM user

To create a new IAM user with the required permissions, follow the steps below.

  1. Open the AWS console
  2. Navigate to IAM Users console
  3. Click Create user
  4. Select a name for your user
  5. In the Set permissions page, select Attach policies directly
  6. In the Permissions policies, search for the policy created in I. Create IAM policy section. Select the policy, and click Next
  7. Review the changes and click Create user

III. Retrieve IAM user credentials

Security credentials for the IAM user will be requested during the deployment script execution. To create a new access key pair, follow the steps below. If you already have valid access key and secret access key, you can skip this section.

  1. Open the AWS console
  2. Navigate to IAM Users console
  3. Select the IAM user created in II. Create IAM user
  4. Go to Security credentials tab
  5. Under Access keys, click Create access key
  6. In Access key best practices & alternatives page, select Command Line Interface (CLI)
  7. Acknowledge the recommendations, and click Next
  8. Click Create access key
  9. Copy Access key and Secret access key. This is the only time that these keys can be viewed or downloaded, and you will need them while executing the deployment script
  10. Click Done

3. Execute the deployment script

The deployment script will create a Sample Solution App in the AWS cloud. To execute the deployment script, follow the steps below.

  1. Identify the deployment script for the programming language you want for your Sample Solution App.
    1. For example, for the Python application the file is app/scripts/python/python-app.sh
  2. Execute the script from your terminal or Git Bash
    1. For example, to execute the Python deployment script in a Unix-based system or using Git Bash, run bash python-app.sh
  3. Wait for the CDK stack creation to finish
    1. Navigate to CloudFormation console
    2. Wait for the stack named sp-api-app-<language>-random_suffix to show status CREATE_COMPLETE

4. Test the sample solution

The deployment script creates a Sample Solution App in the AWS cloud. The solution consists of a Step Functions state machine with a fully functional workflow. To test the sample solution, follow the steps below.

  1. Open the AWS console
  2. Navigate to SQS console
  3. Select the SQS queue created by the deployment script, named sp-api-notifications-queue-random_suffix. Make sure not to confuse the queue with the dead-letter queue named sp-api-notifications-dead-letter-queue-random_suffix
  4. Select Send and receive messages
  5. Under Message body, insert the following simplified notification body to execute a workflow in sandbox mode. If you want to test a real order, set Sandbox to No and replace SellerId, AmazonOrderId, and all parameters under Summary with the correct values of the order that you will use for testing.
    {
        "NotificationType": "ORDER_CHANGE",
        "Payload": {
            "OrderChangeNotification": {
                "SellerId": "ABCDEF12345678",
                "AmazonOrderId": "123-1234567-1234567",
                "Summary": {
                    "MarketplaceId": "ATVPDKIKX0DER",
                    "OrderStatus": "Shipped",
                    "EarliestDeliveryDate": "2024-01-01T13:30:00.000Z",
                    "LatestDeliveryDate": "2024-12-31T13:30:00.000Z"
                }
            }
        },
        "Sandbox": "Yes"
    }
    
  6. Click Send message
  7. The solution will create a schedule set to the order's EarliestDeliveryDate + 5 days. If the date is in the past (like in the sample notification above), the schedule will be executed immediately.
  8. After the EventBridge schedule's execution date, navigate to Step Functions console
  9. Select the state machine created by the deployment script, named SPAPIStateMachine-random_suffix
  10. Under Executions, you will see a workflow for the order submitted through SQS
  11. To check the workflow status and navigate into the individual steps, select the workflow and use the Graph view and Step Detail panels

5. Extra

The deployment script also creates a Lambda function that subscribes selling partners to notifications. You can integrate this function to your product to easily onboard to the notifications feature. To test the function, follow the steps below.

  1. Open the AWS console
  2. Navigate to Lambda console
  3. Select the notification subscriber function, named SPAPISubscribeNotifications-random_suffix
  4. Select Test tab
  5. Under Event JSON, insert the following payload. Replace RefreshToken, RegionCode and NotificationType with the corresponding values of the selling partner and notification type you want to subscribe to.
    {
        "NotificationType": "ORDER_CHANGE",
        "RegionCode": "NA|EU|FE",
        "RefreshToken": "Atzr|Iw..."
    }
    
  6. Click Test
  7. The function will return destination Id and subscription Id

6. Clean-up

The deployment script creates a number of resources in the AWS cloud which you might want to delete after testing the solution. To clean up these resources, follow the steps below.

  1. Identify the clean-up script for the programming language of the Sample Solution App deployed to the AWS cloud.
    1. For example, for the Python application the file is app/scripts/python/python-app-clean.sh
  2. Execute the script from your terminal or Git Bash
    1. For example, to execute the Python clean-up script in a Unix-based system or using Git Bash, run bash python-app-clean.sh

7. Troubleshooting

If the state machine execution fails, follow the steps below to identify the root-cause and retry the workflow

  1. Navigate to Step Functions console
  2. Select the state machine created by the deployment script, named SPAPIStateMachine-random_suffix
  3. Under Executions, you can use the Status column to identify failed executions
  4. To troubleshoot errors, select the corresponding workflow execution and use the Graph view and Step Detail panels
  5. After fixing the issues that caused the error, retry the workflow by clicking on New execution. The original input parameters will be automatically populated
  6. Click Start execution, and validate the results