If you are delivering your applications as a Software as a Service (SaaS), maintaining a single version of your solution is important to keeping your operational efficiency. However, to make your offering attractive to the widest variety of customers, you also need to package your solution with flexible pricing options. Keeping your desired operational efficiency requires tools like configuration management that allow you to offer variations on your solution.
In this hands-on coding example, we demonstrate techniques for using AWS AppConfig (AppConfig) to configure our SaaS solution, allowing us to package it into pricing tiers with a variety of feature sets enabled for our tenants.
The goal is to provide SaaS developers and architects with working code that illustrates how to use AppConfig in designing SaaS solutions without going to the extent of making a full, production-ready solution. Instead, we're hoping this can jump start your process and address some of the common challenges that you may face.
The instructions below are intended to give you a step-by-step walk through for getting this solution up and running in your own AWS account. For a general description and overview of the solution, please see the developer's documentation.
Note : This is not production grade and simply meant as a hands-on coding example.
Figure 1 represents the high-level architecture of this sample SaaS solution. As mentioned before, please see the developer's documentation to understand the architecture in more detail.
Figure 1: High Level Architecture
Before you begin, ensure you have:
- An AWS Account with the appropriate permissions.
- AWS CLI configured.
- Python 3+
- Node and NPM
- Docker running. This is due to the project's dependency on
aws-cdk.aws-lambda-python-alpha
, which leverages Docker for bundling Python Lambda functions. (More information on aws-lambda-python-alpha).
Deploy this sample in:
- us-east-1 (Default region)
- us-east-2
- us-west-1
- us-west-2
Clone the repository
git clone [email protected]:aws-samples/leveraging-aws-appconfig-for-enabling-saas-pricing-tiers.git
Move into the cloned repository
cd leveraging-aws-appconfig-for-enabling-saas-pricing-tiers
-
For AWS Cloud9 (Cloud9) users
We recommend selecting an instance type of at least
t3.small
Resize the Amazon EBS volume to 20 GiB
chmod +x resize.sh ./resize.sh 20
Reboot the instance. It could take a minute or two for the IDE to come back online
sudo reboot
Create and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate
Set the AWS_DEFAULT_REGION
environment variable to determine the AWS region to deploy the resources. Ensure the chosen region is supported.
export AWS_DEFAULT_REGION=REGION
Install the necessary dependencies
pip install -r requirements.txt
(Optional) Bootstrap AWS CDK (CDK) on the target account and region
Note: This is required if you have never used CDK before on this account and region combination. (More information on CDK bootstrapping).
cdk bootstrap aws://{ACCOUNT-NUMBER}/{REGION}
Deploy the application using CDK, ensuring deployment in the supported regions
cdk deploy --all
After a successful deployment, take note of the displayed values: BackendStack.BackendApiURL
, IdentityStack.UserPoolId
, and IdentityStack.UserPoolClientId
. You will use them in the next step.
Note: You can also retrieve these values by visiting the AWS CloudFormation console. Select the appropriate stack (e.g., BackendStack, IdentityStack) and navigate to the “Outputs” tab to view these details.
Move into the app directory
cd app/
Update app/src/aws-exports.js
with your specific AWS resource details from the previous step
const config = {
region: 'YOUR_REGION',
api_gateway: {
regional_endpoint: 'YOUR_API_URL',
},
user_pool_id: 'YOUR_USER_POOL_ID',
user_pool_client_id: 'YOUR_USER_POOL_CLIENT_ID',
};
export default config;
Install dependencies and run the app locally
npm install
npm run start
The application is structured into four CDK stacks, each deploying and managing specific resources. The file serves as the entry point for deploying these stacks.
Note: In this sample application,
RemovalPolicy.DESTROY
is used to ensure that the AWS resources created are automatically removed when the stack is deleted. This is helpful in maintaining a clean testing environment and avoiding unwanted costs. ConsiderRemovalPolicy.RETAIN
orRemovalPolicy.SNAPSHOT
for production environments to safeguard against accidental data loss. (More information on RemovalPolicy).
- backend_stack.py configures and deploys AWS Lambda, Amazon API Gateway, and related resources.
- identity_stack.py configures and deploys Amazon Cognito.
- data_stack.py configures and deploys Amazon DynamoDB.
- config_stack.py configures and deploys AppConfig.
Avoid unwanted charges by cleaning up the resources you've created. To do so, navigate to the root of the repository and run:
cdk destroy --all
Note: If you used Cloud9 to deploy the architecture, then make sure to delete that instance. This will not be done by the command, and will need to be done manually.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.