Create Serverless Application

Now that we have created and deployed an AWS AppConfig Configuration Document, it is available to applications to consume the configuration data and use it.

In this section of the lab, we will create an Amazon API Gateway and AWS Lambda Serverless Application that will ultimately consume the configuration data we have deployed to the development environment.

Create an AWS Lambda

Use the following procedure to create an AWS Lambda.

  1. Visit the Lambda Console and select Create function.
  2. Choose the Author from scratch option.
  3. Enter AppConfigLabAPIGatewayLambda in the Function name field.
  4. Choose Node.js 12.x from the Runtime dropdown list.
  5. Expand the Permissions section and choose the option to Create a new role with basic Lambda permissions from the Execution role options. AppConfig Create API Gateway Lambda
  6. Select Create function.
  7. Replace the contents of the Function code with the following code and select Deploy.
    exports.handler = async (event) => {
      let result = getServices();
    
      const response = {
        statusCode: 200,
        body: result,
      };
      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'
        }
      ];
    }
    

    Note that this block of code simply returns an array of objects (sample list of AWS Services) that includes the name property.

Test Our Lambda Function

Use the following procedures to verify the Lambda returns an array of objects (sample list of AWS Services) that includes the name property:

  1. Select Test from the top right.
  2. Select Create new test event and choose hello-world from the Event template dropdown list.
  3. Enter AppConfigTestEvent in the Event name field.
  4. Do not change the JSON value for the event.
  5. Select Create to finish creating the test event.
  6. Select Test from the top right again.
  7. Expand Details from the Execution result section and verify you observe the following result returned.
    {
      "statusCode": 200,
      "body": [
        {
          "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"
        }
      ]
    }
    

Create Amazon API Gateway

Next, we will create an Amazon API Gateway that we will later integrate with the Lambda we just created.

Use the following procedures to create an AWS API Gateway:

  1. Open a new browser tab or window so that you can easily toggle back and forth between your AWS Lambda and Amazon API Gateway.
  2. In the new browser tab or window, visit API Gateway Console and select Create API.
  3. Locate the REST API with the description Develop a REST API where you gain complete control over the request and response along with API management capabilities and select Build.
  4. Select New API
  5. In the Settings section, enter AppConfigLabAPI in the API name field, AWS AppConfig Lab API Gateway in the Description field.
  6. Select Regional from the Endpoint Type dropdown list. AppConfig Create API Gateway
  7. Select Create API.

Configure Amazon API Gateway & AWS Lambda Integration

Now, we will configure the Amazon API Gateway and AWS Lambda integration so that when we call our API, it will return the results we demonstrated during our AWS Lambda test execution.

Use the following procedures to configure the Amazon API Gateway and AWS Lambda integration:

  1. Choose the root resource / under Resources. From the Actions menu, choose Create Method.
  2. Choose GET from the Method dropdown list and then select the check mark to complete the save operation.
  3. Choose Lambda Function from the Integration type options.
  4. Leave Use Lambda Proxy Integration unchecked.
  5. Choose us-east-1 from the Lambda Region dropdown list.
  6. Enter AppConfigLabAPIGatewayLambda in the Lambda Function field to select the Lambda we created in the previous section and leave Use Default Timeout option checked. AppConfig API Gateway Create Method
  7. Select Save and choose OK on the Add Permission to Lambda Function popup.
  8. Select TEST and then Test again to test the integration between API Gateway and Lambda. The results should indicate a Status value of 200, and the Response Body should appear as shown below and should include an array of objects each with the name property. AppConfig API Gateway Initial Test

At this point in the lab, we have created an Amazon API Gateway and integrated it with an AWS Lambda that returns a simple array of JSON objects each of which contains the name property.

Next, we will configure the AWS Lambda to enable AWS AppConfig integration which will include adding permissions for AWS AppConfig to invoke the AWS Lambda as well as modifying the code to retrieve the configuration data for use in our serverless application.