Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
AWS Lambda: Give Lambda same role as signed in user

Im building a serverless application using the following AWS technologies:

  • AWS Cognito
  • AWS Lambda
  • AWS API gateway
  • AWS IAM

All requests from the client (programmed in Angular2) go through API gateway.

I have created a IAM role for signed in users: Cognito_MyApp_Auth. I use a Cognito User Pool Authorizer to make sure all calls to my API are from valid signed-in users.

Question: How can I grant the Lambda function the same permission as the signed in user?

Use case: A signed in user may only create, edit or delete in his own S3 bucket, so granting Lambda full permission to S3 is not an option.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Cognito_MyApp_Auth does not set different permissions from user to user. It only defines the permissions for any signed user. So you should allow access to all buckets in this role's policy. (However, I'd limit this access to buckets starting with a specific prefix.)

Fortunately, when Lambda is invoked via Cognito SDK, using Cognito provided credentials, context object passed to handler carries Cognito identity used to invoke the Lambda.

For Node.js you can see how to get this information:

http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html#nodejs-prog-model-context-example

if (typeof context.identity !== 'undefined') {
    console.log('Cognito
    identity ID =', context.identity.cognitoIdentityId);
}    

Once you have the identity id, you can set up your own logic to limit the user's access to her own bucket.

This was the case when you call the Lambda directly from the application using Cognito SDK. If you use API Gateway, there is a good post from an AWS employee: https://forums.aws.amazon.com/thread.jspa?messageID=717379

about 4 years ago · Santiago Trujillo Report

0

There is no direct way to grant the Lambda function the same permissions as the signed in user; however, you should be able to implement your use case using Cognito identity pools and policy using the cognito-identity context variable.

For this approach, you won't use Cognito user pools nor the Cognito user pool authorizer. Instead, you use a Cognito identity pool for federated identity. Set the authorization on you API methods as AWS_IAM. Don't use Lambda as the integration, but instead use the AWS proxy to integrate directly to S3. Set the integration request to use the caller's identity.

To cal the API you'll need to use Cognito to get temporary credentials and then sign your API request with them.

With this approach, the S3 call is made with Cognito identity context, so you can attach policy which uses cognito-identity variables to the IAM role associated with authenticated users in your Cognito identity pool.

For example:

{
  "Effect": "Allow",
  "Action": [
    "s3:PutObject"
  ]
  "Resource": [
    "arn:aws:s3:::BUCKET_FOR_USER_${cognito-identity.amazonaws.com:sub}/*"
  ]
}

See this post for a similar use case.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!