Can a python API app trigger a lambda? (EC2 / locally)

0

So, i think my question is pretty clear.

I have an API written in python (FastAPI), and i want to trigger an AWS Lambda function. Currently the python api service creates an API request to an AWS API Gateway, which seems funny to me.
The app is deployed on an EC2 server, but, I run it locally too, and expect it to work.

is it possible? Where can i find a reference for that?

asked a year ago334 views
1 Answer
1

Giving the correct permissions via a role assignment (instance profile), you can invoke a lambda function directly.

Also, if you want to hit an HTTPS endpoint instead of calling the Lambda API, you could use the new Lambda Function URLs.

For non-AWS platforms like your laptop, you can install the AWS CLI and run the aws configure command to setup the access keys. See: Configuring the AWS CLI.

If you want to use a IAM Role (best practice) instead of an IAM user, take a look at AWS Identity and Access Management Roles Anywhere.

Here is the documentation for setting up credentials so that the SDK code can get access keys.

profile pictureAWS
EXPERT
kentrad
answered a year ago
  • That's what i thought. Having an IAM permission for an EC2 server. But how does this apply locally, for example, I usually run my app locally with docker, and can trigger the api routes (urls).

    I do not want to hit anything else, but rather to trigger a lambda directly from a server.

  • So if i understand correct, using EC2, I give it an IAM permission for triggering lambdas (would be happy if you can give me a hint how to do that) and then my python code should look like this:

    import boto3 
    
    
    response = boto3.lambda_client.invoke(
                    FunctionName=add_student,
                    Payload=json.dumps( { fname: "potato", lname: "student" } ),
                    LogType='Tail' if get_log else 'None')
    )
    

    which response will get the response from the lambda. Did i write it properly? again, not sure how to apply this locally with IAM..

  • To assign a role (and the associated permissions) to an EC2 instance: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions