> ## Documentation Index
> Fetch the complete documentation index at: https://cloud-architect.ipoint-labs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Build API Gateway with different stages using stage variables

> Hands-on lab · 15m

Launch this lab in the IP Lab Portal, then follow the steps below in the AWS console.

<a className="ip-lab-portal-btn" href="https://labs.intellectualpoint.com/labs/build-api-gateway-with-different-stages-using-stage-variables" target="_blank" rel="noreferrer">
  Open IP Lab Portal
</a>

## Overview

### Lab Details

1. This lab walks you through the steps to create an API Gateway with two different stages and integrate it to two different lambda functions.
2. You will practice using Amazon API Gateway.
3. Duration: **75 minutes.**
4. AWS Region: **(N. Virginia)** **us-east-1.**

### Introduction

#### EC2

* AWS defines it as an Elastic Compute Cloud.
* It’s a virtual environment where “you rent” to have your environment created, without purchasing.
* Amazon refers to these virtual machines as instances.
* Preconfigured templates can be used to launch instances. These templates are referred to as images. Amazon provides these images in the form of AMIs (Amazon Machine Images).
* Allows you to install custom applications and services.
* Scaling of infrastructure, i.e., up or down, is easy based on the demand you face.
* AWS provides multiple configurations of CPU, memory, storage etc., through which you can pick the flavor that's required for your environment.
* No limitation on storage. You can pick the storage based on the type of the instance that you are working on.
* Temporary storage volumes are provided, which are called Instance Store Volumes.  Data stored in this gets deleted once the instance is terminated.
* Persistent storage volumes are available and are referred to as EBS (Elastic Block Store) volumes.
* These instances can be placed at multiple locations, which are referred to as Regions and Availability Zones (AZ).
* You can have your Instances distributed across multiple AZs i.e., within a single Region, so that if an instance fails, AWS automatically remaps the address to another AZ.
* Instances deployed in one AZ can be migrated to another AZ.
* To manage instances, images, and other EC2 resources, you can optionally assign your own metadata to each resource in the form of tags.
* A Tag is a label that you assign to an AWS resource.  It contains a key and an optional value, both of which are defined by you.
* Each AWS account comes with a set of default limits on the resources on a per-Region basis.
* For any increase in the limit, you need to contact AWS.
* To work with the created instances, we use Key Pairs.

####

#### IAM

* Stands for Identity and Access Management.
* Web service that helps the user securely control access to AWS resources.
* Used to control who is authenticated and authorized to use AWS resources.
* The first “identity” is the creation of an account in the AWS portal.  On providing the email and password, an Identity is created, and that's the “root user” holding all the permissions to access all resources in AWS.
* The primary resources in IAM are users, groups, roles, policies, and identity providers.
* IAM Group is a collection of IAM Users. You use groups to specify permissions for a collection of users, which can make those permissions easier to manage for those users.
* IAM roles are like IAM Users in that they are both identities with permission policies that determine what the owner can access.
* IAM Roles do not have any credentials associated with them.
* IAM Roles are intended to be assumable by anyone who needs them.
* IAM can be used from the AWS CLI, AWS SDK and AWS Management Console.

#### Amazon API Gateway

* Amazon API Gateway is a **fully managed service** that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale.
* APIs act as the **front door** for applications to access data, business logic, or functionality from your backend services.
* API Gateway handles all the tasks involved in accepting and processing up to hundreds or thousands of concurrent API calls, including traffic management, CORS support, authorization and access control, throttling, monitoring, and API version management.
* Using API Gateway, you can create RESTful APIs and WebSocket APIs that enable real-time two-way communication applications. API Gateway supports containerized and serverless workloads, as well as web applications.
* AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume.
* With Lambda, you can run code for virtually any type of application or backend service – all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.

###

### Architecture Diagram

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/001.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=405c6392936f225a344cac7d12ab67fc" alt="" width="2227" height="1180" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/001.png" />

### Task Details

1. Sign into the AWS Management Console.
2. Create an EC2 instance.
3. Create two Lambda Function.
4. Create a new API.
5. Create a Resource.
6. Create a Method.
7. Run the CLI command to give lambda permissions to API using the EC2 instance.
8. Deploy API Gateway with two different stages.
9. Add stage variables to both stages.
10. Test the API Gateway.

### Launching Lab Environment

1. To launch the lab environment, click on the **Start Lab** button.
2. Please wait until the cloud environment is provisioned. It will take less than a minute to provision.
3. Once the Lab is started, you will be provided with **IAM username**, **Password**, **Access** **Key**, and **Secret** **Access** **Key**.

> **Note** : You can only start one lab at any given time

## Lab guide

### Lab Steps

#### Task 1: Sign in to AWS Management Console

1. Click on the **Open Console** button, and you will get redirected to AWS Console in a new browser tab and it will be Logged in Successfully.
2. On the AWS Console, in the search bar search for **IAM** and click on it.
3. Then Click on **IAM Users** and select **TerraformUser-XXXXX**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/u9ozJNi80wMhhqMj/images/labs/access-s3-from-private-ec2-instance-using-vpc-endpoint/002.png?fit=max&auto=format&n=u9ozJNi80wMhhqMj&q=85&s=0f08b499adbb8238a6c3002c1d5280cc" alt="" width="1472" height="774" data-path="images/labs/access-s3-from-private-ec2-instance-using-vpc-endpoint/002.png" />
4. Click on **Create Access Key**.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/u9ozJNi80wMhhqMj/images/labs/access-s3-from-private-ec2-instance-using-vpc-endpoint/003.png?fit=max&auto=format&n=u9ozJNi80wMhhqMj&q=85&s=eb7aabd9a6c1d61ddacca5d1ed00bf2b" alt="" width="2892" height="718" data-path="images/labs/access-s3-from-private-ec2-instance-using-vpc-endpoint/003.png" />
5. Choose **Other**, Click on **Next** and click on **Create Access Key.**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/u9ozJNi80wMhhqMj/images/labs/access-s3-from-private-ec2-instance-using-vpc-endpoint/004.png?fit=max&auto=format&n=u9ozJNi80wMhhqMj&q=85&s=d6dff4ae8d358ff810d879e8653edf67" alt="" width="2390" height="1378" data-path="images/labs/access-s3-from-private-ec2-instance-using-vpc-endpoint/004.png" />
6. Your **Access and Secret key** will get Created. Make a note of it for later use.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/u9ozJNi80wMhhqMj/images/labs/access-s3-from-private-ec2-instance-using-vpc-endpoint/005.png?fit=max&auto=format&n=u9ozJNi80wMhhqMj&q=85&s=31dcaac5e27990f7cb20ef4a1abf2eb4" alt="" width="1858" height="1004" data-path="images/labs/access-s3-from-private-ec2-instance-using-vpc-endpoint/005.png" />

#### Task 2: Launching an EC2 Instance

In this task, we are going to allow the user to create an EC2 instance, which will be used later in the lab to run CLI commands and perform actions related to the API Gateway and Lambda functions.

1. Make sure you are in the US East **(N. Virginia) us-east-1** Region.
2. Navigate to EC2 by clicking on the **Services** menu in the top, then click on **EC2** in the **Compute** section.
3. Navigate to **Instances** on the left panel and click on **Launch instances**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/002.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=e8388b36e5ea07c5a1bda5e81624ad0d" alt="" width="1615" height="316" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/002.png" />

4. Name : Enter **MyEC2CLIInstance**

5. **For Amazon Machine Image (AMI):** Search for **Amazon Linux 2023 AMI** in the search box and click on the **Select** button.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/003.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=f48732ba1c4fda80cdb33dba44a87958" alt="" width="930" height="466" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/003.png" />

6. **For Instance Type:** select ***t2.micro***

7. **For Key pair:** Select **Create a new key pair** Button

   * Key pair name: **WhizKey**
   * Key pair type: **RSA**
   * Private key file format: **.pem**

8. Select **Create key pair** Button.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/yFeE0-3NqSktrM05/images/labs/aws-vpc-nacl-lab-case-study/011.png?fit=max&auto=format&n=yFeE0-3NqSktrM05&q=85&s=01869a444c2a6588664c0c080da24438" alt="" width="750" height="780" data-path="images/labs/aws-vpc-nacl-lab-case-study/011.png" />

9. In Network Settings, Click on **Edit** Button:

* Auto-assign public IP: **Enable**
* Select **Create new Security group**
* Security group name : Enter **MyEC2Server\_SG**
* Description : Enter **Security Group to allow traffic to EC2**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/004.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=322779838750f20a6ec35ea825de106a" alt="" width="975" height="472" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/004.png" />

* Check Allow SSH from and Select Anywhere from dropdown
* To add **SSH**,

  * Choose Type: **SSH**
  * Source: Select **Anywhere**

10. Under **Advanced Settings,** choose IAM Instance profile as **EC2\<RANDOM\_NUMBER>**.
11. Keep rest thing Default and Click on **Launch Instance** Button.
12. Select **View all Instances** to View the Instance you created.
13. **Launch Status:** Your instances are now launching, Navigate to **Instances** page from left menu and wait the status of the EC2 Instance changes to running and health check status changes to **2/2 checks passed.**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/005.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=262a9b7446589e4819dbf6bddabbcc77" alt="" width="697" height="72" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/005.png" />

#### Task 3: Create two Lambda Functions

In this task, we are going to create two Lambda functions, namely ProductionLambda and TestingLambda. These functions will be integrated with the API Gateway in subsequent tasks.

1. Navigate to the **Services** menu at the top, then click on **Lambda** under the **Compute** section.
2. Click on **Create a function**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/006.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=2923e89ae92287250fd73f5986e3f222" alt="" width="1847" height="496" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/006.png" />

* Select **Author from Scratch**
* Function Name : Enter **ProductionLambda**
* Runtime : Select **Python 3.12**
* Expand Change default execution role
* Execution Role : Click on **Use an existing role** and choose **whiz\_lambda\_role-\<random\_number>**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/007.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=11c00bb8ed3f01891919f8c115560089" alt="" width="1901" height="532" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/007.png" />

3. Click on the **Create function**
4. Once the Lambda Function is created successfully **copy the Function ARN.**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/008.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=a7d332cf73c046b1e91c58f903317475" alt="" width="2838" height="930" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/008.png" />

5. Function code : Replace the existing code with the below and then click on **Deploy**.

```plaintext theme={null}
import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Message from production Lambda.')
    }
```

6. Repeat the step 2 and 3 to create one more Lambda function with,

   * Function Name : Enter **TestingLambda**
   * Runtime : Select **Python 3.12**
   * Execution Role : Click on **Use an existing role**
   * Existing role : Select **whiz\_lambda\_role-\<random\_number>**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/007.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=11c00bb8ed3f01891919f8c115560089" alt="" width="1901" height="532" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/007.png" />

* Once the Lambda Function is created successfully **copy the Function ARN.**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/009.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=fcefae87b3dafc833f70a24bc880a0d6" alt="" width="2832" height="850" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/009.png" />

* Function code : Replace the existing code with the below and then click on **Deploy** button.

```plaintext theme={null}
import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Message from testing Lambda')
    }
```

#### Task 4: Create an API

In this task, we are going to enable the user to create a new API in API Gateway called WhizlabAPI. The API will serve as the entry point for accessing the Lambda functions.

1. Navigate to the **Services** menu at the top, then click on **API Gateway** in the **Networking and Content Delivery** section.
2. Click on Build under **REST API**
3. Choose the protocol: Select **REST**
4. Choose create new API as **New API,** Under settings, choose the API name **IP Lab Portal API.** Leave other options as default and click on **Create API.**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/010.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=2e1c72e69384cfdc8008e6c4328ca303" alt="" width="1628" height="1236" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/010.png" />

#### Task 5: Creating a Resource

In this task, we are going to assist the user in creating a resource within the API. In this case, the resource will be used to define the endpoint for accessing the Lambda functions.

1. Once the API is created, select the API.
2. Select **Create Resource**.

* Resource Name: Enter **whizlabsapi**

3. Enter the resource name and click on **Create Resource** button

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/011.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=69857b13733175a438593a489d021032" alt="" width="1936" height="806" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/011.png" />

#### Task 6: Creating a Method

In this task, we are going to guide the user in creating a method for the resource. The method, specifically a GET method, will be integrated with the Lambda functions using the Lambda Proxy integration.

1. Once you create a resource **whizlabsapi**, click on **Create method**. Select **Get** in the drop-down list.
2. Select the Integration Type as **Lambda Function**
3. Use Lambda Proxy integration: Check the checkbox
4. Lambda Region: Select **us-east-1**
5. Lambda Function : Enter ***\$\{stageVariables.functionName}***

> **Note**: Don't select the lambda function name, only enter **\$\{stageVariables.functionName}**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/012.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=03f0fd9eb7461946033d9547e6d52e3e" alt="" width="1598" height="1312" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/012.png" />

6. Click on the **Save** and Copy the **Source-ARN**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/013.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=849242a17574985dd0582203bf311a07" alt="" width="876" height="307" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/013.png" />

#### Task 7: Run the CLI command to give lambda permissions to the API using the EC2 instance

?In this task, we are going to run the CLI command to give lambda permissions to the API using the EC2 instance.

1. Navigate to the EC2 page and make sure you're in the **us-east-1 (N.Virginia)** region.

> **Note: Don’t close the API Gateway page**

2. Click on **Instances** from the left side menu and select the EC2 instance which you created in the above step.

3. Please follow the steps in [SSH into EC2 Instance](https://play.whizlabs.com/site/task_support/ssh-into-ec-instance).

4. Configure the **AWS CLI** on the EC2 instance by running the following command:

```
aws configure
```

AWS Access Key ID \[None] : Enter the **access key** which is available below the lab credentials

AWS Secret Access Key \[None] : Enter the **secret access key** which is available below the lab credentials

Default region name \[None] : Enter **us-east-1**

Default output format \[None] : Click the **Enter** button.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/014.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=d9ff7cf60c6c7a24e52c32767c98f520" alt="" width="1670" height="240" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/014.png" />

5. Copy the CLI commands for API gateway. To generate a UUID in Bash follow below command:

```
echo STATEMENT_ID=$(uuidgen)
```

6. To combine certain information to ensure uniqueness follow below command:

```
STATEMENT_ID="my_statement_id_$(date +%s)"  # This appends current timestamp to the statement ID
```

7. Now, run the below command. Ensure you replace the **\<productionlambda-function-arn>** with the function arn of **production lambda** and **\<api-gateway-arn>** with the arn of the **api gateway** you have copied earlier.

```plaintext theme={null}
aws lambda add-permission \
  --function-name "<productionlambda-function-arn>" \
  --source-arn "<api-gateway-arn>" \
  --principal apigateway.amazonaws.com \
  --statement-id "$STATEMENT_ID" \
  --action lambda:InvokeFunction \
  --region us-east-1
```

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/015.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=a18216fc7acd9b94a98eaa390725b9a4" alt="" width="2242" height="184" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/015.png" />

8. Now, you will be able to see a JSON success output.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/016.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=360c242b5e26c4772eea8ddbba4c3ab5" alt="" width="2874" height="176" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/016.png" />

9. Similarly, run the same command but change the function name to **TestingLambda** (Second lambda function you created). Ensure you replace the **\<testinglambda-function-arn>** with the function arn of **testing lambda** and **\<api-gateway-arn>** with the arn of the **api gateway** you have copied earlier.

```plaintext theme={null}
aws lambda add-permission \
  --function-name "<testinglambda-function-arn>" \
  --source-arn "<api-gateway-arn>" \
  --principal apigateway.amazonaws.com \
  --statement-id "$STATEMENT_ID" \
  --action lambda:InvokeFunction \
  --region us-east-1
```

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/017.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=e950399c9fa2ff4275f1dbf488aa0c23" alt="" width="2202" height="176" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/017.png" />

10. Now, you will be able to see a JSON success output.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/018.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=8e29721692ce78f19fe71f00bc9f7d3e" alt="" width="2878" height="178" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/018.png" />

#### Task 8: Deploy API Gateway with two different stages

In this task, we are going to assist the user in deploying the API Gateway with two different stages: TestingAPI and ProductionAPI. These stages provide separate environments for testing and production.

##### Testing Lambda Stage

1. Once the resource and the method have been created successfully, you can deploy the API.
2. Click on **Deploy API.**
3. Select the Deployment Stage in the drop-down as **New Stage**.
4. Stage Name: Enter **TestingAPI** and Stage description as ***Testing environment for my WhizlabsAPI***.
5. Click on **Deploy**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/019.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=7583ef657872e027771406d428ecdd0e" alt="" width="1086" height="1036" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/019.png" />

6. Once the API has been deployed, navigate to **Stages.**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/020.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=9ceafee3d4fe46397759256e9061f33d" alt="" width="1326" height="520" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/020.png" />

##### Production Lambda stage

1. Click on the **Resources** from the left side menu.

2. Select the GET method which you have created.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/021.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=2b870a91a37715f79d37a1c916e3817f" alt="" width="2152" height="956" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/021.png" />

3. Select **Deploy API.**

4. Select the Deployment Stage in the drop-down as **New Stage**.

5. Stage Name: Enter **ProductionAPI** and Stage description as ***Production environment for my WhizlabsAPI***.

6. Click on **Deploy**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/022.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=c4ab195878d87b721faa5e8aa18d2266" alt="" width="1088" height="1036" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/022.png" />

7. Once the API has been deployed, navigate to **Stages.**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/023.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=487693c6d8a3cd117260e39d9b1f520b" alt="" width="1530" height="712" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/023.png" />

#### Task 9: Add stage variables to both stages

In this task, we are going to add stage variables to the deployed stages. The stage variables will be used to define the specific Lambda functions associated with each stage.

1. Now click on the **TestingAPI** stage in Stages.
2. Select the **Stage Variables** tab in the right side page and click on **Edit** and then **Add Stage Variable**

   * Name: Enter **functionName**
   * Value: Enter **TestingLambda**
   * Note: In the value field, enter the Testing lambda function name.
   * Now click on the **Save** button.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/024.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=049cf61e5c06ee28f2aec81b4f39846d" alt="" width="1474" height="592" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/024.png" />

3. Now click on the **ProductionAPI** stage in Stages.
4. Select the **Stage Variables** tab in right side page and click on **Add Stage Variable**

   * Name: Enter **functionName**
   * Value : Enter **ProductionLambda**
   * Note: In the value field, enter the production lambda function name.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/025.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=3942f607503a77a0b8fa3e3d69b19078" alt="" width="1470" height="634" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/025.png" />

#### Task 10: Test the API Gateway

In this task, we are going to test the API Gateway by making GET requests to the endpoints associated with the deployed stages. This ensures that the API Gateway is properly integrated with the Lambda functions and functioning as expected.

1. Click on the **ProductionLambda** stage, open the stage
2. Now click on the GET method.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/026.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=9fa2ee34efa3abd2b48e4db673545218" alt="" width="510" height="270" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/026.png" />

3. Copy and Paste the **Invoke URL** (followed by the resource name) in the new tab to make a GET request.
4. You will receive the GET request from the API. Here's an example:

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/027.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=cca72aadf8609f2eefa0fcacd1420e60" alt="" width="808" height="93" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/027.png" />

> **Note**: If you make any mistake and after correcting it, still the API URL is showing the same error, please force refresh or clear cache.

5. Similarly, click on the **TestingLambda** stage, open the stage
6. Now click on the GET method.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/028.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=f1612b887edf652a52f7b2eede24b514" alt="" width="488" height="246" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/028.png" />

7. Copy and Paste the **Invoke URL** (followed by the resource name) in the new tab to make a GET request.
8. You will receive the GET request from the API. Here's an example:

<img src="https://mintcdn.com/ip-cloud-architect-pathway/1rdENrngau2mVfJg/images/labs/build-api-gateway-with-different-stages-using-stage-variables/029.png?fit=max&auto=format&n=1rdENrngau2mVfJg&q=85&s=77bfe544c93a98c5bc8367c7d0a6c354" alt="" width="790" height="106" data-path="images/labs/build-api-gateway-with-different-stages-using-stage-variables/029.png" />

> ##### Do you Know?
>
> API Gateway is designed to handle massive scale and can support up to hundreds of thousands of concurrent API requests. This means that even if your application experiences sudden spikes in traffic, API Gateway can handle the increased load seamlessly without any additional configuration or scaling effort on your part.

1. Once the lab steps are completed, please click on the **Validation** button on the left side panel.
2. This will validate the resources in the AWS account and shows you whether you have completed this lab successfully or not.

### Completion and Conclusion

1. You have successfully created an EC2 instance.
2. You have successfully created two Lambda functions.
3. You have successfully created the API.
4. You have successfully created the API Resource and Method.
5. You have successfully created two stages for API.
6. You have successfully added stage variables for both stages.
7. You have successfully tested the API

### End Lab

1. Sign out of AWS Account.
2. You have successfully completed the lab.
3. Once you have completed the steps, click on **End Lab** from your IP Lab Portal and wait till the process gets completed.

## What gets checked

When you press **Check my work**, the platform verifies each of these:

* **Launch an EC2 Instance** — Check whether an EC2 Instance is launched or not.
* **Create a API Gateway** — Check whether a REST/HTTP?WEBSOCKET API gateway is created or not
* **Create an AWS Lambda Function** — Check whether a Lambda Function is created or not

## Related help

* [FAQs and Troubleshooting](/aws-saa/support/faqs-and-troubleshooting)
* [SSH into EC2 Instance](/aws-saa/support/ssh-into-ec2-instance)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.