AWS Serverless Application Model (SAM) allows developers to build and test serverless applications locally. When working with AWS App Config and Python Lambda functions, it is crucial to understand how to test the setup locally. This article will explain the process of testing an AWS SAM local App Config Python Lambda in local development mode.
Prerequisites
To get started, ensure you have the following tools installed:
- Python 3.8 or later
- AWS CLI
- AWS SAM CLI
- Docker
Setting up an AWS App Config Environment
The first step is to set up an AWS App Config environment in your AWS Management Console. Follow the steps in the documentation to create an application and environment:
AWS AppConfig Getting Started Guide
Creating a Python Lambda Function
Next, create a Python Lambda function that will receive configuration data from App Config. Here's an example of a simple Lambda function:
import json
def lambda_handler(event, context):
configuration = event['configuration']
monitored_data = configuration['monitoredData']
print('Received monitored data:', monitored_data)
return {
'statusCode': 200,
'body': json.dumps('Configuration applied successfully!')
}
Configuring the AWS SAM Template
To integrate your Lambda function with App Config, you need to update your AWS SAM template. Here's an example of a template.yaml file:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Serverless Application
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
Runtime: python3.8
Events:
AppConfigEvent:
Type: AWS::AppConfig::Configuration
Properties:
ApplicationName: !Ref MyApplication
ConfigurationName: !Ref MyConfiguration
Replace MyApplication and MyConfiguration with the names of your AWS AppConfig application and configuration.
Testing the Lambda Function Locally
Use the following command to test your Lambda function locally. Replace MyApplication and MyConfiguration with the names of your AWS AppConfig application and configuration.
sam local invoke MyFunction --event event.json
Here's an example event.json file:
{
"configuration": {
"monitoredData": {
"exampleKey": "exampleValue"
}
}
}
Setting up the AppConfig Agent and Lambda Extension
To enable local development mode for App Config, follow these steps:
- Create the AppConfig agent manifest file with the following content:
{
"name": "appconfig-deploy",
"version": "1.0.0",
"config": {
"aws": {
"region": <your-region>
}
}
}
Replace <your-region> with your AWS region.
- Add the following to your .aws/sam/config.toml file:
[dev.appconfig]
agent_manifest = "appconfig-agent-manifest.json"
extension_config = "appconfig-extension-config.json"
Create an empty appconfig-extension-config.json file.
Testing the Agent and Extension in Local Development Mode
After setting up the agent and extension for local development mode, use the following command to test the Lambda function:
sam local start-api
You can now update and monitor the Lambda function using the AppConfig application. The changes will be reflected in your local development environment.
In this article, we have discussed how to test an AWS SAM local App Config Python Lambda in local development mode. By following the steps outlined, you can test and iterate on your serverless applications locally with App Config.