1. Create the Function in AWS Lambda
Step 1: Access the Lambda Console
- Log in to your AWS Console.
- In the search bar, type “Lambda” and select AWS Lambda.
- Click on Create function.
Step 2: Configure the Function
- Select “Author from scratch”.
- Fill in the following fields:
- Function name:
assign_badges_lambda
- Runtime:
Python 3.11
- Architecture:
x86_64
- Execution role:
- Select “Create a new role with basic Lambda permissions”.
- Function name:
- Click on Create function.
2. Create the Code and Dependencies
We need a deployment package (ZIP file) with our code in lambda_function.py
and the required libraries (e.g., requests
).
Step 1: Prepare the Package Locally
- Create a folder, for example:
su-lambda
. - Inside
su-lambda
, createlambda_function.py
with example code that uses a refresh token if the access token has expired. Assume your API has an endpoint/auth/login/refresh/
that receives{"refresh": "<REFRESH_TOKEN>"}
and returns a new access token.
... (same code, no translation needed) ...
- Compress everything into a ZIP:
zip -r deployment_package.zip .
Notes:
refresh_access_token
handles the flow to request a new access token.- It assumes the API returns a JSON object with the
"access"
key if successful. - If your API uses cookies/CSRF, you’ll need to extend the code to retrieve and attach a
csrftoken
in each request.
3.- From the su-lambda
folder, install the dependencies (e.g. requests
):
pip3 install requests -t .
4.- Compress everything into a ZIP:
zip -r deployment_package.zip .
Step 2: Upload the ZIP to AWS Lambda
- Go back to the AWS Lambda console and select your
assign_badges_lambda
function. - Go to the Code tab and click on “Upload from” → “.zip file”.
- Select
deployment_package.zip
and confirm. - Click on Deploy.
3. Set the Environment Variables
Go to the Configuration tab → Environment variables to specify your API URL, access token, refresh token, etc.
For example:
- Key:
DJANGO_API_URL
Value:https://api.example.com/assign-badges/
- Key:
DJANGO_REFRESH_URL
Value:https://api.example.com/auth/login/refresh/
- Key:
DJANGO_API_ACCESS_TOKEN
Value:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
(initial token) - Key:
DJANGO_API_REFRESH_TOKEN
Value:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
(refresh token)
Tip:
If you’re handling real credentials (user/password, tokens, etc.), it’s safer to use AWS Secrets Manager or AWS Systems Manager Parameter Store with encryption and rotation.
4. Schedule Execution with AWS EventBridge
To make your Lambda run automatically (e.g., daily):
- Go to EventBridge in the AWS console.
- Click Rules → Create rule.
- Assign a name, e.g.,
DailyAssignBadges
. - Select Schedule and use a cron expression. For example, for midnight daily:
cron(0 0 * * ? *)
- In Select target, choose Lambda function and select
assign_badges_lambda
. - Click Create.
5. Monitoring with CloudWatch
- In the AWS console, open CloudWatch.
- Select Logs → Log groups.
- Search for
/aws/lambda/assign_badges_lambda
. - Click to view the logs, where you’ll find info and errors (
logger.info
,logger.error
).
6. Testing and Verification
- In Lambda, go to the Test tab.
- Create a test event (a simple JSON, e.g.,
{"test": "run"}
). - Run the test and check if the invocation is
SUCCESS
orERROR
. - Open CloudWatch to view logs and diagnose issues.
Notes on Cookies and CSRF
- If your API uses cookies for session management and requires a
csrftoken
, you’ll need to:- Make an initial
GET
request to obtain the cookie. - Read the
csrftoken
and send it in theX-CSRFToken
header. - Include the cookie in each
POST
(usingsession.cookies
or theCookie: ...
header).
- Make an initial
- If your API does not exempt JWT endpoints from CSRF, update Django or DRF settings (e.g.
@csrf_exempt
) to avoid blocking headless token requests.
Conclusion
With this flow:
- You deployed a Lambda function using
requests
. - You handled a refresh token to obtain a new access token when needed.
- You configured environment variables (or Secrets Manager) to avoid exposing credentials in code.
- You scheduled the function using EventBridge and monitor it with CloudWatch.
This way, your Lambda can interact with secure APIs (JWT + refresh tokens) without manual credential input, executing badge assignment (or any other logic) automatically.