Oprávnění pro lambda vytvořit událost akce:PutEvents

0

Otázka

Chci mít lambda vytváření EventBridge události, ale já se tuto chybu při volání 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

Přidal jsem politik, ale žádná změna.

Zde je lambda volání 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: '',
    };
};

Zde je CDK config. Existují dvě politiky (attachInlinePolicy, addToRolePolicy), protože jsem testoval oba.

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 roli dokumentu

{
  "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

Nejlepší odpověď

2

Vaše Akce Autobusu se nachází v eu-west-1 oblasti, jak je znázorněno tím, že vytvořené politiky, ale že se snažíš přistupovat z eu-north-1. Změna regionu, a to bude fungovat.

2021-11-22 15:33:47
1

Na send metoda EventBridgeClient je asynchronní. Takže by to mělo být:

await eventBridge.send(event);

Jinak by sis nevšimla, výjimky vyvolána.

2021-11-22 14:59:02

V jiných jazycích

Tato stránka je v jiných jazycích

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