At this point we have created a Lambda with an API Gateway trigger that returns our array of JSON objects. In this section of the lab, we will modify our API Gateway Lambda to utilize the boolEnableLimitResults
and intResultLimit
properties of our configuration document to enable a new feature that will limit the number of results returned by API Gateway as well as utilize the value of intResultLimit
to determine how many results to return.
We will start with updating our Lambda configuration to utilize the AWS AppConfig integration with Lambda extensions and then modify our Lambda code to retrieve the configuration data from the AWS AppConfig Lambda extension and extract from it the boolean value for boolEnableLimitResults. We will use that value as a means to turn on or off our filtering feature.
Recall that our configuration document that we stored in the hosted configuration store and subsequently deployed looks like this:
{
"boolEnableLimitResults": false,
"intResultLimit": 0
}
Note that boolEnableLimitResults is set to false. This is common practice to deploy a feature or block of new code disabled by default. Once successfully deployed, we can turn it on by changing the value of boolEnableLimitResults
to true
, observe for errors and turn it off if we encounter any errors. This helps avoid a hotfix on production, for example.
In order to enable our API Gateway Lambda to integrate with AppConfig, we need to add permissions to the Lambda to access our AppConfig Application.
Use the following procedure to add the necessary permissions to the Lambda execution policy to allow our API Gateway Lambda to call the AppConfig GetConfiguration API.
{
"Effect": "Allow",
"Action": "appconfig:GetConfiguration",
"Resource": "arn:aws:appconfig:us-east-1:YOUR_ACCOUNT_NUMBER:*"
}
We will utilize the AWS AppConfig integration with Lambda extensions to retrieve the configuration data from the local configuration cache. The following diagram shows how it works.
The next time the Lambda function is invoked, the extension checks the elapsed time since it retrieved a configuration from AWS AppConfig. If the elapsed time is greater than the poll interval, the extension retrieves the latest configuration from AWS AppConfig and updates the cache.
Visit AWS AppConfig integration with Lambda extensions documentation for more information.
Use the following procedure to configure the AWS AppConfig integration with Lambda extensions:
The AWS_APPCONFIG_EXTENSION_HTTP_PORT environment variable specifies the port on which the local HTTP server that hosts the extension runs. The default value is 2772.
The AWS_APPCONFIG_EXTENSION_POLL_INTERVAL_SECONDS environment variable controls how often the extension polls AWS AppConfig for an updated configuration in seconds. The default value is 45 seconds.
The AWS_APPCONFIG_EXTENSION_POLL_TIMEOUT_MILLIS environment variable controls the maximum amount of time the extension waits before retrieving configurations from the cache in milliseconds. The default value is 3000
Use the following procedure to integrate AppConfig with our API Gateway Lambda.
const http = require('http');
exports.handler = async (event) => {
const res = await new Promise((resolve, reject) => {
http.get(
"http://localhost:2772/applications/AppConfigLab/environments/AppConfigLabAPIGatewayDevelopment/configurations/AppConfigLabHostedConfigurationProfile",
resolve
);
});
let configData = await new Promise((resolve, reject) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('error', err => reject(err));
res.on('end', () => resolve(data));
});
let result = getServices();
const parsedConfigData = JSON.parse(configData);
// add result limit code here
const response = {
statusCode: 200,
body: parsedConfigData,
};
return response;
};
function getServices() {
return [
{
name: 'AWS AppConfig'
},
{
name: 'Amazon SageMaker Studio'
},
{
name: 'Amazon Kendra'
},
{
name: 'Amazon CodeGuru'
},
{
name: 'Amazon Fraud Detector'
},
{
name: 'Amazon EKS on AWS Fargate'
},
{
name: 'AWS Outposts'
},
{
name: 'AWS Wavelength'
},
{
name: 'AWS Transit Gateway'
},
{
name: 'Amazon Detective'
}
];
}
Observe the code and note that we first added code to require http
which is an HTTP interface built in to Node.js.
const http = require('http');
We then added code to call the AWS AppConfig Lambda extension. Note that the hostname for our http API call is localhost which illustrates the call to the local cache created by the AWS AppConfig Lambda extension. The port is set to 2772 which was specified in our Lambda environment variable AWS_APPCONFIG_EXTENSION_HTTP_PORT.
The url format http://localhost:2772/applications/APPLICATION_NAME/environments/ENVIRONMENT_NAME/configurations/CONFIGURATION_NAME
where APPLICATION_NAME is AppConfigLab - the name we used when we created our AWS AppConfig Application earlier in the lab. Similarly, ENVIRONMENT_NAME is AppConfigLabAPIGatewayDevelopment, the Environment name we used, and CONFIGURATION_NAME is AppConfigLabHostedConfigurationProfile, the name chosen for our Configuration profile.
const res = await new Promise((resolve, reject) => {
http.get("http://localhost:2772/applications/AppConfigLab/environments/AppConfigLabAPIGatewayDevelopment/configurations/AppConfigLabHostedConfigurationProfile", resolve);
});
let configData = await new Promise((resolve, reject) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('error', err => reject(err));
res.on('end', () => resolve(data));
});
Lastly, we changed the response to actually include the parsed configuration data just to test the integration all the way through. We can expect that if we perform a test execution of our API Gateway or Lambda that the body will now contain the actual configuration data we included in our hosted configuration instead of a list of AWS Services.
const parsedConfigData = JSON.parse(configData);
// add result limit code here
const response = {
statusCode: 200,
body: parsedConfigData,
};
Use the following procedure to verify the Lambda can retrieve the configuration.
{
"statusCode": 200,
"body": {
"boolEnableLimitResults": false,
"intResultLimit": 0
}
}
That’s it! We have now successfully configured the permissions necessary for Lambda to retrieve our AppConfig configuration data and modified our Lambda code to retrieve the data and return it in the body of our response as a means to test the integration between Lambda and AppConfig.
Next up in the lab, we will modify our Lambda code to use the boolEnableLimitResults value of our configuration data to enable a new feature that will allow us to the limit the number objects returned by the API. Additionally, we will use the value of the intResultLimit property to configure the number of items to return in the API response when the feature is enabled.