This codebase is for a sample e-commerce store used for demo purposes. The code features basic AWS Lambda functions for products, while the UI is uploaded to an Amazon S3 bucket. The following code was largely adapted from FourTheorem and AWS-Samples for Sedai -- an autonomous cloud cost optimization software.
To prepare the frontend assets for deployment, run the build command:
npm run build --workspace=frontend
Export the desired S3 bucket name as an environment variable. This makes it easier to reference in subsequent steps:
export FRONTEND_BUCKET=retaildemostore
Create an S3 bucket that will host the frontend assets:
aws s3 mb s3://$FRONTEND_BUCKET
Copy the built frontend assets to the previously created S3 bucket:
aws s3 cp frontend/dist "s3://$FRONTEND_BUCKET" --recursive
To make the frontend assets publicly accessible, create a bucket policy with the following content. Save this to a file named policy.json
:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::<FRONTEND_BUCKET>/*"
]
}
]
}
Then, apply the policy to the S3 bucket:
aws s3api put-bucket-policy --bucket $FRONTEND_BUCKET --policy file://policy.json
Create a DynamoDB table named product-table
to store product data:
aws dynamodb create-table \
--table-name product-table \
--attribute-definitions AttributeName=id,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
cd backend/
Install the required AWS SDK libraries for working with DynamoDB and SQS:
npm i --save @aws-sdk/client-dynamodb
npm i --save @aws-sdk/lib-dynamodb
npm i --save @aws-sdk/client-sqs
Check if the SAM template is valid:
sam validate
Build the backend using the SAM CLI:
sam build --beta-features
Deploy the backend using the SAM CLI. Follow the guided prompts:
sam deploy --guided
After deployment, you should receive an API ID as output.
Using the provided API ID, create a settings.json
file with the following content:
{
"apiBaseUrl": "https://<restapiid>.execute-api.eu-west-1.amazonaws.com/Prod"
}
Finally, upload the settings.json
file to the .well-known
directory in the S3 bucket:
aws s3 cp settings.json "s3://$FRONTEND_BUCKET/.well-known/settings.json"
After following these steps, the Serverless Retail Demo Store should be successfully deployed!