Objective: Create a GET method on the
/students
resource in thestudent
API (created in section 4.1) to integrate with thegetStudentData
Lambda function (created in section 3.1), allowing the retrieval of the student list from thestudentData
DynamoDB table. The method will require an API Key (StudentApiKey
, created in section 4.2) in thex-api-key
header for security, and prepare for enabling CORS (section 4.7) so the web interface (running on CloudFront) can make requests.
getStudentData
Lambda function to fetch all records from the studentData
DynamoDB table (fields: studentid
, name
, class
, birthdate
, email
).getStudentData
function will return a JSON response with the header Access-Control-Allow-Origin: '*'
to support CORS, suitable for the web interface.StudentApiKey
will be processed.You need to complete section 4.1 (create the student
API), section 4.2 (create the StudentApiKey
API Key), section 4.3 (create the StudentUsagePlan
Usage Plan), and section 3 (create the Lambda functions getStudentData
, insertStudentData
, BackupDynamoDBAndSendEmail
, the studentData
DynamoDB table, the student-backup-20250706
S3 bucket, SES email verification). Ensure your AWS account is set up and the AWS region is us-east-1
.
Access AWS Management Console
Open your browser and log in to AWS Management Console with your AWS account.
In the search bar at the top, type API Gateway and select the Amazon API Gateway service to access the management interface.
Check the AWS region: Ensure you’re working in the primary AWS region (assumed to be us-east-1
for synchronization with previous sections), and check the region at the top right corner of the AWS Console. This region must match the student
API, the getStudentData
Lambda function, the studentData
DynamoDB table, the student-backup-20250706
S3 bucket, and SES.
Figure 1: AWS Console Interface with API Gateway Search Bar.
Navigate to the APIs Section
In the main Amazon API Gateway interface, look at the left navigation menu.
Select APIs to view the list of existing APIs.
The list will show the student
API (created in section 4.1). If not visible, check the AWS region again or refresh the page.
Figure 2: Navigation Menu with APIs Option.
Select the student
API
In the APIs list, find and select the student
API.
You’ll be taken to the student
API management page, displaying options like Resources, Stages, API Keys, etc.
Select Resources from the left menu to start configuring the resource and method.
Figure 3: API Management Page for
student
with Resources Option.
Create the /students
Resource
In the Resources interface, you’ll see the root /
.
Click Actions > Create Resource to create a new resource.
Configure the resource:
students
./students
(or leave it as the default, which will automatically be /students
).Click Create Resource to create the resource.
Check: The /students
resource will appear under the root /
in the resource tree.
Figure 4: Create /students
Resource Interface.
Create the GET Method
In the resource tree, select the /students
resource.
Click Actions > Create Method.
From the dropdown under /students
, select GET and click the checkmark (✔) to confirm.
Note: If the dropdown doesn’t show GET, ensure you’ve selected the correct /students
resource.
Integration Type: Select Lambda Function to integrate with the Lambda function.
Figure 5: Create GET Method Interface.
Configure Lambda Integration
In the GET method configuration interface:
us-east-1
(or your AWS region, which must match the region of the getStudentData
function).getStudentData
.
getStudentData
function doesn’t appear in the suggestion list, enter it manually and ensure the function exists in Lambda (section 3.1).If AWS prompts for permissions, click OK to allow API Gateway to invoke the getStudentData
Lambda function. AWS will automatically add the IAM policy to the Lambda function’s role (usually LambdaGetStudentRole
from section 3.1) with the lambda:InvokeFunction
permission.
Figure 6: Lambda Integration Configuration Interface.
Enable API Key Requirement
In the Method Request interface for GET /students:
x-api-key
header.
StudentApiKey
(created in section 4.2) in the x-api-key
header.
Figure 7: Enable API Key Required Interface.
Check the Status of Method Creation
After configuring and clicking Save, you’ll see the message: “Successfully created method ‘GET’. Redeploy your API for the update to take effect.”
Important Note: The GET method will not work until you deploy the API to a stage (section 4.8).
To check the configuration:
/students
.getStudentData
function exists in Lambda > Functions.apigateway:PUT
permission to create methods.getStudentData
(AWS automatically adds permission when you click OK).
Figure 8: Success Message After Creating GET Method.
Element | Details |
---|---|
Lambda Proxy Integration | Lambda Proxy integration allows sending the entire HTTP request (headers, query parameters, body) to the getStudentData function and receiving a JSON response with headers (like Access-Control-Allow-Origin: '*' ). Ensure the getStudentData function (section 3.1) returns the response in the correct format. |
API Key Security | With API Key Required: true, requests to GET /students must include the header x-api-key: <StudentApiKey> . For enhanced security, store the API Key in AWS Secrets Manager (see section 4.2). |
CORS | The GET method must support CORS for the web interface to make cross-origin requests. This will be configured in detail in section 4.7 (enabling CORS with the OPTIONS method). Ensure the getStudentData function returns the Access-Control-Allow-Origin: '*' header (or a specific CloudFront domain, e.g., https://d12345678.cloudfront.net ). |
AWS Region | Ensure the us-east-1 region matches the region of the getStudentData function, the studentData table, and the student API. If using a different region (e.g., us-west-2 ), select the correct region in the Lambda Region. |
Error Handling | - If you encounter the error “Lambda function not found”: - Check that the getStudentData function exists in Lambda > Functions. - Ensure the AWS region matches (us-east-1 ). - If you encounter a 403 "Forbidden" error when calling the API (after deployment): - Check API Key Required: true and ensure the StudentApiKey is valid. - Ensure the API Key is linked to the Usage Plan (sections 4.3, 4.9). - If you receive a 500 error from Lambda, check the logs in CloudWatch (log group /aws/lambda/getStudentData ) for debugging. |
Optimization | - Add the Access-Control-Allow-Origin header in the Method Response to ensure CORS works correctly: - In Method Response for GET /students, add Status Code 200 with the header Access-Control-Allow-Origin: '*' . - In Integration Response, map the response from Lambda to return a properly formatted JSON response. - Consider using AWS WAF with API Gateway to protect against DDoS attacks or API Key abuse. - If the studentData table is large, ensure the getStudentData function handles pagination (as in the improved code from section 3.1) to avoid exceeding the Scan limit. |
Early Testing | - After creating the GET method, verify the configuration in Resources > GET /students (Integration Request, Method Request). - After deploying the API (section 4.8), test the GET method using Postman or curl. - If you receive a 403 "Forbidden" error, check the API Key or API Key Required configuration. - If you receive a 500 error, check the CloudWatch logs for the getStudentData function. |
Web Interface Integration Testing | After deploying the API (section 4.8) and linking the Usage Plan (section 4.9), use the API Key in the web interface (using Tailwind CSS, running on CloudFront) to call the GET /students endpoint. |
Practical Tip: Verify the Integration Request and API Key Required configurations before deploying the API. Test the JSON response from the
getStudentData
function using Postman to ensure the student data is returned in the correct format.
The GET /students method has been successfully created in the student
API, integrated with the getStudentData
Lambda function and requiring the StudentApiKey
API Key, ready for deployment and use in the web interface.
Next step: Go to Create POST Method to Store Data to continue!