Get DCV URL
The first thing is that we need to define a new API Gateway URL. This one is going to be a bit more complicated than the first one, just follow these steps.
API Gateway Setup
1. Create the API
- Navigate to API Gateway in the AWS Console.
- Click Create API > REST API > Build.
- Settings:
- Protocol:
REST - Create new API:
New API - API name:
ElephantJoinAPI - Endpoint Type:
Regional
- Protocol:
- Click Create API.
2. Build the Resource Structure
You need to create a nested path: /join/{meetingId}/{role}.
- Create
/join:- Select the root (
/) > Actions > Create Resource. - Resource Name:
join - Click Create Resource.
- Select the root (
- Create
/{meetingId}:- Select
/join> Actions > Create Resource. - Resource Name:
meetingId - Resource Path:
{meetingId}(Ensure curly braces are used). - Click Create Resource.
- Select
- Create
/{role}:- Select
/{meetingId}> Actions > Create Resource. - Resource Name:
role - Resource Path:
{role} - Click Create Resource.
- Select
3. Configure the GET Method
- Select the
/{role}resource. - Click Actions > Create Method.
- Select GET from the dropdown and click the checkmark.
- Setup Integration:
- Integration type:
Lambda Function - Use Lambda Proxy integration:
Check(Recommended for easy access to path parameters). - Lambda Function: Enter
GetDcvUrl(or your specific redirection Lambda name). - Click Save.
- Integration type:
4. Deploy
- Click Actions > Deploy API.
- Deployment stage:
[New Stage](e.g.,prod). - Click Deploy.
Your API should look like this:

Awesome - Link as Storage
This allows us to pass in headers into the API and still use the GET method. This is ideal because then there's no middle application that needs to store the payload for when the users want to enter their meeting. Think about it, the meeting could be a week away. This link is effectively the storage! Pretty sick.
Now, let's get to the Lambda.
Environment Variables
None :-)
Code Breakdown
path_params = event.get('pathParameters', {})
meeting_id = path_params.get('meetingId')
user_role = path_params.get('role')
if not meeting_id or user_role not in ['teacher', 'student']:
return {
'statusCode': 400,
'body': json.dumps({'message': 'Invalid meeting ID or role.'}),
'headers': {'Access-Control-Allow-Origin': '*'}
}
- The frontend is going to send the primary key from DynamoDB and the role via the header, and the Lambda is going to parse those.
link_field = f"{user_role}_ec2_link"
final_dcv_url = item.get(link_field)
- This either pulls from DynamoDB the column of
student_ec2_linkorteacher_ec2_link.
if not final_dcv_url or current_status not in ['HEALTHY', 'IN_PROGRESS']:
print(f"Waiting for ready state. Link: {bool(final_dcv_url)}, Status: {current_status}")
return {
'statusCode': 202, # Signal to Frontend: "Accepted, keep polling."
'headers': {'Access-Control-Allow-Origin': '*'},
'body': json.dumps({
'message': 'Instance is warming up. Please wait.',
'status': current_status
})
}
Note - Health Check Status
We need a confirmation that the EC2 is ready for the user, which is what this guard is for. "Healthy" comes from the health check which is covered in another Phase.
try:
table.update_item(
Key={'id': meeting_id},
UpdateExpression="SET #s = :new_status",
ExpressionAttributeNames={'#s': 'status'},
ExpressionAttributeValues={':new_status': 'IN_PROGRESS'}
)
- We write to DynamoDB the status of
IN_PROGRESS. IN_PROGRESSis a signal that the other user is already in the session. We use it as a guard for termination, which we'll get to after this.
IAM Role
- AWSLambdaBasicExecutionRole, pre-built by AWS
- DynamoRead, custom inline
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "dynamodb:GetItem",
"Resource": "arn:aws:dynamodb:us-west-2:034489661489:table/elephant-meetings"
}
]
}
Awesome - Complete Flow
At this point, you should be able to schedule a meeting, and watch the EC2 instances spin up, and then join them via a redirect URL. Like, just take a pause. We just provisioned virtual machines and fully redirected users into them, and it only took like 4 python scripts and a few extra services. The internet is lit.
Congratulations! You've completed Phase 4. You should now have a complete flow from scheduling a meeting to launching EC2 instances and redirecting users to their dedicated virtual machines.