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

263
Views
Error de AWS Lambda "la propiedad iam no existe" al acceder al atributo iam Cognito que sé que existe

Recibo un error al intentar acceder a un objeto que sé que existe.

Al intentar acceder a la propiedad iam del objeto event.authorizer devuelto por AWS Cognito, aparece un error tanto en VS Code como en el terminal una vez que lo ejecuto.

 src/private.ts:9:33 - error TS2339: Property 'iam' does not exist on type '{ jwt: { claims: { [name: string]: string | number | boolean | string[]; }; scopes: string[]; }; }'.ts(2339)

Este error es especialmente desconcertante, porque cuando imprimo el objeto "evento", muestra evidencia de que existe.

Salida de event.requestContext :

{"statusCode":200,"headers":{"Content-type":"text/json"},"body":"{\"event\":{\"accountId\":\"365416766059\",\"apiId\":\"8ihwuguj40\",\"authorizer\":{\"iam\":{\"accessKey\":\"ASIAVKFE... 1068 more characters"}

Esto muestra que hay un objeto iam que es el valor del atributo del authorizer . Esta es la prueba de que existe. Entonces, ¿por qué VS Code tiene un problema con eso?


Ejecuté el código usando npx sst start , que ejecuta el código sin problemas y devuelve el objeto de event esperado con authorizer y awt anidados.

El código completo para la función lambda se encuentra a continuación.

 import { APIGatewayProxyHandlerV2 } from "aws-lambda" export const handler: APIGatewayProxyHandlerV2 = async (event) => { const rand = Math.floor(Math.random() * 10) const authoriser = event.requestContext.authorizer const accountId = authoriser?.iam.accountId ?? "" // Problem starts with reference to "iam" here. return { statusCode: 200, headers: {"Content-type": "text/json"}, body: JSON.stringify({ message: `Private random number: ${rand}`, accountId: accountId}), } }

Yo he tratado:

  • Hacer que iam opcional agregando un signo de interrogación.
  • Usando un bloque try-catch alrededor de la declaración.
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

el event está mal escrito.

Al escribir su función como APIGatewayProxyHandlerV2 , le ha dicho a TypeScript que el event es del tipo APIGatewayProxyEventV2 ( typedef aquí ). El event real de Lambda que obtiene no es de ese tipo, como se desprende de la salida del registro.

 // what typescript thinks your event is: export interface APIGatewayProxyEventV2 { version: string; routeKey: string; rawPath: string; rawQueryString: string; cookies?: string[] | undefined; headers: APIGatewayProxyEventHeaders; queryStringParameters?: APIGatewayProxyEventQueryStringParameters | undefined; requestContext: { accountId: string; apiId: string; authorizer?: { jwt: { claims: { [name: string]: string | number | boolean | string[] }; scopes: string[]; }; } | undefined; domainName: string; domainPrefix: string; ...etc

Por lo tanto, debe corregir el tipo de event . ¿Cuáles son tus opciones?

  1. Busque un tipo predefinido que coincida con el evento lambda
  2. Ser perezoso y odiarte a ti mismo: event: any
  3. Defina un tipo mínimo que satisfaga el contrato: event: MyLambdaEvent
 // custom event type // match the required parts of the event object lambda is actually giving you interface MyLambdaEvent { authorizer: { iam: { accountId: string } } }
about 4 years ago · Juan Pablo Isaza 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!