Autorizzazioni per lambda per creare l'evento degli eventi:PutEvents

0

Domanda

Voglio avere una lambda creazione di EventBridge eventi ma mi da questo errore quando si chiama il lambda:

User: arn:aws:sts::120293923901:assumed-role/MyApiOrdersPostFunct-I1QOYC7P1R0Z/MyApiOrdersPostFunct-SJtAeYoiaguW is not authorized to perform: events:PutEvents on resource: arn:aws:events:eu-north-1:120293923901:event-bus/MyApiEventBus because no identity-based policy allows the events:PutEvents action

Ho aggiunto le politiche, ma nessun cambiamento.

Qui è la lambda chiamata eventbridge.


import { APIGatewayProxyHandler, APIGatewayProxyResult } from 'aws-lambda';
import { EventBridgeClient, PutEventsCommand } from '@aws-sdk/client-eventbridge';

const eventBridge = new EventBridgeClient({ region: 'eu-north-1' });

export const post: APIGatewayProxyHandler = async (): Promise<APIGatewayProxyResult> => {
    const event = new PutEventsCommand({
        Entries: [{
            EventBusName: 'MyApiEventBus',
            Source: 'MyApiEventBus.OrderCreated',
            DetailType: 'OrderCreated',
            Detail: JSON.stringify({ description: 'order has been created' }),
        }]
    });
        eventBridge.send(event);

    return {
        statusCode: 200,
        body: '',
    };
};

Qui è il CDK config. Ci sono due criteri (attachInlinePolicy, addToRolePolicy) perchè l'ho provato entrambi.

import {
    RestApi,
    DomainName,
    BasePathMapping,
    LambdaIntegration,
    Model,
} from '@aws-cdk/aws-apigateway';
import { EventBus, Rule } from '@aws-cdk/aws-events';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import { Policy, PolicyStatement } from '@aws-cdk/aws-iam';

const MyApi = new RestApi(this, `RestApi`, {
  restApiName: 'My API',
  description: 'The My API',
});

// Add an Event Bus
const bus = new EventBus(this, `EventBus`, {
  eventBusName: 'MyApiEventBus',
});

// Add API endpoint
const ordersResource = MyApi.root.addResource('orders');

const ordersPostFunction = new NodejsFunction(this, `OrdersPostFunction`, {
  entry: './lambda.ts',
  handler: 'post',
});

// Allow lambda to create events
ordersPostFunction.addToRolePolicy(
  new PolicyStatement({
    actions: ['events:PutEvents'],
    resources: [bus.eventBusArn],
  }),
);

ordersPostFunction.role?.attachInlinePolicy(
  new Policy(this, `OrdersPostEventBusPolicy`, {
    statements: [
      new PolicyStatement({
        actions: ['events:PutEvents'],
        resources: [bus.eventBusArn],
      }),
    ],
  }),
);

// Role to allow for creating event (not working?)
bus.grantPutEventsTo(ordersPostFunction);

Lambda ruolo di documento

{
  "sdkResponseMetadata": null,
  "sdkHttpMetadata": null,
  "partial": false,
  "permissionsBoundary": null,
  "policies": [
    {
      "arn": null,
      "document": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Action": "events:PutEvents",
            "Resource": "arn:aws:events:eu-west-1:120293923901:event-bus/MyApiEventBus",
            "Effect": "Allow"
          }
        ]
      },
      "id": null,
      "name": "MyApiOrdersPostEventBusPolicyACA51C2D",
      "type": "inline"
    },
    {
      "arn": null,
      "document": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Action": "events:PutEvents",
            "Resource": "arn:aws:events:eu-west-1:120293923901:event-bus/MyApiEventBus",
            "Effect": "Allow"
          }
        ]
      },
      "id": null,
      "name": "MyApiOrdersPostFunctionServiceRoleDefaultPolicyE7615F17",
      "type": "inline"
    },
  ]
}
2

Migliore risposta

2

Un Evento Autobus è situata nel eu-west-1 regione, come mostrato dalla generato politica, ma si sta cercando di accedere da eu-north-1. Cambiare la regione e funziona.

2021-11-22 15:33:47
1

Il send metodo di EventBridgeClient è asincrono. Quindi dovrebbe essere:

await eventBridge.send(event);

Altrimenti non notare le eccezioni generate da questo.

2021-11-22 14:59:02

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................